mirror of
https://github.com/ENSL/ensl_gathers.git
synced 2025-02-18 09:51:26 +00:00
Refactored approach to showing offline users (#131)
This commit is contained in:
parent
8fe13a0629
commit
3d3323fe13
2 changed files with 23 additions and 20 deletions
|
@ -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 = {};
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue