feat: private messaging

This commit is contained in:
cha0s 2020-07-20 23:47:03 -05:00
parent d8c0c2ce76
commit d3ce6868a3
2 changed files with 24 additions and 8 deletions

View File

@ -32,7 +32,11 @@ export const channelUsers = async (req, channel) => (
); );
export const channelState = async (req, channel) => { export const channelState = async (req, channel) => {
const messageKeys = await keys(redisClient, `${channel}:messages:*`); const {name, type} = parseChannel(`/chat${channel}`);
const serverChannel = 'r' === type
? channel
: `/u/${[name, req.user.redditUsername].sort().join('$')}`;
const messageKeys = await keys(redisClient, `${serverChannel}:messages:*`);
const messages = 0 === messageKeys.length const messages = 0 === messageKeys.length
? [] ? []
: (await mget(messageKeys)) : (await mget(messageKeys))
@ -104,6 +108,7 @@ export const appChatState = async (req) => {
if (0 === toHydrate.length) { if (0 === toHydrate.length) {
return null; return null;
} }
console.log({toHydrate})
const entries = await Promise.all( const entries = await Promise.all(
toHydrate.map((favorite) => channelState(req, joinChannel(favorite))), toHydrate.map((favorite) => channelState(req, joinChannel(favorite))),
); );

View File

@ -187,16 +187,27 @@ export function createSocketServer(httpServer) {
} }
if (packet instanceof Message) { if (packet instanceof Message) {
const {channel, message} = packet.data; const {channel, message} = packet.data;
const {name, type} = parseChannel(`/chat${channel}`);
const other = await User.findOne({where: {redditUsername: name}});
const owner = '/r/anonymous' === channel ? 0 : req.userId; const owner = '/r/anonymous' === channel ? 0 : req.userId;
const serverChannel = 'r' === type
? channel
: `/u/${[name, req.user.redditUsername].sort().join('$')}`;
const timestamp = Date.now(); const timestamp = Date.now();
const username = '/r/anonymous' === channel ? 'anonymous' : req.user.redditUsername;
const uuid = uuidv4(); const uuid = uuidv4();
const key = `${channel}:messages:${uuid}`; const key = `${serverChannel}:messages:${uuid}`;
socket.to(channel, new Message({ ('u' === type ? [`/user/${other.id}`, `/user/${req.userId}`] : [channel]).forEach((room) => (
...packet.data, socket.to(room, new Message({
owner, ...packet.data,
timestamp, channel: 'r' === type
uuid, ? channel
})); : `/u/${username === room.substr(3) ? name : username}`,
owner,
timestamp,
uuid,
}))
));
pubClient pubClient
.multi() .multi()
.set(key, JSON.stringify({ .set(key, JSON.stringify({