From 34a380eff546bff3540d6dcd059ed1f901370905 Mon Sep 17 00:00:00 2001 From: eya46 <61458340+eya46@users.noreply.github.com> Date: Thu, 9 May 2024 21:47:05 +0800 Subject: [PATCH] feat: add chat.message.getMessage api (#227) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 添加api: getMessage * 更改异常信息、检查消息是否存在 --- server/openapi.json | 18 +++++++++++ server/services/core/chat/message.service.ts | 34 ++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/server/openapi.json b/server/openapi.json index 6bac963a9d1..d7893950639 100644 --- a/server/openapi.json +++ b/server/openapi.json @@ -830,6 +830,24 @@ } } }, + "/chat.message/getMessage": { + "post": { + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "messageId": { + "type": "string" + } + } + } + } + } + } + } + }, "/chat.message/deleteMessage": { "post": { "requestBody": { diff --git a/server/services/core/chat/message.service.ts b/server/services/core/chat/message.service.ts index 3dae3c4aed9..48b70f06741 100644 --- a/server/services/core/chat/message.service.ts +++ b/server/services/core/chat/message.service.ts @@ -59,6 +59,11 @@ class MessageService extends TcService { messageId: 'string', }, }); + this.registerAction('getMessage', this.getMessage, { + params: { + messageId: 'string', + }, + }); this.registerAction('deleteMessage', this.deleteMessage, { params: { messageId: 'string', @@ -332,6 +337,35 @@ class MessageService extends TcService { return json; } + /** + * 获取消息 + */ + async getMessage(ctx: TcContext<{ messageId: string }>) { + const { messageId } = ctx.params; + const { t, userId } = ctx.meta; + const message = await this.adapter.model.findById(messageId); + if (!message) { + throw new DataNotFoundError(t('该消息未找到')); + } + const converseId = String(message.converseId); + const groupId = message.groupId; + // 鉴权 + if (!groupId) { + // 私人会话 + const converseInfo = await call(ctx).getConverseInfo(converseId); + if (!converseInfo.members.map((m) => String(m)).includes(userId)) { + throw new NoPermissionError(t('没有当前会话权限')); + } + } else { + // 群组会话 + const groupInfo = await call(ctx).getGroupInfo(String(groupId)); + if (!groupInfo.members.map((m) => m.userId).includes(userId)) { + throw new NoPermissionError(t('没有当前会话权限')); + } + } + return message; + } + /** * 删除消息 * 仅支持群组