From 170bc8feb0aa3db0ba69ccdc166cf4f841397858 Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 23 Feb 2024 20:13:55 +0200 Subject: [PATCH] fix repeated "top poster of the day" notifications and console errors that happen when a new poster of the day is selected --- files/helpers/cron.py | 25 +++++++++++++++++++++++++ files/routes/jinja2.py | 28 +--------------------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/files/helpers/cron.py b/files/helpers/cron.py index fd7fca78c..51a14d416 100644 --- a/files/helpers/cron.py +++ b/files/helpers/cron.py @@ -101,6 +101,9 @@ def cron_fn(every_5m, every_1d, every_1mo): _expire_blocks_mutes_exiles() g.db.commit() + _set_top_poster_of_the_day_id() + g.db.commit() + if every_1mo: _give_marseybux_salary() g.db.commit() @@ -376,3 +379,25 @@ def _expire_blocks_mutes_exiles(): for holeblock in holeblocks: send_repeatable_notification(holeblock.user_id, f"Your block of /h/{holeblock.hole} has passed 1 month and expired!") g.db.delete(holeblock) + + +def _set_top_poster_of_the_day_id(): + t = int(time.time()) - 86400 + + db = db_session() + + user = db.query(User, func.sum(Post.upvotes)).join(Post, Post.author_id == User.id).filter( + Post.created_utc > t, + User.admin_level == 0, + ).group_by(User).order_by(func.sum(Post.upvotes).desc()).first() + + if not user: return SNAPPY_ID + + user = user[0] + + t = datetime.datetime.now() + db.flush() + send_notification(user.id, f":marseyjam: You're the Top Poster of the Day for the day of {t.year}-{t.month}-{t.day} :marseyjam:") + badge_grant(badge_id=327, user=user) + + cache.set("top_poster_of_the_day". user.id) diff --git a/files/routes/jinja2.py b/files/routes/jinja2.py index 04d82c2b7..ef8087e9e 100644 --- a/files/routes/jinja2.py +++ b/files/routes/jinja2.py @@ -137,34 +137,8 @@ def bar_position(): def emoji_count(): return g.db.query(Emoji).filter_by(submitter_id=None).count() -@cache.memoize(timeout=86400) -def top_poster_of_the_day_id(): - t = int(time.time()) - 86400 - - db = db_session() - - user = db.query(User, func.sum(Post.upvotes)).join(Post, Post.author_id == User.id).filter( - Post.created_utc > t, - User.admin_level == 0, - ).group_by(User).order_by(func.sum(Post.upvotes).desc()).first() - - if not user: return SNAPPY_ID - - user = user[0] - - t = datetime.datetime.now() - db.flush() - send_notification(user.id, f":marseyjam: You're the Top Poster of the Day for the day of {t.year}-{t.month}-{t.day} :marseyjam:") - badge_grant(badge_id=327, user=user) - uid = user.id - - db.commit() - db.close() - - return uid - def top_poster_of_the_day(): - uid = top_poster_of_the_day_id() + uid = cache.get("top_poster_of_the_day") or SNAPPY_ID user = g.db.query(User).filter_by(id=uid).one() return user