diff --git a/server/models/chat/inbox.ts b/server/models/chat/inbox.ts index f9a083ba..e5356cf9 100644 --- a/server/models/chat/inbox.ts +++ b/server/models/chat/inbox.ts @@ -4,6 +4,8 @@ import { DocumentType, Ref, index, + modelOptions, + Severity, } from '@typegoose/typegoose'; import { Base, TimeStamps } from '@typegoose/typegoose/lib/defaultClasses'; import type { Types } from 'mongoose'; @@ -38,6 +40,11 @@ class InboxMessage { /** * 收件箱管理 */ +@modelOptions({ + options: { + allowMixed: Severity.ALLOW, + }, +}) @index({ userId: 1 }) export class Inbox extends TimeStamps implements Base { _id: Types.ObjectId; @@ -60,6 +67,12 @@ export class Inbox extends TimeStamps implements Base { }) message?: InboxMessage; + /** + * 信息体,没有类型 + */ + @prop() + payload?: object; + /** * 是否已读 */ diff --git a/server/services/core/chat/inbox.service.ts b/server/services/core/chat/inbox.service.ts index 07a7e6f3..7d88b130 100644 --- a/server/services/core/chat/inbox.service.ts +++ b/server/services/core/chat/inbox.service.ts @@ -56,6 +56,14 @@ class InboxService extends TcService { } ); + this.registerAction('append', this.append, { + visibility: 'public', + params: { + userId: { type: 'string', optional: true }, + type: 'string', + payload: 'any', + }, + }); this.registerAction('appendMessage', this.appendMessage, { visibility: 'public', params: { @@ -84,6 +92,35 @@ class InboxService extends TcService { this.registerAction('clear', this.clear); } + /** + * 通用的增加inbox的接口 + * 用于内部,插件化的形式 + */ + async append( + ctx: TcContext<{ + userId?: string; + type: string; + payload: any; + }> + ) { + const { userId = ctx.meta.userId, type, payload } = ctx.params; + + const doc = await this.adapter.model.create({ + userId, + type, + payload, + }); + + const inboxItem = await this.transformDocuments(ctx, {}, doc); + + await this.notifyUsersInboxAppend(ctx, [userId], inboxItem); + + return true; + } + + /** + * 增加收件夹消息 + */ async appendMessage( ctx: TcContext<{ userId?: string;