From 369000ad465ebbc415d283e4937f1e294ce6c028 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sat, 18 Jul 2020 17:34:39 -0500 Subject: [PATCH] fix: mask anonymous user ID --- src/client/chat--submitMessage.jsx | 2 +- src/server/entry.js | 16 +++++++++++++--- src/server/sockets.js | 6 +++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/client/chat--submitMessage.jsx b/src/client/chat--submitMessage.jsx index 895a73c..d4086be 100644 --- a/src/client/chat--submitMessage.jsx +++ b/src/client/chat--submitMessage.jsx @@ -25,7 +25,7 @@ export default function ChatSubmitMessage() { dispatch(submitMessage({ channel, message, - owner: user.id, + owner: 'r/anonymous' === channel ? user.id : 0, timestamp: Date.now(), uuid: uuidv4(), })); diff --git a/src/server/entry.js b/src/server/entry.js index 6c76de0..af85bce 100644 --- a/src/server/entry.js +++ b/src/server/entry.js @@ -13,7 +13,14 @@ export const channelUserCounts = async (req, channel) => { const clients = promisify(req.adapter.clients.bind(req.adapter)); const socketKeys = await clients([channel]); const customRequest = promisify(req.adapter.customRequest.bind(req.adapter)); - const replies = await customRequest({type: 'socketUsers', payload: socketKeys}); + // eslint-disable-next-line no-nested-ternary + const replies = '/r/anonymous' === channel + ? ( + socketKeys.length > 0 + ? [socketKeys.reduce((r, socketKey) => ({...r, [socketKey]: 0}), {})] + : [] + ) + : await customRequest({type: 'socketUsers', payload: socketKeys}); const socketUsers = replies.reduce((r, m) => ({...r, ...m}), {}); return 0 === socketKeys.length ? [] @@ -34,7 +41,7 @@ export const channelState = async (req, channel) => { uuid: messageKeys[i].split(':')[2], })) .sort((l, r) => l.timestamp - r.timestamp); - const userId = req.user ? req.user.id : 0; + const userId = '/r/anonymous' === channel ? 0 : req.userId; const users = await channelUsers(req, channel); return { messages, @@ -108,7 +115,10 @@ export const appUsernamesState = async (req) => { const {User} = allModels(); const usernames = await Promise.all( (await chatUsers(req)) - .map(async (id) => [id, (await User.findByPk(id)).redditUsername]), + .map(async (id) => [ + id, + 0 === id ? 'anonymous' : (await User.findByPk(id)).redditUsername, + ]), ); return usernames.reduce((r, [id, username]) => ({...r, [id]: username}), {}); }; diff --git a/src/server/sockets.js b/src/server/sockets.js index 9d03c7a..85f3261 100644 --- a/src/server/sockets.js +++ b/src/server/sockets.js @@ -50,7 +50,7 @@ export function createSocketServer(httpServer) { next(); }); const userJoin = async (channel, socket) => { - const {userId} = socket.handshake; + const userId = '/r/anonymous' === channel ? 0 : socket.handshake.userId; const users = await channelUsers(socket.handshake, channel); if (-1 === users.indexOf(userId)) { ServerSocket.send(socket.to(channel), new Join({channel, id: userId})); @@ -58,7 +58,7 @@ export function createSocketServer(httpServer) { await promisify(socket.join.bind(socket))(channel); }; const userLeave = async (channel, socket) => { - const {userId} = socket.req; + const userId = '/r/anonymous' === channel ? 0 : socket.handshake.userId; await promisify(socket.leave.bind(socket))(channel); const userCounts = await channelUserCounts(socket.req, channel); if (!userCounts[userId]) { @@ -88,7 +88,7 @@ export function createSocketServer(httpServer) { } if (packet instanceof Message) { const {channel, message} = packet.data; - const owner = req.user ? req.user.id : 0; + const owner = '/r/anonymous' === channel ? 0 : req.userId; const timestamp = Date.now(); const uuid = uuidv4(); const key = `${channel}:messages:${uuid}`;