From 449c0a8806b7345a80c8b34e8bbd6cf14649e08f Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Mon, 6 Oct 2025 02:09:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9B=E5=BB=BA=E7=BE=A4=E7=BB=84,?= =?UTF-8?q?=20impl=20-=20=E8=8E=B7=E5=8F=96=E7=BE=A4=E7=BB=84=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/api/ChatApi.ts | 56 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/server/api/ChatApi.ts b/server/api/ChatApi.ts index 980b57a..30a4530 100644 --- a/server/api/ChatApi.ts +++ b/server/api/ChatApi.ts @@ -7,7 +7,8 @@ import UserChatLinker from "../data/UserChatLinker.ts" import ApiManager from "./ApiManager.ts" import BaseApi from "./BaseApi.ts" import TokenManager from "./TokenManager.ts" -import ChatPrivate from "../data/ChatPrivate.ts"; +import ChatPrivate from "../data/ChatPrivate.ts" +import ChatGroup from "../data/ChatGroup.ts" export default class ChatApi extends BaseApi { override getName(): string { @@ -56,6 +57,18 @@ export default class ChatApi extends BaseApi { } } } + if (chat!.bean.type == 'group') { + return { + code: 200, + msg: "成功", + data: { + id: args.target as string, + type: chat.bean.type, + title: chat.getTitle(), + avatar: chat.getAvatarFileHash() ? "uploaded_files/" + chat.getAvatarFileHash() : undefined + } + } + } return { code: 501, @@ -230,6 +243,47 @@ export default class ChatApi extends BaseApi { } } }) + /** + * 创建群组 + * @param token 令牌 + * @param title 名称 + * @param [id] 群组 ID + */ + this.registerEvent("Chat.createGroup", (args, { deviceId }) => { + if (this.checkArgsMissing(args, ['token', 'title'])) return { + msg: "參數缺失", + code: 400, + } + if (this.checkArgsEmpty(args, ['title'])) return { + msg: "參數不得為空", + code: 400, + } + + const token = TokenManager.decode(args.token as string) + if (!this.checkToken(token, deviceId)) return { + code: 401, + msg: "令牌無效", + } + const user = User.findById(token.author) as User + + const haveId = args.id && (args.id as string) != '' + if (haveId && Chat.findById(args.id as string) != null) return + + const chat = ChatGroup.createGroup(haveId ? undefined : args.id as string) + chat.setTitle(args.title as string) + chat.addMembers([ + user.bean.id, + ]) + user.addContact(chat.bean.id) + + return { + code: 200, + msg: '成功', + data: { + chat_id: chat.bean.id, + } + } + }) /** * 从私聊获取对方的 UserId * @param token 令牌