fix: mask anonymous user ID

This commit is contained in:
cha0s 2020-07-18 17:34:39 -05:00
parent 76a7a1943e
commit 369000ad46
3 changed files with 17 additions and 7 deletions

View File

@ -25,7 +25,7 @@ export default function ChatSubmitMessage() {
dispatch(submitMessage({ dispatch(submitMessage({
channel, channel,
message, message,
owner: user.id, owner: 'r/anonymous' === channel ? user.id : 0,
timestamp: Date.now(), timestamp: Date.now(),
uuid: uuidv4(), uuid: uuidv4(),
})); }));

View File

@ -13,7 +13,14 @@ export const channelUserCounts = async (req, channel) => {
const clients = promisify(req.adapter.clients.bind(req.adapter)); const clients = promisify(req.adapter.clients.bind(req.adapter));
const socketKeys = await clients([channel]); const socketKeys = await clients([channel]);
const customRequest = promisify(req.adapter.customRequest.bind(req.adapter)); 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}), {}); const socketUsers = replies.reduce((r, m) => ({...r, ...m}), {});
return 0 === socketKeys.length return 0 === socketKeys.length
? [] ? []
@ -34,7 +41,7 @@ export const channelState = async (req, channel) => {
uuid: messageKeys[i].split(':')[2], uuid: messageKeys[i].split(':')[2],
})) }))
.sort((l, r) => l.timestamp - r.timestamp); .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); const users = await channelUsers(req, channel);
return { return {
messages, messages,
@ -108,7 +115,10 @@ export const appUsernamesState = async (req) => {
const {User} = allModels(); const {User} = allModels();
const usernames = await Promise.all( const usernames = await Promise.all(
(await chatUsers(req)) (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}), {}); return usernames.reduce((r, [id, username]) => ({...r, [id]: username}), {});
}; };

View File

@ -50,7 +50,7 @@ export function createSocketServer(httpServer) {
next(); next();
}); });
const userJoin = async (channel, socket) => { 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); const users = await channelUsers(socket.handshake, channel);
if (-1 === users.indexOf(userId)) { if (-1 === users.indexOf(userId)) {
ServerSocket.send(socket.to(channel), new Join({channel, id: 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); await promisify(socket.join.bind(socket))(channel);
}; };
const userLeave = async (channel, socket) => { 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); await promisify(socket.leave.bind(socket))(channel);
const userCounts = await channelUserCounts(socket.req, channel); const userCounts = await channelUserCounts(socket.req, channel);
if (!userCounts[userId]) { if (!userCounts[userId]) {
@ -88,7 +88,7 @@ export function createSocketServer(httpServer) {
} }
if (packet instanceof Message) { if (packet instanceof Message) {
const {channel, message} = packet.data; 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 timestamp = Date.now();
const uuid = uuidv4(); const uuid = uuidv4();
const key = `${channel}:messages:${uuid}`; const key = `${channel}:messages:${uuid}`;