11import { Context } from "grammy" ;
2- import { UserService } from "../db/user" ;
32import { GroupSettingsService } from "../db/group" ;
43import { ApprovedUserService } from "../db/user/Approved" ;
54import { Permissions } from "./Permissions" ;
65import { initGroupSetting } from "../../decorators/db" ;
6+ import { UserService } from "../db/user" ;
77
88export class Approved {
9- private static userRepo : UserService = new UserService ( ) ;
109 private static approvedUserRepo : ApprovedUserService =
1110 new ApprovedUserService ( ) ;
1211 private static groupSettingsRepo : GroupSettingsService =
1312 new GroupSettingsService ( ) ;
13+ private static userRepo : UserService = new UserService ( ) ;
14+
1415 // Helper method to fetch the user and group settings
1516 private static async getEntities ( ctx : Context ) {
1617 const userId = ctx . message ?. reply_to_message ?. from ?. id ! ;
1718 const chatId = ctx . chat ?. id ! ;
1819
19- const user = await this . userRepo . getByTelegramId ( userId ) ;
2020 const groupSettings = await this . groupSettingsRepo . getByGroupId ( chatId ) ;
2121
22- return { userId, chatId, user, groupSettings } ;
22+ let user = await this . userRepo . getByTelegramId ( userId ) ;
23+ if ( ! user ) {
24+ user = await this . userRepo . create ( {
25+ telegram_id : userId ! ,
26+ username : ctx . message ?. reply_to_message ?. from ?. username ! ,
27+ role : "member" ,
28+ } ) ;
29+ await this . userRepo . save ( user ) ;
30+ }
31+ return { user, groupSettings } ;
2332 }
33+
2434 @initGroupSetting ( )
2535 static async add ( ctx : Context ) {
26- const { userId, user, groupSettings } = await this . getEntities ( ctx ) ;
27-
28- if ( user ) {
36+ const { user, groupSettings } = await this . getEntities ( ctx ) ;
37+ // Check if the user is already approved
38+ const existingApproval = await this . approvedUserRepo . getByUserIdAndGroup (
39+ user . id ,
40+ groupSettings ! . id !
41+ ) ;
42+ if ( existingApproval ) {
2943 return ctx . reply ( "This user is already approved." ) ;
3044 }
45+
3146 // Add user to the approved list
3247 const approvedUser = await this . approvedUserRepo . create ( {
3348 group : groupSettings ! ,
34- user_id : userId ,
35- username : ctx . message ?. reply_to_message ?. from ?. username ! ,
49+ user_id : user . telegram_id ,
50+ username : user . username ! ,
51+ user : user ,
3652 } ) ;
37-
3853 await this . approvedUserRepo . save ( approvedUser ) ;
54+ if ( ! groupSettings ! . approvedUsers ) {
55+ groupSettings ! . approvedUsers = [ ] ;
56+ }
57+ groupSettings ! . approvedUsers . push ( approvedUser ) ;
58+ await this . groupSettingsRepo . save ( groupSettings ! ) ;
59+ user . role = "approved" ;
60+ await this . userRepo . save ( user ) ;
3961 // Grant full permissions
40- await ctx . restrictChatMember ( userId , Permissions . APPROVED_USER ( ) ) ;
62+ await ctx . restrictChatMember ( user . telegram_id , Permissions . APPROVED_USER ( ) ) ;
4163
4264 // Send a confirmation message
4365 await ctx . reply (
@@ -47,20 +69,23 @@ export class Approved {
4769 }
4870 ) ;
4971 }
72+
5073 @initGroupSetting ( )
5174 static async remove ( ctx : Context ) {
52- const { userId, groupSettings } = await this . getEntities ( ctx ) ;
75+ const { user, groupSettings } = await this . getEntities ( ctx ) ;
76+
5377 // Remove user from the approved list
54- const approvedUser = await this . approvedUserRepo . getByIdAndGroup (
55- userId ,
78+ const approvedUser = await this . approvedUserRepo . getByUserIdAndGroup (
79+ user . id ,
5680 groupSettings ! . id !
5781 ) ;
5882 if ( approvedUser ) {
5983 await this . approvedUserRepo . remove ( approvedUser . id ) ;
6084 }
61-
85+ user . role = "member" ;
86+ await this . userRepo . save ( user ) ;
6287 // Restrict permissions
63- await ctx . restrictChatMember ( userId , Permissions . APPROVED_USER ( false ) ) ;
88+ await ctx . restrictChatMember ( user . telegram_id , Permissions . UN_APPROVE ( ) ) ;
6489
6590 // Send a confirmation message
6691 await ctx . reply (
@@ -70,20 +95,19 @@ export class Approved {
7095 }
7196 ) ;
7297 }
98+
7399 @initGroupSetting ( )
74100 static async list ( ctx : Context ) {
75- const { chatId } = await this . getEntities ( ctx ) ;
76- const updatedGroupSettings = await Approved . groupSettingsRepo . getByGroupId (
77- chatId
78- ) ;
101+ const chatId = ctx . chat ?. id ! ;
102+ const groupSettings = await this . groupSettingsRepo . getByGroupId ( chatId ) ;
79103 const approvedUsers = await this . approvedUserRepo . getByGroup (
80- updatedGroupSettings !
104+ groupSettings !
81105 ) ;
106+
82107 if ( ! approvedUsers || approvedUsers . length === 0 ) {
83108 await ctx . reply ( "There are no approved users in this group." ) ;
84109 return ;
85110 }
86-
87111 // Format the list of approved users
88112 const userList = approvedUsers
89113 . map ( ( user ) => {
0 commit comments