feat: intercom

This commit is contained in:
cha0s 2020-12-09 04:00:09 -06:00
parent f485d27bb7
commit 1cdeb124a2
5 changed files with 97 additions and 29 deletions

View File

@ -25,7 +25,7 @@
"express-session": "^1.17.1",
"mkdirp": "^1.0.4",
"redis": "^3.0.2",
"socket.io-redis": "^6.0.1"
"socket.io-redis": "5.3.0"
},
"devDependencies": {
"@neutrinojs/airbnb-base": "^9.4.0",

View File

@ -1,3 +1,5 @@
import {promisify} from 'util';
import session from 'express-session';
import redisAdapter from 'socket.io-redis';
@ -8,6 +10,14 @@ const RedisStore = require('connect-redis')(session);
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);
const clients = promisify(adapter.clients.bind(adapter));
req.clients = clients;
next();
},
'@latus/user/session': (latus) => ({
store: new RedisStore({client: createClient(latus)}),
}),
@ -18,5 +28,22 @@ export default {
adapter: redisAdapter({pubClient, subClient}),
};
},
'@latus/socket/server.io': (io, latus) => {
const {adapter} = io.of('/');
// 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;
if (hooks[type]) {
try {
fn(await hooks[type](req));
}
catch (error) {
fn(error);
}
}
};
},
},
};

View File

