From d34f836052ab73f3591528514f39c222c94b270e Mon Sep 17 00:00:00 2001 From: Aevann Date: Mon, 23 Jan 2023 10:42:38 +0200 Subject: [PATCH] make patron rewards automatic in WPD --- files/helpers/cron.py | 31 ++----------------- files/routes/users.py | 70 ++++++++++++++++++++++++++----------------- 2 files changed, 44 insertions(+), 57 deletions(-) diff --git a/files/helpers/cron.py b/files/helpers/cron.py index bb08edb92..f1ba28ca1 100644 --- a/files/helpers/cron.py +++ b/files/helpers/cron.py @@ -42,9 +42,8 @@ def cron(every_5m, every_1h, every_1d, every_1mo): site_stats = stats.stats(SITE_NAME) cache.set(f'{SITE}_stats', site_stats) - if every_1mo: - if KOFI_LINK: _give_monthly_marseybux_task_kofi() - else: _give_monthly_marseybux_task() + if every_1mo and not KOFI_LINK: + _give_monthly_marseybux_task() g.db.commit() g.db.close() @@ -160,29 +159,3 @@ def _give_monthly_marseybux_task(): g.db.add(ma) return True - - -def _give_monthly_marseybux_task_kofi(): - month = datetime.datetime.now() + datetime.timedelta(days=5) - month = month.strftime('%B') - - tx_emails = [x[0] for x in g.db.query(Transaction.email).distinct().all()] - - for u in g.db.query(User).filter(User.patron > 0, User.patron_utc == 0).all(): - g.db.add(u) - - if not (u.is_activated and u.email in tx_emails): - u.patron = 0 - continue - - marseybux_reward = marseybux_li[u.patron] - u.pay_account('marseybux', marseybux_reward) - send_repeatable_notification(u.id, f"@AutoJanny has given you {marseybux_reward} Marseybux for the month of {month}! You can use them to buy awards in the [shop](/shop).") - - ma = ModAction( - kind="monthly", - user_id=AUTOJANNY_ID, - ) - g.db.add(ma) - - return True diff --git a/files/routes/users.py b/files/routes/users.py index 95582ced9..576b4179b 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -1271,6 +1271,42 @@ def bid_list(v:User, bid): ) + +kofi_tiers={ + 5: 1, + 10: 2, + 20: 3, + 50: 4, + 100: 5, + 200: 6 + } + +def claim_rewards(v): + g.db.flush() + transactions = g.db.query(Transaction).filter_by(email=v.email, claimed=None).all() + + highest_tier = 0 + marseybux = 0 + + for transaction in transactions: + tier = kofi_tiers[transaction.amount] + marseybux += marseybux_li[tier] + if transaction.type == 'Subscription' and tier > highest_tier: + highest_tier = tier + transaction.claimed = True + g.db.add(transaction) + + v.pay_account('marseybux', marseybux) + send_repeatable_notification(v.id, f"You have received {marseybux} Marseybux! You can use them to buy awards in the [shop](/shop).") + g.db.add(v) + + if highest_tier > v.patron: + v.patron = highest_tier + for badge in g.db.query(Badge).filter(Badge.user_id == v.id, Badge.badge_id > 20, Badge.badge_id < 28).all(): + g.db.delete(badge) + badge_grant(badge_id=20+highest_tier, user=v) + + @app.post("/kofi") def kofi(): if not KOFI_TOKEN: abort(404) @@ -1297,16 +1333,13 @@ def kofi(): ) g.db.add(transaction) + + user = g.db.query(User).filter_by(email=email, is_activated=True).order_by(User.truescore.desc()).first() + if user: + claim_rewards(user) + return '' -kofi_tiers={ - 5: 1, - 10: 2, - 20: 3, - 50: 4, - 100: 5, - 200: 6 - } @app.post("/settings/kofi") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @@ -1323,26 +1356,7 @@ def settings_kofi(v:User): if not transactions: abort(400, f"{patron} rewards already claimed") - highest_tier = 0 - marseybux = 0 - - for transaction in transactions: - tier = kofi_tiers[transaction.amount] - marseybux += marseybux_li[tier] - if transaction.type == 'Subscription' and tier > highest_tier: - highest_tier = tier - transaction.claimed = True - g.db.add(transaction) - - v.pay_account('marseybux', marseybux) - send_repeatable_notification(v.id, f"You have received {marseybux} Marseybux! You can use them to buy awards in the [shop](/shop).") - g.db.add(v) - - if highest_tier > v.patron: - v.patron = highest_tier - for badge in g.db.query(Badge).filter(Badge.user_id == v.id, Badge.badge_id > 20, Badge.badge_id < 28).all(): - g.db.delete(badge) - badge_grant(badge_id=20+highest_tier, user=v) + claim_rewards(v) return {"message": f"{patron} rewards claimed!"}