Skip to content

Commit 34a380e

Browse files
authored
feat: add chat.message.getMessage api (#227)
* 添加api: getMessage * 更改异常信息、检查消息是否存在
1 parent 8fbc090 commit 34a380e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

server/openapi.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,24 @@
830830
}
831831
}
832832
},
833+
"/chat.message/getMessage": {
834+
"post": {
835+
"requestBody": {
836+
"content": {
837+
"application/json": {
838+
"schema": {
839+
"type": "object",
840+
"properties": {
841+
"messageId": {
842+
"type": "string"
843+
}
844+
}
845+
}
846+
}
847+
}
848+
}
849+
}
850+
},
833851
"/chat.message/deleteMessage": {
834852
"post": {
835853
"requestBody": {

server/services/core/chat/message.service.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class MessageService extends TcService {
5959
messageId: 'string',
6060
},
6161
});
62+
this.registerAction('getMessage', this.getMessage, {
63+
params: {
64+
messageId: 'string',
65+
},
66+
});
6267
this.registerAction('deleteMessage', this.deleteMessage, {
6368
params: {
6469
messageId: 'string',
@@ -332,6 +337,35 @@ class MessageService extends TcService {
332337
return json;
333338
}
334339

340+
/**
341+
* 获取消息
342+
*/
343+
async getMessage(ctx: TcContext<{ messageId: string }>) {
344+
const { messageId } = ctx.params;
345+
const { t, userId } = ctx.meta;
346+
const message = await this.adapter.model.findById(messageId);
347+
if (!message) {
348+
throw new DataNotFoundError(t('该消息未找到'));
349+
}
350+
const converseId = String(message.converseId);
351+
const groupId = message.groupId;
352+
// 鉴权
353+
if (!groupId) {
354+
// 私人会话
355+
const converseInfo = await call(ctx).getConverseInfo(converseId);
356+
if (!converseInfo.members.map((m) => String(m)).includes(userId)) {
357+
throw new NoPermissionError(t('没有当前会话权限'));
358+
}
359+
} else {
360+
// 群组会话
361+
const groupInfo = await call(ctx).getGroupInfo(String(groupId));
362+
if (!groupInfo.members.map((m) => m.userId).includes(userId)) {
363+
throw new NoPermissionError(t('没有当前会话权限'));
364+
}
365+
}
366+
return message;
367+
}
368+
335369
/**
336370
* 删除消息
337371
* 仅支持群组

0 commit comments

Comments
 (0)