chore:d ebug

This commit is contained in:
cha0s 2020-12-10 03:49:15 -06:00
parent 46621882b2
commit 3963519718

View File

@ -1,27 +1,42 @@
/* eslint-disable import/no-extraneous-dependencies */
import {promisify} from 'util';
import D from 'debug';
import {ModelMap} from '@latus/db';
import {createClient, keys} from '@latus/redis';
import {channelIsAnonymous, parseChannel, renderChannel} from '@reddichat/core';
const debug = D('@reddichat/chat/state');
export const channelUserCounts = async (req, channel) => {
const ids = await req.clients([renderChannel(channel)]);
const rooms = [renderChannel(channel)];
debug('channelUserCounts: rooms(%O)', rooms);
const ids = await req.clients(rooms);
debug('channelUserCounts: ids(%O)', ids);
if (0 === ids.length) {
return [];
return {};
}
const replies = channelIsAnonymous(channel)
? [ids.reduce((r, socketKey) => ({...r, [socketKey]: 0}), {})]
: await req.intercom('@reddichat/user/users', ids);
debug('channelUserCounts: replies(%O)', replies);
const uids = replies.reduce((r, m) => ({...r, ...m}), {});
return Object.values(uids).reduce((r, uid) => ({...r, [uid]: 1 + (r[uid] || 0)}), {});
debug('channelUserCounts: uids(%O)', uids);
const reduced = Object.values(uids).reduce((r, uid) => ({...r, [uid]: 1 + (r[uid] || 0)}), {});
debug('channelUserCounts: reduced(%O)', reduced);
return reduced;
};
export const channelUsers = async (req, channel) => (
Object.keys(await channelUserCounts(req, channel)).map((id) => parseInt(id, 10))
);
export const channelUsers = async (req, channel) => {
const userCounts = await channelUserCounts(req, channel);
debug('channelUsers: userCounts(%O)', userCounts);
const parsed = Object.keys(userCounts).map((id) => parseInt(id, 10));
debug('channelUsers: parsed(%O)', parsed);
return parsed;
};
export const channelState = async (req, latus, channel) => {
debug('channelState');
const {name, type} = channel;
const redisClient = createClient(latus);
const mget = promisify(redisClient.mget.bind(redisClient));
@ -47,6 +62,7 @@ export const channelState = async (req, latus, channel) => {
};
export const channelsToHydrate = async (req, latus) => {
debug('channelsToHydrate');
const {channel, user} = req;
if (!user) {
return channel ? [channel] : [];
@ -67,6 +83,7 @@ export const channelsToHydrate = async (req, latus) => {
};
export const chatUserIds = async (req, latus) => {
debug('chatUserIds');
const toHydrate = await channelsToHydrate(req, latus);
if (0 === toHydrate.length) {
return [];
@ -84,7 +101,9 @@ export const chatUserIds = async (req, latus) => {
};
export default async (req, latus) => {
debug('default state: url(%s), headers(%O)', req.url, req.headers);
const toHydrate = await channelsToHydrate(req, latus);
debug('default state: toHydrate(%O)', toHydrate);
const chat = {
channels: {},
input: {},
@ -96,6 +115,7 @@ export default async (req, latus) => {
const entries = await Promise.all(
toHydrate.map((favorite) => channelState(req, latus, favorite)),
);
debug('default state: entries(%O)', entries);
for (let i = 0; i < toHydrate.length; i++) {
const channel = renderChannel(toHydrate[i]);
const {messages, users} = entries[i];
@ -107,5 +127,6 @@ export default async (req, latus) => {
chat.messages[message.uuid] = message;
});
}
debug('default state(%O)', chat);
return chat;
};