refactor: channel inference
This commit is contained in:
parent
ad915391b7
commit
efd6c3d360
|
@ -2,8 +2,17 @@ import createRedisClient from './redis';
|
||||||
|
|
||||||
const redisClient = createRedisClient();
|
const redisClient = createRedisClient();
|
||||||
|
|
||||||
|
export const channelMiddleware = (req, res, next) => {
|
||||||
|
const matches = req.url.match(/^\/chat\/([^/]+)\/([^/]+)/i);
|
||||||
|
if (matches) {
|
||||||
|
const [, type, name] = matches;
|
||||||
|
req.channel = {name, type};
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
};
|
||||||
|
|
||||||
// eslint-disable-next-line import/prefer-default-export
|
// eslint-disable-next-line import/prefer-default-export
|
||||||
export const enterChannel = async (channel) => {
|
export const enterChannel = async (req, channel) => {
|
||||||
const messages = await new Promise((resolve, reject) => {
|
const messages = await new Promise((resolve, reject) => {
|
||||||
redisClient.scan(
|
redisClient.scan(
|
||||||
0,
|
0,
|
||||||
|
@ -31,6 +40,14 @@ export const enterChannel = async (channel) => {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
// const users = await new Promise((resolve, reject) => {
|
||||||
|
// req.adapter.remoteJoin(req.socketId, channel, (error) => {
|
||||||
|
// if (error) {
|
||||||
|
// reject(error);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// });
|
||||||
return {
|
return {
|
||||||
messages,
|
messages,
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,6 +9,7 @@ import httpProxy from 'http-proxy';
|
||||||
import {invokeHookFlat} from 'scwp';
|
import {invokeHookFlat} from 'scwp';
|
||||||
|
|
||||||
import userRoutes from './routes/user';
|
import userRoutes from './routes/user';
|
||||||
|
import {channelMiddleware} from './entry';
|
||||||
import passport from './passport';
|
import passport from './passport';
|
||||||
import session from './session';
|
import session from './session';
|
||||||
|
|
||||||
|
@ -31,6 +32,7 @@ export async function createHttpServer() {
|
||||||
app.use(session());
|
app.use(session());
|
||||||
app.use(passport.initialize());
|
app.use(passport.initialize());
|
||||||
app.use(passport.session());
|
app.use(passport.session());
|
||||||
|
app.use(channelMiddleware);
|
||||||
userRoutes(app);
|
userRoutes(app);
|
||||||
const httpServer = http.createServer(app);
|
const httpServer = http.createServer(app);
|
||||||
httpServer.listen(31344, '0.0.0.0');
|
httpServer.listen(31344, '0.0.0.0');
|
||||||
|
|
|
@ -24,12 +24,7 @@ registerHooks({
|
||||||
hydration: async (req) => {
|
hydration: async (req) => {
|
||||||
const hydration = {};
|
const hydration = {};
|
||||||
const {user} = req;
|
const {user} = req;
|
||||||
let inferred;
|
const inferred = req.channel;
|
||||||
const matches = req.url.match(/^\/chat\/([^/]+)\/([^/]+)/i);
|
|
||||||
if (matches) {
|
|
||||||
const [, type, name] = matches;
|
|
||||||
inferred = {type, name};
|
|
||||||
}
|
|
||||||
if (user) {
|
if (user) {
|
||||||
hydration.user = {
|
hydration.user = {
|
||||||
friends: await user.friends(),
|
friends: await user.friends(),
|
||||||
|
@ -51,7 +46,7 @@ registerHooks({
|
||||||
recent: [],
|
recent: [],
|
||||||
};
|
};
|
||||||
const entries = await Promise.all(
|
const entries = await Promise.all(
|
||||||
favorites.map((favorite) => enterChannel(channelName(favorite))),
|
favorites.map((favorite) => enterChannel(req, channelName(favorite))),
|
||||||
);
|
);
|
||||||
for (let i = 0; i < favorites.length; i++) {
|
for (let i = 0; i < favorites.length; i++) {
|
||||||
const channel = channelName(favorites[i]);
|
const channel = channelName(favorites[i]);
|
||||||
|
|
|
@ -13,10 +13,11 @@ import session from './session';
|
||||||
|
|
||||||
const pubClient = createRedisClient();
|
const pubClient = createRedisClient();
|
||||||
const subClient = createRedisClient();
|
const subClient = createRedisClient();
|
||||||
|
const adapter = redisAdapter({pubClient, subClient});
|
||||||
|
|
||||||
export function createSocketServer(httpServer) {
|
export function createSocketServer(httpServer) {
|
||||||
const socketServer = new SocketServer(httpServer, {
|
const socketServer = new SocketServer(httpServer, {
|
||||||
adapter: redisAdapter({pubClient, subClient}),
|
adapter,
|
||||||
});
|
});
|
||||||
socketServer.io.use(socketSession(session()));
|
socketServer.io.use(socketSession(session()));
|
||||||
socketServer.io.use((socket, next) => {
|
socketServer.io.use((socket, next) => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user