From 90cb33c017b184a0ce5b8076aaa1193dfba72eb2 Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 28 Jul 2023 01:08:06 +0300 Subject: [PATCH] make group-pingspamming marginally faster (it will still timeout anyway bc of the transaction timeout i added in previous commit) --- files/helpers/actions.py | 7 ++----- files/helpers/alerts.py | 15 +++++++-------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/files/helpers/actions.py b/files/helpers/actions.py index 8499001a2..c5815fca3 100644 --- a/files/helpers/actions.py +++ b/files/helpers/actions.py @@ -134,14 +134,11 @@ def execute_snappy(post:Post, v:User): group_members = group.member_ids - if group.name == 'biofoids': mul = 20 - else: mul = 10 - for user in g.db.query(User).filter(User.id.in_(group_members)).all(): - user.pay_account('coins', mul) + user.pay_account('coins', 10) g.db.add(user) - cost = len(group_members) * mul + cost = len(group_members) * 10 snappy.charge_account('coins', cost) body = f'!{group.name}' diff --git a/files/helpers/alerts.py b/files/helpers/alerts.py index ea7e3df7e..2e0e8895a 100644 --- a/files/helpers/alerts.py +++ b/files/helpers/alerts.py @@ -145,6 +145,7 @@ def NOTIFY_USERS(text, v, oldtext=None, ghost=False, log_cost=None): if FEATURES['PING_GROUPS']: cost = 0 + total_members = set() for i in group_mention_regex.finditer(text): if oldtext and i.group(1) in oldtext: @@ -174,21 +175,19 @@ def NOTIFY_USERS(text, v, oldtext=None, ghost=False, log_cost=None): notify_users.update(members) if ghost or v.id not in member_ids: - if group and group.name == 'biofoids': mul = 20 - else: mul = 10 - - cost += len(members) * mul + cost += len(members) * 10 if cost > v.coins: abort(403, f"You need {cost} coins to mention these ping groups!") + total_members.update(members) + if log_cost: log_cost.ping_cost = cost - for user in g.db.query(User).filter(User.id.in_(members)).all(): - user.pay_account('coins', mul) - g.db.add(user) + if total_members: + g.db.query(User).options(load_only(User.coins)).filter(User.id.in_(total_members)).update({ User.coins: User.coins + 10 }) - v.charge_account('coins', cost) + v.charge_account('coins', cost)