mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: move message notification to home handler
This commit is contained in:
5 files changed
+39
-54
No files matched your search
@@ -214,7 +214,6 @@ export class Handler<C = CordisContext> extends HandlerCommon<C> {
|
|||||||
static [kHandler] = 'Handler';
|
static [kHandler] = 'Handler';
|
||||||
|
|
||||||
loginMethods: any;
|
loginMethods: any;
|
||||||
noCheckPermView = false;
|
|
||||||
notUsage = false;
|
notUsage = false;
|
||||||
allowCors = false;
|
allowCors = false;
|
||||||
__param: Record<string, decorators.ParamOption<any>[]>;
|
__param: Record<string, decorators.ParamOption<any>[]>;
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import { Context } from '../context';
|
import { Context } from '../context';
|
||||||
import { ForbiddenError } from '../error';
|
import { ForbiddenError } from '../error';
|
||||||
import avatar from '../lib/avatar';
|
|
||||||
import { PRIV } from '../model/builtin';
|
|
||||||
import * as SystemModel from '../model/system';
|
import * as SystemModel from '../model/system';
|
||||||
import TokenModel from '../model/token';
|
import TokenModel from '../model/token';
|
||||||
import UserModel from '../model/user';
|
import UserModel from '../model/user';
|
||||||
@@ -43,7 +41,6 @@ class WebsocketEventsConnectionManagerHandler extends ConnectionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async message(payload: any) {
|
async message(payload: any) {
|
||||||
console.log(payload);
|
|
||||||
try {
|
try {
|
||||||
if (['resume', 'subscribe'].includes(payload.operation)) await this.subscribe(payload);
|
if (['resume', 'subscribe'].includes(payload.operation)) await this.subscribe(payload);
|
||||||
if (payload.operation === 'unsubscribe') await this.unsubscribe(payload);
|
if (payload.operation === 'unsubscribe') await this.unsubscribe(payload);
|
||||||
@@ -98,6 +95,7 @@ class WebsocketEventsConnectionManagerHandler extends ConnectionHandler {
|
|||||||
operation: 'verify',
|
operation: 'verify',
|
||||||
accept,
|
accept,
|
||||||
reject,
|
reject,
|
||||||
|
request_id: payload.request_id,
|
||||||
subscription_id: payload.subscription_id,
|
subscription_id: payload.subscription_id,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -112,38 +110,4 @@ class WebsocketEventsConnectionManagerHandler extends ConnectionHandler {
|
|||||||
|
|
||||||
export function apply(ctx: Context) {
|
export function apply(ctx: Context) {
|
||||||
ctx.Connection('websocket_gateway', '/websocket', WebsocketEventsConnectionManagerHandler);
|
ctx.Connection('websocket_gateway', '/websocket', WebsocketEventsConnectionManagerHandler);
|
||||||
|
|
||||||
async function notifyMessage(uid: number[], mdoc: any, h) {
|
|
||||||
const udoc = (await UserModel.getById('system', mdoc.from))!;
|
|
||||||
return {
|
|
||||||
operation: 'event',
|
|
||||||
channels: uid.map((u) => `message:${u}`),
|
|
||||||
payload: { udoc: { ...udoc.serialize(h) as any, avatarUrl: avatar(udoc.avatar, 128) }, mdoc },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.on('subscription/init', (h, privileged) => {
|
|
||||||
if (!privileged) return;
|
|
||||||
h.ctx.on('user/message', async (uid, mdoc) => {
|
|
||||||
h.send(await notifyMessage(uid, mdoc, h));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.on('subscription/enable', (channel, h, privileged) => {
|
|
||||||
if (!channel.startsWith('message:') || privileged) return;
|
|
||||||
const uid = +channel.split(':')[1];
|
|
||||||
h.ctx.on('user/message', async (uids, mdoc) => {
|
|
||||||
if (!uids.includes(uid)) return;
|
|
||||||
h.send(await notifyMessage([uid], mdoc, h));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.on('subscription/subscribe', (channel, user) => { // eslint-disable-line consistent-return
|
|
||||||
if (channel === 'message' && user.hasPriv(PRIV.PRIV_USER_PROFILE)) {
|
|
||||||
return {
|
|
||||||
ok: true,
|
|
||||||
channel: `message:${user._id}`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,7 @@ import {
|
|||||||
NotFoundError, PermissionError, UserAlreadyExistError,
|
NotFoundError, PermissionError, UserAlreadyExistError,
|
||||||
UserNotFoundError, ValidationError, VerifyPasswordError,
|
UserNotFoundError, ValidationError, VerifyPasswordError,
|
||||||
} from '../error';
|
} from '../error';
|
||||||
import { DomainDoc, MessageDoc, Setting } from '../interface';
|
import { DomainDoc, Setting } from '../interface';
|
||||||
import avatar, { validate } from '../lib/avatar';
|
import avatar, { validate } from '../lib/avatar';
|
||||||
import * as mail from '../lib/mail';
|
import * as mail from '../lib/mail';
|
||||||
import { verifyTFA } from '../lib/verifyTFA';
|
import { verifyTFA } from '../lib/verifyTFA';
|
||||||
@@ -29,7 +29,7 @@ import token from '../model/token';
|
|||||||
import * as training from '../model/training';
|
import * as training from '../model/training';
|
||||||
import user from '../model/user';
|
import user from '../model/user';
|
||||||
import {
|
import {
|
||||||
ConnectionHandler, Handler, param, query, requireSudo, subscribe, Types,
|
Handler, param, query, requireSudo, Types,
|
||||||
} from '../service/server';
|
} from '../service/server';
|
||||||
import { camelCase, md5 } from '../utils';
|
import { camelCase, md5 } from '../utils';
|
||||||
|
|
||||||
@@ -608,18 +608,6 @@ class HomeMessagesHandler extends Handler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HomeMessagesConnectionHandler extends ConnectionHandler {
|
|
||||||
category = '#message';
|
|
||||||
|
|
||||||
@subscribe('user/message')
|
|
||||||
async onMessageReceived(uid: number, mdoc: MessageDoc) {
|
|
||||||
if (uid !== this.user._id) return;
|
|
||||||
const udoc = (await user.getById(this.args.domainId, mdoc.from))!;
|
|
||||||
udoc.avatarUrl = avatar(udoc.avatar, 64);
|
|
||||||
this.send({ udoc, mdoc });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const inject = { geoip: { required: false }, oauth: {} };
|
export const inject = { geoip: { required: false }, oauth: {} };
|
||||||
export function apply(ctx: Context) {
|
export function apply(ctx: Context) {
|
||||||
ctx.Route('homepage', '/', HomeHandler);
|
ctx.Route('homepage', '/', HomeHandler);
|
||||||
@@ -630,5 +618,38 @@ export function apply(ctx: Context) {
|
|||||||
ctx.Route('home_domain', '/home/domain', HomeDomainHandler, PRIV.PRIV_USER_PROFILE);
|
ctx.Route('home_domain', '/home/domain', HomeDomainHandler, PRIV.PRIV_USER_PROFILE);
|
||||||
ctx.Route('home_domain_create', '/home/domain/create', HomeDomainCreateHandler, PRIV.PRIV_CREATE_DOMAIN);
|
ctx.Route('home_domain_create', '/home/domain/create', HomeDomainCreateHandler, PRIV.PRIV_CREATE_DOMAIN);
|
||||||
ctx.Route('home_messages', '/home/messages', HomeMessagesHandler, PRIV.PRIV_USER_PROFILE);
|
ctx.Route('home_messages', '/home/messages', HomeMessagesHandler, PRIV.PRIV_USER_PROFILE);
|
||||||
ctx.Connection('home_messages_conn', '/home/messages-conn', HomeMessagesConnectionHandler, PRIV.PRIV_USER_PROFILE);
|
|
||||||
|
async function notifyMessage(uid: number[], mdoc: any, h) {
|
||||||
|
const udoc = (await user.getById('system', mdoc.from))!;
|
||||||
|
return {
|
||||||
|
operation: 'event',
|
||||||
|
channels: uid.map((u) => `message:${u}`),
|
||||||
|
payload: { udoc: { ...udoc.serialize(h) as any, avatarUrl: avatar(udoc.avatar, 128) }, mdoc },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.on('subscription/init', (h, privileged) => {
|
||||||
|
if (!privileged) return;
|
||||||
|
h.ctx.on('user/message', async (uid, mdoc) => {
|
||||||
|
h.send(await notifyMessage(uid, mdoc, h));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.on('subscription/enable', (channel, h, privileged) => {
|
||||||
|
if (!channel.startsWith('message:') || privileged) return;
|
||||||
|
const uid = +channel.split(':')[1];
|
||||||
|
h.ctx.on('user/message', async (uids, mdoc) => {
|
||||||
|
if (!uids.includes(uid)) return;
|
||||||
|
h.send(await notifyMessage([uid], mdoc, h));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.on('subscription/subscribe', (channel, udoc) => { // eslint-disable-line consistent-return
|
||||||
|
if (channel === 'message' && udoc.hasPriv(PRIV.PRIV_USER_PROFILE)) {
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
channel: `message:${udoc._id}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ export { default as moment, isMoment } from 'moment-timezone';
|
|||||||
|
|
||||||
export {
|
export {
|
||||||
WebService, Router, HandlerCommon, httpServer,
|
WebService, Router, HandlerCommon, httpServer,
|
||||||
|
Query, Mutation, Subscription, Apis,
|
||||||
} from '@hydrooj/framework';
|
} from '@hydrooj/framework';
|
||||||
|
|
||||||
export * from './pipelineUtils';
|
export * from './pipelineUtils';
|
||||||
|
|||||||
@@ -271,7 +271,7 @@ export async function apply(ctx: Context) {
|
|||||||
text: v.text,
|
text: v.text,
|
||||||
name: v.name,
|
name: v.name,
|
||||||
}));
|
}));
|
||||||
if (!h.noCheckPermView && !h.user.hasPriv(PRIV.PRIV_VIEW_ALL_DOMAIN)) h.checkPerm(PERM.PERM_VIEW);
|
if ((!('noCheckPermView' in h) || !h.noCheckPermView) && !h.user.hasPriv(PRIV.PRIV_VIEW_ALL_DOMAIN)) h.checkPerm(PERM.PERM_VIEW);
|
||||||
if (h.context.pendingError) throw h.context.pendingError;
|
if (h.context.pendingError) throw h.context.pendingError;
|
||||||
});
|
});
|
||||||
on('handler/create/ws', async (h) => {
|
on('handler/create/ws', async (h) => {
|
||||||
|
|||||||
Reference in new issue
Block a user