diff --git a/server/locales/en-US/translation.json b/server/locales/en-US/translation.json index e4df8f45..d998e3ff 100644 --- a/server/locales/en-US/translation.json +++ b/server/locales/en-US/translation.json @@ -17,6 +17,7 @@ "k4241451e": "Not member of this group", "k429851b9": "No operation authority", "k42cdd273": "Username already exists!", + "k43ef8fbe": "{{nickname}} automatically join this group through the system settings", "k45c8d1bf": "Claimed user does not exist", "k493e44f1": "Can't add myself as a friend", "k4fd701fe": "Email does not exist", diff --git a/server/locales/zh-CN/translation.json b/server/locales/zh-CN/translation.json index c6f4ad5a..2e0890e4 100644 --- a/server/locales/zh-CN/translation.json +++ b/server/locales/zh-CN/translation.json @@ -17,6 +17,7 @@ "k4241451e": "不是该群组成员", "k429851b9": "没有操作权限", "k42cdd273": "用户名已存在!", + "k43ef8fbe": "{{nickname}} 通过系统自动加入群组", "k45c8d1bf": "认领用户不存在", "k493e44f1": "不能添加自己为好友", "k4fd701fe": "邮箱不存在", diff --git a/server/packages/sdk/src/services/base.ts b/server/packages/sdk/src/services/base.ts index fba28cdc..138e4746 100644 --- a/server/packages/sdk/src/services/base.ts +++ b/server/packages/sdk/src/services/base.ts @@ -179,7 +179,12 @@ export abstract class TcService extends Service { if (Array.isArray(afterHooks) && afterHooks.length > 0) { for (const action of afterHooks) { // 异步调用, 暂时不修改值 - ctx.call(String(action), ctx.params, { meta: ctx.meta }); + ctx.call(String(action), ctx.params, { + meta: { + ...ctx.meta, + actionResult: res, + }, + }); } } } catch (err) { @@ -400,7 +405,7 @@ export abstract class TcService extends Service { * @param fullActionName 完整的带servicename的action名 * @param callbackAction 当前服务的action名,不需要带servicename */ - async registryAfterActionHook( + async registerAfterActionHook( fullActionName: string, callbackAction: string ) { diff --git a/server/packages/sdk/src/services/types.ts b/server/packages/sdk/src/services/types.ts index 7af7b471..eb790434 100644 --- a/server/packages/sdk/src/services/types.ts +++ b/server/packages/sdk/src/services/types.ts @@ -1,6 +1,5 @@ import type { Context } from 'moleculer'; import type { TFunction } from 'i18next'; -import type { UserStruct } from '../structs/user'; import type { GroupStruct } from '../structs/group'; import type { BuiltinEventMap } from '../structs/events'; @@ -47,6 +46,11 @@ export type TcContext

= TcPureContext< * 仅在 socket.io 的请求中会出现 */ socketId?: string; + + /** + * 仅在 afterActionHook 请求中会出现 + */ + actionResult?: any; } & M >; diff --git a/server/plugins/com.msgbyte.autojoinGroup/package.json b/server/plugins/com.msgbyte.autojoinGroup/package.json new file mode 100644 index 00000000..a65e5935 --- /dev/null +++ b/server/plugins/com.msgbyte.autojoinGroup/package.json @@ -0,0 +1,20 @@ +{ + "name": "tailchat-plugin-autojoingroup", + "version": "1.0.0", + "main": "index.js", + "author": "moonrailgun", + "description": "Auto join group after register", + "license": "MIT", + "private": true, + "scripts": { + "build:web": "ministar buildPlugin all", + "build:web:watch": "ministar watchPlugin all" + }, + "devDependencies": { + "@types/react": "18.0.20", + "mini-star": "*" + }, + "dependencies": { + "tailchat-server-sdk": "*" + } +} diff --git a/server/plugins/com.msgbyte.autojoinGroup/services/autojoinGroup.service.ts b/server/plugins/com.msgbyte.autojoinGroup/services/autojoinGroup.service.ts new file mode 100644 index 00000000..576f088b --- /dev/null +++ b/server/plugins/com.msgbyte.autojoinGroup/services/autojoinGroup.service.ts @@ -0,0 +1,71 @@ +import { call, TcContext } from 'tailchat-server-sdk'; +import { TcService } from 'tailchat-server-sdk'; + +/** + * Autojoin Group + * + * Auto join group after register + */ +class AutojoinGroupService extends TcService { + get serviceName() { + return 'plugin:com.msgbyte.autojoinGroup'; + } + + get autojoinGroupIds(): string[] | null { + const ids = process.env.AUTOJOIN_GROUP_ID; + if (!ids) { + return null; + } + + return ids.split(','); + } + + onInit() { + if (!this.autojoinGroupIds) { + return; + } + + this.registerAfterActionHook('user.register', 'autojoinGroup'); + this.registerAfterActionHook('user.createTemporaryUser', 'autojoinGroup'); + + this.registerAction('autojoinGroup', this.autojoinGroup, { + visibility: 'public', + }); + } + + async autojoinGroup(ctx: TcContext) { + const autojoinGroupIds = this.autojoinGroupIds; + if (!autojoinGroupIds) { + return; + } + + console.log(ctx.params, ctx.meta); + + const userId = ctx.meta.actionResult?._id; + const t = ctx.meta.t; + + if (!userId) { + this.logger.fatal('Autojoin Group Failed: cannot found userId from ctx'); + return; + } + + await Promise.all( + autojoinGroupIds.map(async (groupId: string) => { + await ctx.call('group.addMember', { + groupId, + userId, + }); + + const nickname = ctx.meta.actionResult?.nickname; + await call(ctx).addGroupSystemMessage( + String(groupId), + t('{{nickname}} 通过系统自动加入群组', { + nickname, + }) + ); + }) + ); + } +} + +export default AutojoinGroupService; diff --git a/server/plugins/com.msgbyte.welcome/services/welcome.service.ts b/server/plugins/com.msgbyte.welcome/services/welcome.service.ts index 9efc22fb..283280ec 100644 --- a/server/plugins/com.msgbyte.welcome/services/welcome.service.ts +++ b/server/plugins/com.msgbyte.welcome/services/welcome.service.ts @@ -12,7 +12,7 @@ class WelcomeService extends TcService { } onInit() { - this.registryAfterActionHook('group.joinGroup', 'joinGroupCallback'); + this.registerAfterActionHook('group.joinGroup', 'joinGroupCallback'); this.registerAction('joinGroupCallback', this.joinGroupCallback, { params: { diff --git a/server/services/core/group/group.service.ts b/server/services/core/group/group.service.ts index 3c4bc07f..46a8a369 100644 --- a/server/services/core/group/group.service.ts +++ b/server/services/core/group/group.service.ts @@ -91,6 +91,10 @@ class GroupService extends TcService { }, visibility: 'public', }); + /** + * 加入群组 + * @deprecated 请尽量使用 addMember + */ this.registerAction('joinGroup', this.joinGroup, { params: { groupId: 'string',