File size: 1,541 Bytes
c7dfe8b |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import threading
from sqlalchemy import Column, String, UnicodeText
from Database.sql import BASE, SESSION
class BlacklistUsers(BASE):
__tablename__ = "blacklistusers"
user_id = Column(String(14), primary_key=True)
reason = Column(UnicodeText)
def __init__(self, user_id, reason=None):
self.user_id = user_id
self.reason = reason
BlacklistUsers.__table__.create(checkfirst=True)
BLACKLIST_LOCK = threading.RLock()
BLACKLIST_USERS = set()
def blacklist_user(user_id, reason=None):
with BLACKLIST_LOCK:
user = SESSION.query(BlacklistUsers).get(str(user_id))
if not user:
user = BlacklistUsers(str(user_id), reason)
else:
user.reason = reason
SESSION.add(user)
SESSION.commit()
__load_blacklist_userid_list()
def unblacklist_user(user_id):
with BLACKLIST_LOCK:
user = SESSION.query(BlacklistUsers).get(str(user_id))
if user:
SESSION.delete(user)
SESSION.commit()
__load_blacklist_userid_list()
def get_reason(user_id):
user = SESSION.query(BlacklistUsers).get(str(user_id))
rep = ""
if user:
rep = user.reason
SESSION.close()
return rep
def is_user_blacklisted(user_id):
return user_id in BLACKLIST_USERS
def __load_blacklist_userid_list():
global BLACKLIST_USERS
try:
BLACKLIST_USERS = {int(x.user_id) for x in SESSION.query(BlacklistUsers).all()}
finally:
SESSION.close()
__load_blacklist_userid_list()
|