11import { Context } from "grammy" ;
22import { UserService } from "../db/user" ;
33import { GroupSettingsService } from "../db/group" ;
4- import { GroupMembershipService } from "../db/group/Membership" ;
54import { ApprovedUserService } from "../db/user/Approved" ;
65import { Permissions } from "./Permissions" ;
6+ import { initGroupSetting } from "../../decorators/db" ;
77
88export class Approved {
99 private static userRepo : UserService = new UserService ( ) ;
1010 private static approvedUserRepo : ApprovedUserService =
1111 new ApprovedUserService ( ) ;
1212 private static groupSettingsRepo : GroupSettingsService =
1313 new GroupSettingsService ( ) ;
14- private static groupMembershipRepo : GroupMembershipService =
15- new GroupMembershipService ( ) ;
16-
1714 // Helper method to fetch the user and group settings
1815 private static async getEntities ( ctx : Context ) {
1916 const userId = ctx . message ?. reply_to_message ?. from ?. id ! ;
@@ -24,20 +21,16 @@ export class Approved {
2421
2522 return { userId, chatId, user, groupSettings } ;
2623 }
27-
24+ @ initGroupSetting ( )
2825 static async add ( ctx : Context ) {
29- const { userId, chatId , user, groupSettings } = await this . getEntities ( ctx ) ;
26+ const { userId, user, groupSettings } = await this . getEntities ( ctx ) ;
3027
3128 if ( user ) {
3229 return ctx . reply ( "This user is already approved." ) ;
3330 }
34-
35- if ( ! groupSettings ) {
36- return ctx . reply ( "Group settings not found." ) ;
37- }
3831 // Add user to the approved list
3932 const approvedUser = await this . approvedUserRepo . create ( {
40- group : groupSettings ,
33+ group : groupSettings ! ,
4134 user_id : userId ,
4235 username : ctx . message ?. reply_to_message ?. from ?. username ! ,
4336 } ) ;
@@ -54,30 +47,54 @@ export class Approved {
5447 }
5548 ) ;
5649 }
57-
50+ @ initGroupSetting ( )
5851 static async remove ( ctx : Context ) {
59- const { userId, chatId, groupSettings } = await this . getEntities ( ctx ) ;
60-
61- if ( ! groupSettings ) {
62- return ctx . reply ( "Group settings not found." ) ;
63- }
52+ const { userId, groupSettings } = await this . getEntities ( ctx ) ;
53+ // Remove user from the approved list
54+ const approvedUser = await this . approvedUserRepo . getByIdAndGroup (
55+ userId ,
56+ groupSettings ! . id !
57+ ) ;
58+ if ( approvedUser ) {
59+ await this . approvedUserRepo . remove ( approvedUser . id ) ;
60+ }
6461
65- // Remove user from the approved list
66- const approvedUser = await this . approvedUserRepo . getByIdAndGroup ( userId , groupSettings )
62+ // Restrict permissions
63+ await ctx . restrictChatMember ( userId , Permissions . APPROVED_USER ( false ) ) ;
6764
68- if ( approvedUser ) {
69- await this . approvedUserRepo . remove ( approvedUser . id ) ;
65+ // Send a confirmation message
66+ await ctx . reply (
67+ "The user's approval has been removed, and their permissions have been restricted." ,
68+ {
69+ reply_to_message_id : ctx . message ?. message_id ,
7070 }
71+ ) ;
72+ }
73+ @initGroupSetting ( )
74+ static async list ( ctx : Context ) {
75+ const { chatId } = await this . getEntities ( ctx ) ;
76+ const updatedGroupSettings = await Approved . groupSettingsRepo . getByGroupId (
77+ chatId
78+ ) ;
79+ const approvedUsers = await this . approvedUserRepo . getByGroup (
80+ updatedGroupSettings !
81+ ) ;
82+ if ( ! approvedUsers || approvedUsers . length === 0 ) {
83+ await ctx . reply ( "There are no approved users in this group." ) ;
84+ return ;
85+ }
7186
72- // Restrict permissions
73- await ctx . restrictChatMember ( userId , Permissions . APPROVED_USER ( false ) ) ;
87+ // Format the list of approved users
88+ const userList = approvedUsers
89+ . map ( ( user ) => {
90+ const username = user . username ? `@${ user . username } ` : "No username" ;
91+ return `- ${ username } ` ;
92+ } )
93+ . join ( "\n" ) ;
7494
75- // Send a confirmation message
76- await ctx . reply (
77- "The user's approval has been removed, and their permissions have been restricted." ,
78- {
79- reply_to_message_id : ctx . message ?. message_id ,
80- }
81- ) ;
95+ // Send the list to the chat
96+ await ctx . reply ( `Approved users in this group:\n${ userList } ` , {
97+ reply_to_message_id : ctx . message ?. message_id ,
98+ } ) ;
8299 }
83100}
0 commit comments