| skipped 3 lines |
4 | 4 | | from ..lib.words import get_random_words |
5 | 5 | | from ..types.account import Service_Key |
6 | 6 | | from ..config import Configuration |
7 | | - | from ..internals.cache.redis import ( |
8 | | - | get_conn, serialize_dict_list, |
9 | | - | deserialize_dict_list, |
10 | | - | KemonoRedisLock) |
| 7 | + | from ..internals.cache.redis import get_conn |
11 | 8 | | |
12 | 9 | | from datetime import datetime, timedelta, timezone |
13 | 10 | | from flask import session, current_app, flash |
| skipped 175 lines |
189 | 186 | | account = deserialize_account(account) |
190 | 187 | | |
191 | 188 | | return account |
192 | | - | |
193 | | - | |
194 | | - | def get_saved_key_import_ids(key_id, reload=False): |
195 | | - | redis = get_conn() |
196 | | - | key = 'saved_key_import_ids:' + str(key_id) |
197 | | - | saved_key_import_ids = redis.get(key) |
198 | | - | if saved_key_import_ids is None or reload: |
199 | | - | lock = KemonoRedisLock(redis, key, expire=60, auto_renewal=True) |
200 | | - | if lock.acquire(blocking=False): |
201 | | - | cursor = get_cursor() |
202 | | - | # TODO: select columns |
203 | | - | query = """ |
204 | | - | SELECT * |
205 | | - | FROM saved_session_key_import_ids |
206 | | - | WHERE key_id = %s |
207 | | - | """ |
208 | | - | cursor.execute(query, (int(key_id),)) |
209 | | - | saved_key_import_ids = cursor.fetchall() |
210 | | - | redis.set(key, serialize_dict_list(saved_key_import_ids), ex=3600) |
211 | | - | lock.release() |
212 | | - | else: |
213 | | - | time.sleep(0.1) |
214 | | - | return get_saved_key_import_ids(key_id, reload=reload) |
215 | | - | else: |
216 | | - | saved_key_import_ids = deserialize_dict_list(saved_key_import_ids) |
217 | | - | |
218 | | - | return saved_key_import_ids |
219 | | - | |
220 | | - | |
221 | | - | def get_saved_keys(account_id: int, reload: bool = False): |
222 | | - | redis = get_conn() |
223 | | - | key = 'saved_keys:' + str(account_id) |
224 | | - | saved_keys = redis.get(key) |
225 | | - | result = None |
226 | | - | if saved_keys is None or reload: |
227 | | - | lock = KemonoRedisLock(redis, key, expire=60, auto_renewal=True) |
228 | | - | if lock.acquire(blocking=False): |
229 | | - | cursor = get_cursor() |
230 | | - | args_dict = dict( |
231 | | - | account_id=str(account_id) |
232 | | - | ) |
233 | | - | query = """ |
234 | | - | SELECT id, service, discord_channel_ids, added, dead |
235 | | - | FROM saved_session_keys_with_hashes |
236 | | - | WHERE contributor_id = %(account_id)s |
237 | | - | ORDER BY |
238 | | - | added DESC |
239 | | - | """ |
240 | | - | cursor.execute(query, args_dict) |
241 | | - | result = cursor.fetchall() |
242 | | - | redis.set(key, serialize_dict_list(result), ex=3600) |
243 | | - | lock.release() |
244 | | - | else: |
245 | | - | time.sleep(0.1) |
246 | | - | return get_saved_keys(account_id, reload=reload) |
247 | | - | else: |
248 | | - | result = deserialize_dict_list(saved_keys) |
249 | | - | saved_keys = [Service_Key.init_from_dict(service_key) for service_key in result] |
250 | | - | return saved_keys |
251 | | - | |
252 | | - | |
253 | | - | def revoke_saved_keys(key_ids: List[int], account_id: int): |
254 | | - | cursor = get_cursor() |
255 | | - | query_args = dict( |
256 | | - | key_ids=key_ids, |
257 | | - | account_id=account_id |
258 | | - | ) |
259 | | - | query1 = """ |
260 | | - | DELETE |
261 | | - | FROM saved_session_key_import_ids skid |
262 | | - | USING saved_session_keys_with_hashes sk |
263 | | - | WHERE |
264 | | - | skid.key_id = sk.id |
265 | | - | AND sk.id = ANY (%(key_ids)s) |
266 | | - | AND sk.contributor_id = %(account_id)s |
267 | | - | """ |
268 | | - | cursor.execute(query1, query_args) |
269 | | - | query2 = """ |
270 | | - | DELETE |
271 | | - | FROM saved_session_keys_with_hashes |
272 | | - | WHERE |
273 | | - | id = ANY (%(key_ids)s) |
274 | | - | AND contributor_id = %(account_id)s |
275 | | - | """ |
276 | | - | cursor.execute(query2, query_args) |
277 | | - | redis = get_conn() |
278 | | - | key = 'saved_keys:' + str(account_id) |
279 | | - | redis.delete(key) |
280 | | - | return True |
281 | 189 | | |
282 | 190 | | |
283 | 191 | | def get_login_info_for_username(username): |
| skipped 132 lines |