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