|
""" |
|
MIT License |
|
|
|
Copyright (c) 2022 Aʙɪsʜɴᴏɪ |
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
of this software and associated documentation files (the "Software"), to deal |
|
in the Software without restriction, including without limitation the rights |
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
copies of the Software, and to permit persons to whom the Software is |
|
furnished to do so, subject to the following conditions: |
|
|
|
The above copyright notice and this permission notice shall be included in all |
|
copies or substantial portions of the Software. |
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
SOFTWARE. |
|
""" |
|
|
|
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 = user.reason if user else "" |
|
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() |
|
|