Skip to content

Commit

Permalink
fix: allow guests to see their replies immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Nov 30, 2020
1 parent 7b39cf4 commit a4fe4d3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/api/topics.js
Expand Up @@ -8,6 +8,7 @@ const meta = require('../meta');
const apiHelpers = require('./helpers');
const doTopicAction = apiHelpers.doTopicAction;

const websockets = require('../socket.io');
const socketHelpers = require('../socket.io/helpers');

const topicsAPI = module.exports;
Expand Down Expand Up @@ -72,7 +73,12 @@ topicsAPI.reply = async function (caller, data) {
};

user.updateOnlineUsers(caller.uid);
socketHelpers.emitToUids('event:new_post', result, [caller.uid]);
if (caller.uid) {
socketHelpers.emitToUids('event:new_post', result, [caller.uid]);
} else if (caller.uid === 0 && caller.sessionID) {
websockets.in('sess_' + caller.sessionID).emit('event:new_post', result);
}

socketHelpers.notifyNew(caller.uid, 'newPost', result);

return postObj[0];
Expand Down
5 changes: 3 additions & 2 deletions src/socket.io/index.js
Expand Up @@ -198,14 +198,15 @@ async function authorize(socket, callback) {
}

await cookieParserAsync(request);

const sessionData = await getSessionAsync(request.signedCookies[nconf.get('sessionKey')]);
const sid = request.signedCookies[nconf.get('sessionKey')];
const sessionData = await getSessionAsync(sid);
if (sessionData && sessionData.passport && sessionData.passport.user) {
request.session = sessionData;
socket.uid = parseInt(sessionData.passport.user, 10);
} else {
socket.uid = 0;
}
socket.sessionID = sid;
request.uid = socket.uid;
callback();
}
Expand Down
6 changes: 6 additions & 0 deletions src/user/online.js
Expand Up @@ -7,6 +7,9 @@ var meta = require('../meta');

module.exports = function (User) {
User.updateLastOnlineTime = async function (uid) {
if (!(parseInt(uid, 10) > 0)) {
return;
}
const userData = await db.getObjectFields('user:' + uid, ['status', 'lastonline']);
const now = Date.now();
if (userData.status === 'offline' || now - parseInt(userData.lastonline, 10) < 300000) {
Expand All @@ -16,6 +19,9 @@ module.exports = function (User) {
};

User.updateOnlineUsers = async function (uid) {
if (!(parseInt(uid, 10) > 0)) {
return;
}
const now = Date.now();
const userOnlineTime = await db.sortedSetScore('users:online', uid);
if (now - parseInt(userOnlineTime, 10) < 300000) {
Expand Down

0 comments on commit a4fe4d3

Please sign in to comment.