@ -943,9 +943,9 @@
integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==
"@types/node@*":
version "14.14.10"
resolved "https://npm.i12e.cha0s.io/@types%2fnode/-/node-14.14.10.tgz#5958a82e41863cfc71f2307b3748e3491ba03785"
integrity sha512-J32dgx2hw8vXrSbu4ZlVhn1Nm3GbeCFNw2FWL8S5QKucHGY0cyNwjdQdO+KMBZ4wpmC7KhLCiNsdk1RFRIYUQQ==
version "14.14.11"
resolved "https://npm.i12e.cha0s.io/@types%2fnode/-/node-14.14.11.tgz#fc25a4248a5e8d0837019b1d170146d07334abe0"
integrity sha512-BJ97wAUuU3NUiUCp44xzUFquQEvnk1wu7q4CMEUYKJWjdkr0YWYDsm4RFtAvxYsNjLsKcrFt6RvK8r+mnzMbEQ==
"@types/source-list-map@*":
version "0.1.2"
@ -2161,6 +2161,11 @@ domain-browser@^1.1.1:
resolved "https://npm.i12e.cha0s.io/domain-browser/-/domain-browser-1.2.0.tgz#3d31f50191a6749dd1375a7f522e823d42e54eda"
integrity sha512-jnjyiM6eRyZl2H+W8Q/zLMA481hzi0eszAaBUzIVnmYVDBbnLxVNnfu1HgEBvCbL+71FrxMl3E6lpKH7Ge3OXA==
double-ended-queue@^2.1.0-0:
version "2.1.0-0"
resolved "https://npm.i12e.cha0s.io/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz#103d3527fd31528f40188130c841efdd78264e5c"
integrity sha1-ED01J/0xUo9AGIEwyEHv3XgmTlw=
duplexify@^3.4.2, duplexify@^3.6.0:
version "3.7.1"
resolved "https://npm.i12e.cha0s.io/duplexify/-/duplexify-3.7.1.tgz#2a4df5317f6ccfd91f86d6fd25d8d8a103b88309"
@ -2172,9 +2177,9 @@ duplexify@^3.4.2, duplexify@^3.6.0:
stream-shift "^1.0.0"
electron-to-chromium@^1.3.612:
version "1.3.616"
resolved "https://npm.i12e.cha0s.io/electron-to-chromium/-/electron-to-chromium-1.3.616.tgz#de63d1c79bb8eb61168774df0c11c9e1af69f9e8"
integrity sha512-CI8L38UN2BEnqXw3/oRIQTmde0LiSeqWSRlPA42ZTYgJQ8fYenzAM2Z3ni+jtILTcrs5aiXZCGJ96Pm+3/yGyQ==
version "1.3.620"
resolved "https://npm.i12e.cha0s.io/electron-to-chromium/-/electron-to-chromium-1.3.620.tgz#c6f36a7e398acc9d7d12743a6f58d536fbc58700"
integrity sha512-YbgWXUR2Mu+Fp6rm3GZ5YJdNo8SgZKLUTNSl2PNvdOcM8OIz07jRJnRkIaV9vdszFv9UUuGChh19w9qSuoLJgw==
elliptic@^6.5.3:
version "6.5.3"
@ -3078,9 +3083,9 @@ inherits@2.0.3:
integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=
ini@^1.3.4, ini@^1.3.5:
version "1.3.5"
resolved "https://npm.i12e.cha0s.io/ini/-/ini-1.3.5.tgz#eee25f56db1c9ec6085e0c22778083f596abf927"
integrity sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==
version "1.3.6"
resolved "https://npm.i12e.cha0s.io/ini/-/ini-1.3.6.tgz#f1c46a2a93a253e7b3905115e74d527cd23061a1"
integrity sha512-IZUoxEjNjubzrmvzZU4lKP7OnYmX72XRl3sqkfJhBKweKi5rnGi5+IUdlj/H1M+Ip5JQ1WzaDMOBRY90Ajc5jg==
interpret@^1.2.0, interpret@^1.4.0:
version "1.4.0"
@ -3319,7 +3324,7 @@ js-tokens@^4.0.0:
resolved "https://npm.i12e.cha0s.io/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
js-yaml@3.14.0, js-yaml@^3.13.1:
js-yaml@3.14.0:
version "3.14.0"
resolved "https://npm.i12e.cha0s.io/js-yaml/-/js-yaml-3.14.0.tgz#a7a34170f26a21bb162424d8adacb4113a69e482"
integrity sha512-/4IbIeHcD9VMHFqDR/gQ7EdZdLimOvW2DdcxFjdyyZ9NsbS+ccrXqVWDtab/lRl5AlUqmpBx8EhPaWR+OtY17A==
@ -3327,6 +3332,14 @@ js-yaml@3.14.0, js-yaml@^3.13.1:
argparse "^1.0.7"
esprima "^4.0.0"
js-yaml@^3.13.1:
version "3.14.1"
resolved "https://npm.i12e.cha0s.io/js-yaml/-/js-yaml-3.14.1.tgz#dae812fdb3825fa306609a8717383c50c36a0537"
integrity sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==
dependencies:
argparse "^1.0.7"
esprima "^4.0.0"
jsesc@^2.5.1:
version "2.5.2"
resolved "https://npm.i12e.cha0s.io/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"
@ -3694,11 +3707,16 @@ ms@2.0.0:
resolved "https://npm.i12e.cha0s.io/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=
ms@2.1.2, ms@^2.1.1:
ms@2.1.2:
version "2.1.2"
resolved "https://npm.i12e.cha0s.io/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
ms@^2.1.1:
version "2.1.3"
resolved "https://npm.i12e.cha0s.io/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
nan@^2.12.1:
version "2.14.2"
resolved "https://npm.i12e.cha0s.io/nan/-/nan-2.14.2.tgz#f5376400695168f4cc694ac9393d0c9585eeea19"
@ -3812,10 +3830,10 @@ normalize-path@^3.0.0, normalize-path@~3.0.0:
resolved "https://npm.i12e.cha0s.io/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==
notepack.io@~2.2.0:
version "2.2.0"
resolved "https://npm.i12e.cha0s.io/notepack.io/-/notepack.io-2.2.0.tgz#d7ea71d1cb90094f88c6f3c8d84277c2d0cd101c"
integrity sha512-9b5w3t5VSH6ZPosoYnyDONnUTF8o0UkBw7JLA6eBlYJWyGT1Q3vQa8Hmuj1/X6RYvHjjygBDgw6fJhe0JEojfw==
notepack.io@~2.1.2:
version "2.1.3"
resolved "https://npm.i12e.cha0s.io/notepack.io/-/notepack.io-2.1.3.tgz#cc904045c751b1a27b2dcfd838d81d0bf3ced923"
integrity sha512-AgSt+cP5XMooho1Ppn8NB3FFaVWefV+qZoZncYTUSch2GAEwlYLcIIbT5YVkMlFeNHnfwOvc4HDlbvrB5BRxXA==
object-assign@^4.0.1, object-assign@^4.1.1:
version "4.1.1"
@ -4351,7 +4369,7 @@ readdirp@~3.5.0:
dependencies:
picomatch "^2.2.1"
redis-commands@^1.5.0:
redis-commands@^1.2.0, redis-commands@^1.5.0:
version "1.6.0"
resolved "https://npm.i12e.cha0s.io/redis-commands/-/redis-commands-1.6.0.tgz#36d4ca42ae9ed29815cdb30ad9f97982eba1ce23"
integrity sha512-2jnZ0IkjZxvguITjFTrGiLyzQZcTvaw8DAaCXxZq/dsHXz7KfMQ3OUJy7Tz9vnRtZRVz6VRCPDvruvU8Ts44wQ==
@ -4361,6 +4379,11 @@ redis-errors@^1.0.0, redis-errors@^1.2.0:
resolved "https://npm.i12e.cha0s.io/redis-errors/-/redis-errors-1.2.0.tgz#eb62d2adb15e4eaf4610c04afe1529384250abad"
integrity sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=
redis-parser@^2.6.0:
version "2.6.0"
resolved "https://npm.i12e.cha0s.io/redis-parser/-/redis-parser-2.6.0.tgz#52ed09dacac108f1a631c07e9b69941e7a19504b"
integrity sha1-Uu0J2srBCPGmMcB+m2mUHnoZUEs=
redis-parser@^3.0.0:
version "3.0.0"
resolved "https://npm.i12e.cha0s.io/redis-parser/-/redis-parser-3.0.0.tgz#b66d828cdcafe6b4b8a428a7def4c6bcac31c8b4"
@ -4368,7 +4391,7 @@ redis-parser@^3.0.0:
dependencies:
redis-errors "^1.0.0"
redis@^3.0.0, redis@^3.0.2:
redis@^3.0.2:
version "3.0.2"
resolved "https://npm.i12e.cha0s.io/redis/-/redis-3.0.2.tgz#bd47067b8a4a3e6a2e556e57f71cc82c7360150a"
integrity sha512-PNhLCrjU6vKVuMOyFu7oSP296mwBkcE6lrAjruBYG5LgdSqtRBoVQIylrMyVZD/lkF24RSNNatzvYag6HRBHjQ==
@ -4378,6 +4401,15 @@ redis@^3.0.0, redis@^3.0.2:
redis-errors "^1.2.0"
redis-parser "^3.0.0"
redis@~2.8.0:
version "2.8.0"
resolved "https://npm.i12e.cha0s.io/redis/-/redis-2.8.0.tgz#202288e3f58c49f6079d97af7a10e1303ae14b02"
integrity sha512-M1OkonEQwtRmZv4tEWF2VgpG0JWJ8Fv1PhlgT5+B+uNq2cA3Rt1Yt/ryoR+vQNOQcIEgdCdfH0jr3bDpihAw1A==
dependencies:
double-ended-queue "^2.1.0-0"
redis-commands "^1.2.0"
redis-parser "^2.6.0"
regenerate-unicode-properties@^8.2.0:
version "8.2.0"
resolved "https://npm.i12e.cha0s.io/regenerate-unicode-properties/-/regenerate-unicode-properties-8.2.0.tgz#e5de7111d655e7ba60c057dbe9ff37c87e65cdec"
@ -4713,20 +4745,20 @@ snapdragon@^0.8.1:
source-map-resolve "^0.5.0"
use "^3.1.0"
socket.io-adapter@~2.0.0:
version "2.0.3"
resolved "https://npm.i12e.cha0s.io/socket.io-adapter/-/socket.io-adapter-2.0.3.tgz#372b7cde7a535fc4f4f0d5ac7f73952a3062d438"
integrity sha512-2wo4EXgxOGSFueqvHAdnmi5JLZzWqMArjuP4nqC26AtLh5PoCPsaRbRdah2xhcwTAMooZfjYiNVNkkmmSMaxOQ==
socket.io-adapter@~1.1.0:
version "1.1.2"
resolved "https://npm.i12e.cha0s.io/socket.io-adapter/-/socket.io-adapter-1.1.2.tgz#ab3f0d6f66b8fc7fca3959ab5991f82221789be9"
integrity sha512-WzZRUj1kUjrTIrUKpZLEzFZ1OLj5FwLlAFQs9kuZJzJi5DKdU7FsWc36SNmA8iDOtwBQyT8FkrriRM8vXLYz8g==
socket.io-redis@^6.0.1:
version "6.0.1"
resolved "https://npm.i12e.cha0s.io/socket.io-redis/-/socket.io-redis-6.0.1.tgz#0d6c82bd6e0dcbb0d70dcbc57f0c3269e6e53594"
integrity sha512-RvxAhVSsDQJfDUEXUER9MvsE99XZurXkAVORjym1FTReqWlvmPVjyAnrpLlH3RxvPFdFa9sN4kmaTtyzjOtRRA==
socket.io-redis@5.3.0:
version "5.3.0"
resolved "https://npm.i12e.cha0s.io/socket.io-redis/-/socket.io-redis-5.3.0.tgz#d01716d2813c25b76918cd704e1cf540b2f3985a"
integrity sha512-w2EqyGdw3oXzd1MY4sVIg2rYVooDI5sSwel8DOt38sTgaJuuXQSC847x38FvLSn2Rt6MAcdLhiNw/FqjzeC4RQ==
dependencies:
debug "~4.1.0"
notepack.io "~2.2.0"
redis "^3.0.0"
socket.io-adapter "~2.0.0"
notepack.io "~2.1.2"
redis "~2.8.0"
socket.io-adapter "~1.1.0"
uid2 "0.0.3"
source-list-map@^2.0.0:

View File

@ -22,6 +22,7 @@ export default class SocketServer {
serveClient: false,
...latus.invokeReduce('@latus/socket/server'),
});
latus.invoke('@latus/socket/server.io', this.io);
this.io.on('connect', this.onConnect);
this.io.use((socket, next) => {
latus.invokeMiddleware('@latus/socket/authenticate', socket, next);

View File

@ -38,6 +38,14 @@ export default {
},
},
],
'@latus/redis/intercom': (latus, sockets) => ({
'@reddichat/user/users': (req) => {
const sids = req.payload;
const {connected} = sockets.io.of('/');
const here = sids.filter((sid) => !!connected[sid]);
return here.reduce((r, sid) => ({...r, [sid]: connected[sid].handshake.userId}), {});
},
}),
'@latus/socket/authenticate': () => (socket, next) => {
debug('@latus/socket/authenticate: passport.initialize()');
passport.initialize()(socket.handshake, undefined, () => {