From 235389b394237a26ea505aa7a2ce08e6cb793627 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Thu, 5 Oct 2023 14:33:08 +0800 Subject: [PATCH] feat: add message.searchMessage api --- server/locales/en-US/translation.json | 1 + server/locales/zh-CN/translation.json | 1 + server/services/core/chat/message.service.ts | 39 ++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/server/locales/en-US/translation.json b/server/locales/en-US/translation.json index 03b655ff..26b7d74a 100644 --- a/server/locales/en-US/translation.json +++ b/server/locales/en-US/translation.json @@ -13,6 +13,7 @@ "k313eb9b3": "User does not exist, please check your username", "k3b35c0b0": "No permission to create invitation codes", "k3b4422dc": "This user is not a temporary user", + "k3b44c0a": "Messages cannot be searched if you are not a member of the group", "k3d0d56b": "Invalid group invitation: group id is empty", "k4241451e": "Not member of this group", "k429851b9": "No operation authority", diff --git a/server/locales/zh-CN/translation.json b/server/locales/zh-CN/translation.json index 679dd331..5eb6d4f5 100644 --- a/server/locales/zh-CN/translation.json +++ b/server/locales/zh-CN/translation.json @@ -13,6 +13,7 @@ "k313eb9b3": "用户不存在, 请检查您的用户名", "k3b35c0b0": "没有创建邀请码权限", "k3b4422dc": "该用户不是临时用户", + "k3b44c0a": "不是群组成员无法搜索消息", "k3d0d56b": "群组邀请失效: 群组id为空", "k4241451e": "不是该群组成员", "k429851b9": "没有操作权限", diff --git a/server/services/core/chat/message.service.ts b/server/services/core/chat/message.service.ts index f86c94ca..ca430586 100644 --- a/server/services/core/chat/message.service.ts +++ b/server/services/core/chat/message.service.ts @@ -64,6 +64,13 @@ class MessageService extends TcService { messageId: 'string', }, }); + this.registerAction('searchMessage', this.searchMessage, { + params: { + groupId: { type: 'string', optional: true }, + converseId: 'string', + text: 'string', + }, + }); this.registerAction( 'fetchConverseLastMessages', this.fetchConverseLastMessages, @@ -368,6 +375,38 @@ class MessageService extends TcService { return true; } + /** + * 搜索消息 + */ + async searchMessage( + ctx: TcContext<{ groupId?: string; converseId: string; text: string }> + ) { + const { groupId, converseId, text } = ctx.params; + const userId = ctx.meta.userId; + const t = ctx.meta.t; + + if (groupId) { + const groupInfo = await call(ctx).getGroupInfo(groupId); + if (!groupInfo.members.map((m) => m.userId).includes(userId)) { + throw new Error(t('不是群组成员无法搜索消息')); + } + } + + const messages = this.adapter.model + .find({ + groupId: groupId ?? null, + converseId, + content: { + $regex: text, + }, + }) + .sort({ _id: -1 }) + .limit(10) + .maxTimeMS(5 * 1000); // 超过5s的查询直接放弃 + + return messages; + } + /** * 基于会话id获取会话最后一条消息的id */