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({
channel,
message,
owner: user.id,
owner: 'r/anonymous' === channel ? user.id : 0,
timestamp: Date.now(),
uuid: uuidv4(),
}));

View File

@ -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}), {});
};

View File

@ -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}`;