Refactored approach to showing offline users (#131)

This commit is contained in:
ArturPhilibin 2019-02-14 20:21:29 +00:00 committed by Absurdon
parent 8fe13a0629
commit 3d3323fe13
2 changed files with 23 additions and 20 deletions

View file

@ -28,14 +28,26 @@ const winston = require("winston");
module.exports = function (namespace) {
const emitGather = (socket, gather) => {
const onlineIds = Object.values(namespace.sockets).map(s => s._user.id);
socket.emit("gather:refresh", {
gather: gather ? gather.toJson(onlineIds) : null,
gather: gather ? gather.toJson() : null,
type: gather ? gather.type : null,
maps: Map.list,
});
}
};
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) => {
@ -80,10 +92,8 @@ module.exports = function (namespace) {
gatherManager.onArchiveUpdate(refreshArchive);
gatherRefreshers[type] = _.debounce(function () {
const onlineIds = Object.values(namespace.sockets).map(s => s._user.id);
namespace.emit("gather:refresh", {
gather: gatherManager.current ? gatherManager.current.toJson(onlineIds) : null,
gather: gatherManager.current ? gatherManager.current.toJson() : null,
type: gatherManager.current ? gatherManager.current.type : null,
maps: Map.list,
});
@ -105,7 +115,7 @@ module.exports = function (namespace) {
}, refreshGather());
});
}
namespace.on("connection", function (socket) {
ArchivedGather.recent((error, recentGathers) => {
if (error) return winston.error(error);
@ -114,6 +124,10 @@ module.exports = function (namespace) {
maps: Map.list
});
});
socket.on("disconnect", () => {
updateGathererLastSeenStatuses();
});
socket.on("gather:join", function (data) {
if (!data) data = {};

View file

@ -337,18 +337,7 @@ Gather.prototype.electionStartTime = function () {
null : this.election.startTime.toISOString();
};
Gather.prototype.toJson = function (onlineIds) {
if (onlineIds) {
const now = Date.now();
for (var gatherer of this.gatherers) {
if (gatherer.user.online) {
gatherer.user.lastSeen = now;
}
gatherer.user.online = onlineIds.indexOf(gatherer.id) !== -1;
}
}
Gather.prototype.toJson = function () {
return {
name: this.name,
description: this.description,