From e400ab0113a470eb167281bf7543b0d556c2519a Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 16 Jul 2020 23:27:35 -0500 Subject: [PATCH] refactor: channelUsers --- src/server/entry.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/server/entry.js b/src/server/entry.js index 17fd383..c075079 100644 --- a/src/server/entry.js +++ b/src/server/entry.js @@ -7,6 +7,15 @@ import createRedisClient, {keys} from './redis'; const redisClient = createRedisClient(); const mget = promisify(redisClient.mget.bind(redisClient)); + +export const channelUsers = async (channel) => { + const socketKeys = await keys(redisClient, `${channel}:users:*`); + return 0 === socketKeys.length + ? [] + : Object.keys((await mget(socketKeys)).reduce((r, k) => ({[k]: true, ...r}), {})) + .map((idStrings) => parseInt(idStrings, 10)); +}; + const channelState = async (req, channel) => { const messageKeys = await keys(redisClient, `${channel}:messages:*`); const messages = 0 === messageKeys.length @@ -17,14 +26,9 @@ const channelState = async (req, channel) => { uuid: messageKeys[i].split(':')[2], })) .sort((l, r) => l.timestamp - r.timestamp); - const socketKeys = await keys(redisClient, `${channel}:users:*`); - const users = 0 === socketKeys.length - ? [] - : Object.keys((await mget(socketKeys)).reduce((r, k) => ({[k]: true, ...r}), {})) - .map((idStrings) => parseInt(idStrings, 10)); return { messages, - users, + users: channelUsers(channel), }; };