Spaces:
Paused
Paused
Captain Ezio
commited on
Commit
·
3772da1
1
Parent(s):
27cfb8d
Update approve_db.py
Browse files
Powers/database/approve_db.py
CHANGED
|
@@ -1,17 +1,11 @@
|
|
| 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 Approve(MongoDB):
|
| 10 |
"""Class for managing Approves in Chats in Bot."""
|
| 11 |
-
|
| 12 |
# Database name to connect to to preform operations
|
| 13 |
db_name = "approve"
|
| 14 |
-
|
| 15 |
def __init__(self, chat_id: int) -> None:
|
| 16 |
super().__init__(self.db_name)
|
| 17 |
self.chat_id = chat_id
|
|
@@ -19,14 +13,7 @@ class Approve(MongoDB):
|
|
| 19 |
|
| 20 |
def check_approve(self, user_id: int):
|
| 21 |
with INSERTION_LOCK:
|
| 22 |
-
|
| 23 |
-
if not self.chat_info["users"]:
|
| 24 |
-
return j
|
| 25 |
-
for i in self.chat_info["users"]:
|
| 26 |
-
if user_id in i:
|
| 27 |
-
j = True
|
| 28 |
-
break
|
| 29 |
-
return j
|
| 30 |
|
| 31 |
def add_approve(self, user_id: int, user_name: str):
|
| 32 |
with INSERTION_LOCK:
|
|
@@ -37,7 +24,6 @@ class Approve(MongoDB):
|
|
| 37 |
{"users": self.chat_info["users"]},
|
| 38 |
)
|
| 39 |
return True
|
| 40 |
-
|
| 41 |
def remove_approve(self, user_id: int):
|
| 42 |
with INSERTION_LOCK:
|
| 43 |
if self.check_approve(user_id):
|
|
@@ -52,30 +38,24 @@ class Approve(MongoDB):
|
|
| 52 |
{"users": self.chat_info["users"]},
|
| 53 |
)
|
| 54 |
return True
|
| 55 |
-
|
| 56 |
def unapprove_all(self):
|
| 57 |
with INSERTION_LOCK:
|
| 58 |
return self.delete_one(
|
| 59 |
{"_id": self.chat_id},
|
| 60 |
)
|
| 61 |
-
|
| 62 |
def clean_approve(self):
|
| 63 |
with INSERTION_LOCK:
|
| 64 |
return self.delete_one(
|
| 65 |
{"_id":self.chat_id}
|
| 66 |
)
|
| 67 |
-
|
| 68 |
def list_approved(self):
|
| 69 |
with INSERTION_LOCK:
|
| 70 |
return self.chat_info["users"]
|
| 71 |
-
|
| 72 |
def count_approved(self):
|
| 73 |
with INSERTION_LOCK:
|
| 74 |
return len(self.chat_info["users"])
|
| 75 |
-
|
| 76 |
def load_from_db(self):
|
| 77 |
return self.find_all()
|
| 78 |
-
|
| 79 |
def __ensure_in_db(self):
|
| 80 |
chat_data = self.find_one({"_id": self.chat_id})
|
| 81 |
if not chat_data:
|
|
@@ -84,28 +64,24 @@ class Approve(MongoDB):
|
|
| 84 |
LOGGER.info(f"Initialized Approve Document for chat {self.chat_id}")
|
| 85 |
return new_data
|
| 86 |
return chat_data
|
| 87 |
-
|
| 88 |
# Migrate if chat id changes!
|
| 89 |
def migrate_chat(self, new_chat_id: int):
|
| 90 |
old_chat_db = self.find_one({"_id": self.chat_id})
|
| 91 |
new_data = old_chat_db.update({"_id": new_chat_id})
|
| 92 |
self.insert_one(new_data)
|
| 93 |
self.delete_one({"_id": self.chat_id})
|
| 94 |
-
|
| 95 |
@staticmethod
|
| 96 |
def count_all_approved():
|
| 97 |
with INSERTION_LOCK:
|
| 98 |
collection = MongoDB(Approve.db_name)
|
| 99 |
all_data = collection.find_all()
|
| 100 |
return sum(len(i["users"]) for i in all_data if len(i["users"]) >= 1)
|
| 101 |
-
|
| 102 |
@staticmethod
|
| 103 |
def count_approved_chats():
|
| 104 |
with INSERTION_LOCK:
|
| 105 |
collection = MongoDB(Approve.db_name)
|
| 106 |
all_data = collection.find_all()
|
| 107 |
return sum(len(i["users"]) >= 1 for i in all_data)
|
| 108 |
-
|
| 109 |
@staticmethod
|
| 110 |
def repair_db(collection):
|
| 111 |
all_data = collection.find_all()
|
|
|
|
| 1 |
from threading import RLock
|
|
|
|
| 2 |
from Powers import LOGGER
|
| 3 |
from Powers.database import MongoDB
|
|
|
|
| 4 |
INSERTION_LOCK = RLock()
|
|
|
|
|
|
|
| 5 |
class Approve(MongoDB):
|
| 6 |
"""Class for managing Approves in Chats in Bot."""
|
|
|
|
| 7 |
# Database name to connect to to preform operations
|
| 8 |
db_name = "approve"
|
|
|
|
| 9 |
def __init__(self, chat_id: int) -> None:
|
| 10 |
super().__init__(self.db_name)
|
| 11 |
self.chat_id = chat_id
|
|
|
|
| 13 |
|
| 14 |
def check_approve(self, user_id: int):
|
| 15 |
with INSERTION_LOCK:
|
| 16 |
+
return bool(user_id in i for i in self.chat_info["users"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def add_approve(self, user_id: int, user_name: str):
|
| 19 |
with INSERTION_LOCK:
|
|
|
|
| 24 |
{"users": self.chat_info["users"]},
|
| 25 |
)
|
| 26 |
return True
|
|
|
|
| 27 |
def remove_approve(self, user_id: int):
|
| 28 |
with INSERTION_LOCK:
|
| 29 |
if self.check_approve(user_id):
|
|
|
|
| 38 |
{"users": self.chat_info["users"]},
|
| 39 |
)
|
| 40 |
return True
|
|
|
|
| 41 |
def unapprove_all(self):
|
| 42 |
with INSERTION_LOCK:
|
| 43 |
return self.delete_one(
|
| 44 |
{"_id": self.chat_id},
|
| 45 |
)
|
|
|
|
| 46 |
def clean_approve(self):
|
| 47 |
with INSERTION_LOCK:
|
| 48 |
return self.delete_one(
|
| 49 |
{"_id":self.chat_id}
|
| 50 |
)
|
|
|
|
| 51 |
def list_approved(self):
|
| 52 |
with INSERTION_LOCK:
|
| 53 |
return self.chat_info["users"]
|
|
|
|
| 54 |
def count_approved(self):
|
| 55 |
with INSERTION_LOCK:
|
| 56 |
return len(self.chat_info["users"])
|
|
|
|
| 57 |
def load_from_db(self):
|
| 58 |
return self.find_all()
|
|
|
|
| 59 |
def __ensure_in_db(self):
|
| 60 |
chat_data = self.find_one({"_id": self.chat_id})
|
| 61 |
if not chat_data:
|
|
|
|
| 64 |
LOGGER.info(f"Initialized Approve Document for chat {self.chat_id}")
|
| 65 |
return new_data
|
| 66 |
return chat_data
|
|
|
|
| 67 |
# Migrate if chat id changes!
|
| 68 |
def migrate_chat(self, new_chat_id: int):
|
| 69 |
old_chat_db = self.find_one({"_id": self.chat_id})
|
| 70 |
new_data = old_chat_db.update({"_id": new_chat_id})
|
| 71 |
self.insert_one(new_data)
|
| 72 |
self.delete_one({"_id": self.chat_id})
|
|
|
|
| 73 |
@staticmethod
|
| 74 |
def count_all_approved():
|
| 75 |
with INSERTION_LOCK:
|
| 76 |
collection = MongoDB(Approve.db_name)
|
| 77 |
all_data = collection.find_all()
|
| 78 |
return sum(len(i["users"]) for i in all_data if len(i["users"]) >= 1)
|
|
|
|
| 79 |
@staticmethod
|
| 80 |
def count_approved_chats():
|
| 81 |
with INSERTION_LOCK:
|
| 82 |
collection = MongoDB(Approve.db_name)
|
| 83 |
all_data = collection.find_all()
|
| 84 |
return sum(len(i["users"]) >= 1 for i in all_data)
|
|
|
|
| 85 |
@staticmethod
|
| 86 |
def repair_db(collection):
|
| 87 |
all_data = collection.find_all()
|