-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add plugin which can auto join group after register or createTe…
…mporaryUser
- Loading branch information
1 parent
a90c0dd
commit 1084913
Showing
8 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "*" | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
server/plugins/com.msgbyte.autojoinGroup/services/autojoinGroup.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters