Skip to content

Commit

Permalink
feat: add plugin which can auto join group after register or createTe…
Browse files Browse the repository at this point in the history
…mporaryUser
  • Loading branch information
moonrailgun committed Aug 3, 2023
1 parent a90c0dd commit 1084913
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 4 deletions.
1 change: 1 addition & 0 deletions server/locales/en-US/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions server/locales/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"k4241451e": "不是该群组成员",
"k429851b9": "没有操作权限",
"k42cdd273": "用户名已存在!",
"k43ef8fbe": "{{nickname}} 通过系统自动加入群组",
"k45c8d1bf": "认领用户不存在",
"k493e44f1": "不能添加自己为好友",
"k4fd701fe": "邮箱不存在",
Expand Down
9 changes: 7 additions & 2 deletions server/packages/sdk/src/services/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
) {
Expand Down
6 changes: 5 additions & 1 deletion server/packages/sdk/src/services/types.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -47,6 +46,11 @@ export type TcContext<P = {}, M = {}> = TcPureContext<
* 仅在 socket.io 的请求中会出现
*/
socketId?: string;

/**
* 仅在 afterActionHook 请求中会出现
*/
actionResult?: any;
} & M
>;

Expand Down
20 changes: 20 additions & 0 deletions server/plugins/com.msgbyte.autojoinGroup/package.json
Original file line number Diff line number Diff line change
@@ -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": "*"
}
}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
4 changes: 4 additions & 0 deletions server/services/core/group/group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class GroupService extends TcService {
},
visibility: 'public',
});
/**
* 加入群组
* @deprecated 请尽量使用 addMember
*/
this.registerAction('joinGroup', this.joinGroup, {
params: {
groupId: 'string',
Expand Down

0 comments on commit 1084913

Please sign in to comment.