From 56c9f99c6734a5fd1bc0af63a8ab5aa05a3b59ea Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 5 Feb 2023 17:38:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20inbox.append=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=86=85=E9=83=A8?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=A2=9E=E5=8A=A0=E6=94=B6=E4=BB=B6=E7=AE=B1?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/models/chat/inbox.ts | 13 ++++++++ server/services/core/chat/inbox.service.ts | 37 ++++++++++++++++++++++ 2 files changed, 50 insertions(+) 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;