File size: 1,271 Bytes
fc19cc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from threading import RLock
from time import time

from Powers import LOGGER
from Powers.database import MongoDB

INSERTION_LOCK = RLock()


class AUTOJOIN(MongoDB):
    """class to store auto join requests"""

    db_name = "autojoin"

    def __init__(self) -> None:
        super().__init__(self.db_name)

    def load_autojoin(self, chat,mode="auto"):
        """
        type = auto or notify
        auto to auto accept join requests
        notify to notify the admins about the join requests
        """
        curr = self.find_one({"chat_id":chat,})
        if not curr:
            with INSERTION_LOCK:
                self.insert_one({"chat_id":chat,"type":mode})
            return True
        return False

    def get_autojoin(self,chat):
        curr = self.find_one({"chat_id":chat})
        if not curr:
            return False
        else:
            return curr["type"]

    def update_join_type(self,chat,mode):
        curr = self.find_one({"chat_id":chat})
        if curr:
            self.update({"chat_id":chat},{"type":mode})
            return 
        else:
            return

    def remove_autojoin(self,chat):
        curr = self.find_one({"chat_id":chat})
        if curr:
            self.delete_one({"chat_id":chat})
        return