mirror of https://github.com/msgbyte/tailchat
feat: add plugin which can auto join group after register or createTemporaryUser
parent
a90c0ddd06
commit
1084913d3a
@ -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;
|
Loading…
Reference in New Issue