refactor: channelUsers

This commit is contained in:
cha0s 2020-07-16 23:27:35 -05:00
parent 3a86b1dd0e
commit e400ab0113

View File

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