Karma commited on
Commit
4cc6d64
·
1 Parent(s): a3fc3d5

Create blacklistdb.py

Browse files
Files changed (1) hide show
  1. Database/mongodb/blacklistdb.py +51 -0
Database/mongodb/blacklistdb.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import codecs
2
+ import pickle
3
+ from typing import List
4
+
5
+ from Database.mongodb.db import dbname
6
+
7
+ blacklist_filtersdb = dbname.blacklistFilters
8
+
9
+
10
+ def obj_to_str(obj):
11
+ if not obj:
12
+ return False
13
+ string = codecs.encode(pickle.dumps(obj), "base64").decode()
14
+ return string
15
+
16
+
17
+ def str_to_obj(string: str):
18
+ obj = pickle.loads(codecs.decode(string.encode(), "base64"))
19
+ return obj
20
+
21
+
22
+ async def get_blacklisted_words(chat_id: int) -> List[str]:
23
+ _filters = await blacklist_filtersdb.find_one({"chat_id": chat_id})
24
+ if not _filters:
25
+ return []
26
+ return _filters["filters"]
27
+
28
+
29
+ async def save_blacklist_filter(chat_id: int, word: str):
30
+ word = word.lower().strip()
31
+ _filters = await get_blacklisted_words(chat_id)
32
+ _filters.append(word)
33
+ await blacklist_filtersdb.update_one(
34
+ {"chat_id": chat_id},
35
+ {"$set": {"filters": _filters}},
36
+ upsert=True,
37
+ )
38
+
39
+
40
+ async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
41
+ filtersd = await get_blacklisted_words(chat_id)
42
+ word = word.lower().strip()
43
+ if word in filtersd:
44
+ filtersd.remove(word)
45
+ await blacklist_filtersdb.update_one(
46
+ {"chat_id": chat_id},
47
+ {"$set": {"filters": filtersd}},
48
+ upsert=True,
49
+ )
50
+ return True
51
+ return False