From 0c0dad087f0f30198df1bdc2d916217b0498425d Mon Sep 17 00:00:00 2001
From: ArturPhilibin <1541024+ArturPhilibin@users.noreply.github.com>
Date: Wed, 12 Feb 2020 00:02:22 +0000
Subject: [PATCH] Fixed an issue where in some scenarios users coming back
online wouldn't be counted, incorrectly marking the user as still being
offline. Also changed last seen status to show hours when offline time
exceeeds 60 minutes
---
app/javascripts/components/gather.js | 26 +++++++++++---
lib/gather/controller.js | 51 ++++++++++++++++++++--------
2 files changed, 57 insertions(+), 20 deletions(-)
diff --git a/app/javascripts/components/gather.js b/app/javascripts/components/gather.js
index 223c182..ab62208 100644
--- a/app/javascripts/components/gather.js
+++ b/app/javascripts/components/gather.js
@@ -920,16 +920,32 @@ const GathererListItem = React.createClass({
}
const lastSeenInMinutes = (gatherer) => {
- return parseInt((Date.now() - gatherer.user.lastSeen) / (1000 * 60));
+ const now = Date.now();
+ const minutesSinceLastSeen = (now - gatherer.user.lastSeen) / 60000;
+
+ if (minutesSinceLastSeen < 0) {
+ return 0;
+ }
+
+ return parseInt(minutesSinceLastSeen || 0);
};
let idleStatus;
if (!gatherer.user.online) {
const mins = lastSeenInMinutes(gatherer);
- idleStatus = [
-
Last Seen,
- {mins} mins ago
- ]
+
+ if (mins > 60) {
+ const hours = Math.round(mins / 6) / 10;
+ idleStatus = [
+ Last Seen,
+ {hours} hours ago
+ ]
+ } else{
+ idleStatus = [
+ Last Seen,
+ {mins} mins ago
+ ]
+ }
}
let tabColor = gatherer.team !== "lobby" ? `panel-${gatherer.team}` : "panel-info";
diff --git a/lib/gather/controller.js b/lib/gather/controller.js
index 17c71d5..7728107 100644
--- a/lib/gather/controller.js
+++ b/lib/gather/controller.js
@@ -35,20 +35,6 @@ module.exports = function (namespace) {
});
};
- const updateGathererLastSeenStatuses = () => {
- const onlineIds = Object.values(namespace.sockets).map(s => s._user.id);
- const now = Date.now();
- GatherPool.forEach((gatherManager, type) => {
- for (var gatherer of gatherManager.current.gatherers) {
- if (gatherer.user.online) {
- gatherer.user.lastSeen = now;
- }
-
- gatherer.user.online = onlineIds.indexOf(gatherer.id) !== -1;
- }
- });
- };
-
const refreshArchive = () => {
ArchivedGather.recent((error, recentGathers) => {
if (error) return winston.error(error);
@@ -70,6 +56,29 @@ module.exports = function (namespace) {
}
}
+ const updateUserAsOnlineInGather = (userId) => {
+ return GatherPool.forEach((gatherManager, type) => {
+ for (var gatherer of gatherManager.current.gatherers) {
+ if (gatherer.user.id == userId && !gatherer.user.online) {
+ gatherer.user.online = true;
+ }
+ }
+ });
+ };
+
+ const refreshGathersWhereUserWentOffline = (userId) => {
+ return GatherPool.forEach((gatherManager, type) => {
+ for (var gatherer of gatherManager.current.gatherers) {
+ if (gatherer.user.id == userId && gatherer.user.online) {
+ gatherer.user.online = false;
+ gatherer.user.lastSeen = Date.now();
+
+ refreshGather(type);
+ }
+ }
+ });
+ };
+
const gatherRefreshers = {}; // Stores debounced procedures to refresh gathers
GatherPool.forEach((gatherManager, type) => {
@@ -117,6 +126,10 @@ module.exports = function (namespace) {
}
namespace.on("connection", function (socket) {
+ const connectedUserId = socket._user && socket._user.id;
+
+ updateUserAsOnlineInGather(connectedUserId);
+
ArchivedGather.recent((error, recentGathers) => {
if (error) return winston.error(error);
socket.emit("gather:archive:refresh", {
@@ -126,7 +139,13 @@ module.exports = function (namespace) {
});
socket.on("disconnect", () => {
- updateGathererLastSeenStatuses();
+ const onlineIds = Object.values(namespace.sockets).map(s => s._user.id);
+
+ // need to check if this disconnect actually meant user went offline
+ // they could have another tab open...
+ if (onlineIds.indexOf(connectedUserId) === -1) {
+ refreshGathersWhereUserWentOffline(connectedUserId);
+ }
});
socket.on("gather:join", function (data) {
@@ -163,10 +182,12 @@ module.exports = function (namespace) {
const removeGatherer = (gather, user) => {
let gatherLeaver = gather.getGatherer(user);
+
if (gather.can("removeGatherer")) {
gather.removeGatherer(user);
}
if (user.cooldown) gather.applyCooldown(user);
+
Event.leaver(gatherLeaver.user);
refreshGather(gather.type);
}