feat: add plugin which can auto join group after register or createTemporaryUser

pull/105/merge
moonrailgun 2 years ago
parent a90c0ddd06
commit 1084913d3a

@ -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",

@ -17,6 +17,7 @@
"k4241451e": "不是该群组成员",
"k429851b9": "没有操作权限",
"k42cdd273": "用户名已存在!",
"k43ef8fbe": "{{nickname}} 通过系统自动加入群组",
"k45c8d1bf": "认领用户不存在",
"k493e44f1": "不能添加自己为好友",
"k4fd701fe": "邮箱不存在",

@ -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 servicenameaction
* @param callbackAction actionservicename
*/
async registryAfterActionHook(
async registerAfterActionHook(
fullActionName: string,
callbackAction: string
) {

@ -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<P = {}, M = {}> = TcPureContext<
* socket.io
*/
socketId?: string;
/**
* afterActionHook
*/
actionResult?: any;
} & M
>;

@ -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": "*"
}
}

@ -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;

@ -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: {

@ -91,6 +91,10 @@ class GroupService extends TcService {
},
visibility: 'public',
});
/**
*
* @deprecated 使 addMember
*/
this.registerAction('joinGroup', this.joinGroup, {
params: {
groupId: 'string',

Loading…
Cancel
Save