1+ from threading import RLock
2+
3+ from Powers import LOGGER
4+ from Powers .database import MongoDB
5+
6+ INSERTION_LOCK = RLock ()
7+
8+
9+ class CAPTCHA (MongoDB ):
10+ """Class to store captcha's info"""
11+ db_name = "captcha"
12+
13+ def __init__ (self ) -> None :
14+ super ().__init__ (self .db_name )
15+
16+ def insert_captcha (self , chat , captcha_type :str = "qr" , captcha_action :str = "mute" ):
17+ with INSERTION_LOCK :
18+ curr = self .is_captcha (chat )
19+ if not curr :
20+ self .insert_one (
21+ {
22+ "chat_id" :chat ,
23+ "captcha_type" :captcha_type ,
24+ "captcha_action" :captcha_action
25+ }
26+ )
27+ return
28+
29+ def is_captcha (self , chat ):
30+ curr = self .find_one ({"chat_id" : chat })
31+ if curr :
32+ return True
33+ return False
34+
35+ def update_type (self , chat , captcha_type ):
36+ with INSERTION_LOCK :
37+ curr = self .is_captcha (chat )
38+ if curr :
39+ self .update ({"chat_id" :chat },{"captcha_type" :captcha_type })
40+ return
41+
42+ def update_action (self , chat , captcha_action ):
43+ with INSERTION_LOCK :
44+ curr = self .is_captcha (chat )
45+ if curr :
46+ self .update ({"chat_id" :chat },{"captcha_action" :captcha_action })
47+ return
48+
49+ def remove_captcha (self , chat ):
50+ with INSERTION_LOCK :
51+ curr = self .is_captcha (chat )
52+ if curr :
53+ self .delete_one ({"chat_id" :chat })
54+ return
55+
56+ def get_captcha (self , chat ):
57+ curr = self .find_one ({"chat_id" :chat })
58+ if curr :
59+ return curr
60+ return False
61+
62+ class CAPTCHA_DATA (MongoDB ):
63+ """class to store captcha data"""
64+ db_name = "captcha_data"
65+
66+ def __init__ (self ) -> None :
67+ super ().__init__ (self .db_name )
68+
69+ def load_cap_data (self , chat , user , data ):
70+ curr = self .find_one ({"chat_id" :chat ,"user_id" :user })
71+ if not curr :
72+ with INSERTION_LOCK :
73+ self .insert_one ({"chat_id" :chat ,"user_id" :user ,"data" :data })
74+ return True
75+ else :
76+ return
77+
78+ def get_cap_data (self , chat , user ):
79+ curr = self .find_one ({"chat_id" :chat ,"user_id" :user })
80+ if curr :
81+ return curr ["data" ]
82+ else :
83+ return False
84+
85+ def remove_cap_data (self , chat , user ):
86+ curr = self .find_one ({"chat_id" :chat ,"user_id" :user })
87+ if curr :
88+ with INSERTION_LOCK :
89+ self .delete_one ({"chat_id" :chat ,"user_id" :user })
90+ return
91+
92+ def store_message_id (self , chat , user , message ):
93+ curr = self .find_one ({"chat_id" :chat ,"user_id" :user })
94+ if not curr :
95+ with INSERTION_LOCK :
96+ self .insert_one ({"chat_id" :chat ,"user_id" :user ,"message_id" :message })
97+ return
98+ else :
99+ return
100+
101+ def is_already_data (self , chat , user ):
102+ curr = self .find_one ({"chat_id" :chat ,"user_id" :user })
103+ if curr :
104+ return curr ["message_id" ]
105+ else :
106+ return False
107+
108+ def del_message_id (self , chat , user ):
109+ curr = self .find_one ({"chat_id" :chat ,"user_id" :user })
110+ if curr :
111+ with INSERTION_LOCK :
112+ self .delete_one ({"chat_id" :chat ,"user_id" :user })
113+ return
0 commit comments