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