From 091e44928432fc5bacd841452e713a2bc7987792 Mon Sep 17 00:00:00 2001 From: Aevann Date: Sat, 3 Feb 2024 00:39:02 +0200 Subject: [PATCH] order unban_utc properly --- files/classes/user.py | 10 +++++----- files/routes/admin.py | 8 ++++---- files/routes/awards.py | 2 +- files/routes/routehelpers.py | 4 ++-- files/routes/users.py | 5 +++-- migrations/20240203-order-unban-utc-properly.sql | 2 ++ 6 files changed, 17 insertions(+), 14 deletions(-) create mode 100644 migrations/20240203-order-unban-utc-properly.sql diff --git a/files/classes/user.py b/files/classes/user.py index 963cd58fc..2cc430dad 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -118,7 +118,7 @@ class User(Base): enemies = deferred(Column(String)) enemies_html = deferred(Column(String)) is_banned = Column(Integer, ForeignKey("users.id")) - unban_utc = Column(Integer, default=0) + unban_utc = Column(Integer) ban_reason = deferred(Column(String)) is_muted = Column(Boolean, default=False, nullable=False) login_nonce = Column(Integer, default=0) @@ -654,7 +654,7 @@ class User(Base): @property @lazy def unban_string(self): - if self.unban_utc == 0: + if not self.unban_utc: return "permanently banned" wait = self.unban_utc - int(time.time()) @@ -1035,7 +1035,7 @@ class User(Base): else: self.unban_utc = int(time.time()) + (days * 86400) else: - self.unban_utc = 0 + self.unban_utc = None self.is_banned = admin.id if admin else AUTOJANNY_ID if reason and len(reason) <= 256: @@ -1046,12 +1046,12 @@ class User(Base): @property @lazy def is_suspended(self): - return (self.is_banned and (self.unban_utc == 0 or self.unban_utc > time.time())) + return (self.is_banned and (not self.unban_utc or self.unban_utc > time.time())) @property @lazy def is_permabanned(self): - return (self.is_banned and self.unban_utc == 0) + return (self.is_banned and not self.unban_utc) @property @lazy diff --git a/files/routes/admin.py b/files/routes/admin.py index 51140e90a..45d27e061 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -254,7 +254,7 @@ def revert_actions(v, username): for user in users: user.shadowbanned = None - user.unban_utc = 0 + user.unban_utc = None user.ban_reason = None if user.is_banned: user.is_banned = None @@ -263,7 +263,7 @@ def revert_actions(v, username): for u in get_alt_graph(user.id): u.shadowbanned = None - u.unban_utc = 0 + u.unban_utc = None u.ban_reason = None if u.is_banned: u.is_banned = None @@ -1193,7 +1193,7 @@ def unban_user(fullname, v): abort(403, "You can't undo a ban award!") user.is_banned = None - user.unban_utc = 0 + user.unban_utc = None if not user.shadowbanned: user.ban_reason = None send_repeatable_notification(user.id, f"@{v.username} (a site admin) has unbanned you!") @@ -1202,7 +1202,7 @@ def unban_user(fullname, v): for x in get_alt_graph(user.id): if x.is_banned: send_repeatable_notification(x.id, f"@{v.username} (a site admin) has unbanned you!") x.is_banned = None - x.unban_utc = 0 + x.unban_utc = None if not x.shadowbanned: x.ban_reason = None g.db.add(x) diff --git a/files/routes/awards.py b/files/routes/awards.py index 48f2f4ed5..5ea0fff99 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -290,7 +290,7 @@ def award_thing(v, thing_type, id): author.unban_utc -= 86400 send_repeatable_notification(author.id, "Your ban duration has been reduced by 1 day!") else: - author.unban_utc = 0 + author.unban_utc = None author.is_banned = None if not author.shadowbanned: author.ban_reason = None diff --git a/files/routes/routehelpers.py b/files/routes/routehelpers.py index 0e4aa6c0d..d84565394 100644 --- a/files/routes/routehelpers.py +++ b/files/routes/routehelpers.py @@ -112,12 +112,12 @@ def check_for_alts(current, include_current_session=False): elif u.is_permabanned and not current.is_permabanned: current.is_banned = u.is_banned current.ban_reason = u.ban_reason - current.unban_utc = 0 + current.unban_utc = None g.db.add(current) elif current.is_permabanned and not u.is_permabanned: u.is_banned = current.is_banned u.ban_reason = current.ban_reason - u.unban_utc = 0 + u.unban_utc = None g.db.add(u) if current.ban_reason == "Under Siege" and u.ban_reason and u.ban_reason != "Under Siege": diff --git a/files/routes/users.py b/files/routes/users.py index 119e5a25f..c09dec6d0 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -8,6 +8,7 @@ import gevent import qrcode from sqlalchemy.orm import aliased, load_only from sqlalchemy.exc import IntegrityError +from sqlalchemy import nullslast from files.classes import * from files.classes.transactions import * @@ -334,7 +335,7 @@ def banned(v): users = g.db.query(User).filter( User.is_banned != None, - or_(User.unban_utc == 0, User.unban_utc > time.time()), + or_(User.unban_utc == None, User.unban_utc > time.time()), ) total = users.count() @@ -349,7 +350,7 @@ def banned(v): key = User.is_banned else: sort = "unban_utc" - key = User.unban_utc.desc() + key = nullslast(User.unban_utc) users = users.order_by(key).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE) diff --git a/migrations/20240203-order-unban-utc-properly.sql b/migrations/20240203-order-unban-utc-properly.sql new file mode 100644 index 000000000..fcb1d9396 --- /dev/null +++ b/migrations/20240203-order-unban-utc-properly.sql @@ -0,0 +1,2 @@ +alter table users alter column unban_utc drop not null; +update users set unban_utc=null where unban_utc=0;