File size: 1,328 Bytes
ca4eb6d
11ae35a
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from threading import RLock

from Powers.database import MongoDB
from Powers.database.chats_db import Chats

INSERTION_LOCK = RLock()
BLACKLIST_CHATS = []


class GroupBlacklist(MongoDB):
    """Class to blacklist chats where bot will exit."""

    db_name = "group_blacklists"

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

    def add_chat(self, chat_id: int):
        with INSERTION_LOCK:
            global BLACKLIST_CHATS
            try:
                Chats.remove_chat(chat_id)  # Delete chat from database
            except KeyError:
                pass
            BLACKLIST_CHATS.append(chat_id)
            BLACKLIST_CHATS.sort()
            return self.insert_one({"_id": chat_id, "blacklist": True})

    def remove_chat(self, chat_id: int):
        with INSERTION_LOCK:
            global BLACKLIST_CHATS
            BLACKLIST_CHATS.remove(chat_id)
            BLACKLIST_CHATS.sort()
            return self.delete_one({"_id": chat_id})

    def list_all_chats(self):
        with INSERTION_LOCK:
            try:
                BLACKLIST_CHATS.sort()
                return BLACKLIST_CHATS
            except Exception:
                all_chats = self.find_all()
                return [chat["_id"] for chat in all_chats]

    def get_from_db(self):
        return self.find_all()