Karma commited on
Commit
4d00e9a
·
1 Parent(s): cff0667

Create blsticker_sql.py

Browse files
Files changed (1) hide show
  1. Database/sql/blsticker_sql.py +200 -0
Database/sql/blsticker_sql.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import threading
2
+
3
+ from sqlalchemy import Column, Integer, String, UnicodeText, distinct, func
4
+
5
+ from Database.sql import BASE, SESSION
6
+
7
+
8
+ class StickersFilters(BASE):
9
+ __tablename__ = "blacklist_stickers"
10
+ chat_id = Column(String(14), primary_key=True)
11
+ trigger = Column(UnicodeText, primary_key=True, nullable=False)
12
+
13
+ def __init__(self, chat_id, trigger):
14
+ self.chat_id = str(chat_id) # ensure string
15
+ self.trigger = trigger
16
+
17
+ def __repr__(self):
18
+ return "<Stickers filter '%s' for %s>" % (self.trigger, self.chat_id)
19
+
20
+ def __eq__(self, other):
21
+ return bool(
22
+ isinstance(other, StickersFilters)
23
+ and self.chat_id == other.chat_id
24
+ and self.trigger == other.trigger,
25
+ )
26
+
27
+
28
+ class StickerSettings(BASE):
29
+ __tablename__ = "blsticker_settings"
30
+ chat_id = Column(String(14), primary_key=True)
31
+ blacklist_type = Column(Integer, default=1)
32
+ value = Column(UnicodeText, default="0")
33
+
34
+ def __init__(self, chat_id, blacklist_type=1, value="0"):
35
+ self.chat_id = str(chat_id)
36
+ self.blacklist_type = blacklist_type
37
+ self.value = value
38
+
39
+ def __repr__(self):
40
+ return "<{} will executing {} for blacklist trigger.>".format(
41
+ self.chat_id,
42
+ self.blacklist_type,
43
+ )
44
+
45
+
46
+ StickersFilters.__table__.create(checkfirst=True)
47
+ StickerSettings.__table__.create(checkfirst=True)
48
+
49
+ STICKERS_FILTER_INSERTION_LOCK = threading.RLock()
50
+ STICKSET_FILTER_INSERTION_LOCK = threading.RLock()
51
+
52
+ CHAT_STICKERS = {}
53
+ CHAT_BLSTICK_BLACKLISTS = {}
54
+
55
+
56
+ def add_to_stickers(chat_id, trigger):
57
+ with STICKERS_FILTER_INSERTION_LOCK:
58
+ stickers_filt = StickersFilters(str(chat_id), trigger)
59
+
60
+ SESSION.merge(stickers_filt) # merge to avoid duplicate key issues
61
+ SESSION.commit()
62
+ global CHAT_STICKERS
63
+ if CHAT_STICKERS.get(str(chat_id), set()) == set():
64
+ CHAT_STICKERS[str(chat_id)] = {trigger}
65
+ else:
66
+ CHAT_STICKERS.get(str(chat_id), set()).add(trigger)
67
+
68
+
69
+ def rm_from_stickers(chat_id, trigger):
70
+ with STICKERS_FILTER_INSERTION_LOCK:
71
+ stickers_filt = SESSION.query(StickersFilters).get((str(chat_id), trigger))
72
+ if stickers_filt:
73
+ if trigger in CHAT_STICKERS.get(str(chat_id), set()): # sanity check
74
+ CHAT_STICKERS.get(str(chat_id), set()).remove(trigger)
75
+
76
+ SESSION.delete(stickers_filt)
77
+ SESSION.commit()
78
+ return True
79
+
80
+ SESSION.close()
81
+ return False
82
+
83
+
84
+ def get_chat_stickers(chat_id):
85
+ return CHAT_STICKERS.get(str(chat_id), set())
86
+
87
+
88
+ def num_stickers_filters():
89
+ try:
90
+ return SESSION.query(StickersFilters).count()
91
+ finally:
92
+ SESSION.close()
93
+
94
+
95
+ def num_stickers_chat_filters(chat_id):
96
+ try:
97
+ return (
98
+ SESSION.query(StickersFilters.chat_id)
99
+ .filter(StickersFilters.chat_id == str(chat_id))
100
+ .count()
101
+ )
102
+ finally:
103
+ SESSION.close()
104
+
105
+
106
+ def num_stickers_filter_chats():
107
+ try:
108
+ return SESSION.query(func.count(distinct(StickersFilters.chat_id))).scalar()
109
+ finally:
110
+ SESSION.close()
111
+
112
+
113
+ def set_blacklist_strength(chat_id, blacklist_type, value):
114
+ # for blacklist_type
115
+ # 0 = nothing
116
+ # 1 = delete
117
+ # 2 = warn
118
+ # 3 = mute
119
+ # 4 = kick
120
+ # 5 = ban
121
+ # 6 = tban
122
+ # 7 = tmute
123
+ with STICKSET_FILTER_INSERTION_LOCK:
124
+ global CHAT_BLSTICK_BLACKLISTS
125
+ curr_setting = SESSION.query(StickerSettings).get(str(chat_id))
126
+ if not curr_setting:
127
+ curr_setting = StickerSettings(
128
+ chat_id,
129
+ blacklist_type=int(blacklist_type),
130
+ value=value,
131
+ )
132
+
133
+ curr_setting.blacklist_type = int(blacklist_type)
134
+ curr_setting.value = str(value)
135
+ CHAT_BLSTICK_BLACKLISTS[str(chat_id)] = {
136
+ "blacklist_type": int(blacklist_type),
137
+ "value": value,
138
+ }
139
+
140
+ SESSION.add(curr_setting)
141
+ SESSION.commit()
142
+
143
+
144
+ def get_blacklist_setting(chat_id):
145
+ try:
146
+ setting = CHAT_BLSTICK_BLACKLISTS.get(str(chat_id))
147
+ if setting:
148
+ return setting["blacklist_type"], setting["value"]
149
+ else:
150
+ return 1, "0"
151
+
152
+ finally:
153
+ SESSION.close()
154
+
155
+
156
+ def __load_CHAT_STICKERS():
157
+ global CHAT_STICKERS
158
+ try:
159
+ chats = SESSION.query(StickersFilters.chat_id).distinct().all()
160
+ for (chat_id,) in chats: # remove tuple by ( ,)
161
+ CHAT_STICKERS[chat_id] = []
162
+
163
+ all_filters = SESSION.query(StickersFilters).all()
164
+ for x in all_filters:
165
+ CHAT_STICKERS[x.chat_id] += [x.trigger]
166
+
167
+ CHAT_STICKERS = {x: set(y) for x, y in CHAT_STICKERS.items()}
168
+
169
+ finally:
170
+ SESSION.close()
171
+
172
+
173
+ def __load_chat_stickerset_blacklists():
174
+ global CHAT_BLSTICK_BLACKLISTS
175
+ try:
176
+ chats_settings = SESSION.query(StickerSettings).all()
177
+ for x in chats_settings: # remove tuple by ( ,)
178
+ CHAT_BLSTICK_BLACKLISTS[x.chat_id] = {
179
+ "blacklist_type": x.blacklist_type,
180
+ "value": x.value,
181
+ }
182
+
183
+ finally:
184
+ SESSION.close()
185
+
186
+
187
+ def migrate_chat(old_chat_id, new_chat_id):
188
+ with STICKERS_FILTER_INSERTION_LOCK:
189
+ chat_filters = (
190
+ SESSION.query(StickersFilters)
191
+ .filter(StickersFilters.chat_id == str(old_chat_id))
192
+ .all()
193
+ )
194
+ for filt in chat_filters:
195
+ filt.chat_id = str(new_chat_id)
196
+ SESSION.commit()
197
+
198
+
199
+ __load_CHAT_STICKERS()
200
+ __load_chat_stickerset_blacklists()