fix: intercom
This commit is contained in:
parent
cd65fdbe2b
commit
04d97aa6c0
|
@ -1,30 +1,40 @@
|
|||
import {promisify} from 'util';
|
||||
|
||||
import D from 'debug';
|
||||
import session from 'express-session';
|
||||
import redisAdapter from 'socket.io-redis';
|
||||
|
||||
import {createClient} from './index';
|
||||
|
||||
const debug = D('@latus/redis/session');
|
||||
|
||||
// eslint-disable-next-line import/newline-after-import
|
||||
const RedisStore = require('connect-redis')(session);
|
||||
|
||||
const adapterIntoReq = (path, adapter, req) => {
|
||||
const customRequest = promisify(adapter.customRequest.bind(adapter));
|
||||
req.intercom = (type, payload) => {
|
||||
debug('%s intercom: %s(%o)', path, type, payload);
|
||||
return customRequest({type, payload});
|
||||
};
|
||||
const clients = promisify(adapter.clients.bind(adapter));
|
||||
req.clients = (rooms) => {
|
||||
debug('%s clients: %o', path, rooms);
|
||||
return clients(rooms);
|
||||
};
|
||||
};
|
||||
|
||||
export default {
|
||||
hooks: {
|
||||
'@latus/http/request': (latus) => (req, res, next) => {
|
||||
const adapter = latus.config['%redisAdapter'];
|
||||
const customRequest = promisify(adapter.customRequest.bind(req.adapter));
|
||||
req.intercom = (type, payload) => customRequest(type, payload);
|
||||
req.clients = promisify(adapter.clients.bind(adapter));
|
||||
'@latus/http/request': ({config: {'%redisAdapter': adapter}}) => (req, res, next) => {
|
||||
adapterIntoReq('http', adapter, req);
|
||||
next();
|
||||
},
|
||||
'@latus/user/session': (latus) => ({
|
||||
store: new RedisStore({client: createClient(latus)}),
|
||||
}),
|
||||
'@latus/socket/connect': ({req}) => {
|
||||
const {adapter} = req;
|
||||
const customRequest = promisify(adapter.customRequest.bind(req.adapter));
|
||||
req.intercom = (type, payload) => customRequest(type, payload);
|
||||
req.clients = promisify(adapter.clients.bind(adapter));
|
||||
'@latus/socket/connect': ({req}, {config: {'%redisAdapter': adapter}}) => {
|
||||
adapterIntoReq('sockets', adapter, req);
|
||||
},
|
||||
'@latus/socket/server': (latus) => {
|
||||
const pubClient = createClient(latus);
|
||||
|
@ -38,11 +48,11 @@ export default {
|
|||
// eslint-disable-next-line no-param-reassign
|
||||
latus.config['%redisAdapter'] = adapter;
|
||||
const hooks = latus.invokeReduce('@latus/redis/intercom');
|
||||
adapter.customHook = async (req, fn) => {
|
||||
const {type} = req;
|
||||
adapter.customHook = async ({payload, type}, fn) => {
|
||||
debug('customHook: %s(%o)', type, payload);
|
||||
if (hooks[type]) {
|
||||
try {
|
||||
fn(await hooks[type](req));
|
||||
fn(await hooks[type](payload));
|
||||
}
|
||||
catch (error) {
|
||||
fn(error);
|
||||
|
|
|
@ -39,8 +39,7 @@ export default {
|
|||
},
|
||||
],
|
||||
'@latus/redis/intercom': (latus, sockets) => ({
|
||||
'@reddichat/user/users': (req) => {
|
||||
const sids = req.payload;
|
||||
'@reddichat/user/users': (sids) => {
|
||||
const {connected} = sockets.io.of('/');
|
||||
const here = sids.filter((sid) => !!connected[sid]);
|
||||
return here.reduce((r, sid) => ({...r, [sid]: connected[sid].handshake.userId}), {});
|
||||
|
|
Loading…
Reference in New Issue
Block a user