diff --git a/files/helpers/actions.py b/files/helpers/actions.py index d22b823ff5..624f6a3566 100644 --- a/files/helpers/actions.py +++ b/files/helpers/actions.py @@ -1,27 +1,17 @@ -from flask import g, abort -from files.classes.user import User -from files.classes.badges import Badge, BadgeDef +from flask import g +from files.classes.badges import Badge +from files.helpers.alerts import send_repeatable_notification -# TODO: More sanity checks on passed parameters. -# TODO: Add `replace=False` parameter which, when set true, removes any -# existing badge with identical id & user and replaces with new one. -def badge_grant(user_id, badge_id, desc='', url='', commit=True): - user = g.db.query(User).filter(User.id == int(user_id)).one_or_none() - if not user: - return None - elif user.has_badge(badge_id): - return None +def badge_grant(user, badge_id): + + if user.has_badge(badge_id): return badge = Badge( badge_id=int(badge_id), - user_id=user.id, - description=desc if desc != '' else None, - url=url if url != '' else None, + user_id=user.id ) g.db.add(badge) - if commit: - g.db.commit() - else: - g.db.flush() - return badge + g.db.flush() + + send_repeatable_notification(user.id, f"@AutoJanny has given you the following profile badge:\n\n![]({badge.path})\n\n{badge.name}") diff --git a/files/helpers/lottery.py b/files/helpers/lottery.py index ab1e2a18c4..ac503062f1 100644 --- a/files/helpers/lottery.py +++ b/files/helpers/lottery.py @@ -3,7 +3,7 @@ from random import choice from sqlalchemy import * from files.helpers.alerts import * from files.helpers.wrappers import * -from files.helpers.actions import badge_grant +from files.helpers.actions import * from flask import g from .const import * @@ -43,7 +43,7 @@ def end_lottery_session(): winning_user = next(filter(lambda x: x.id == winner, participating_users)) winning_user.coins += active_lottery.prize winning_user.total_lottery_winnings += active_lottery.prize - badge_grant(winner, LOTTERY_WINNER_BADGE_ID) + badge_grant(user=winning_user, badge_id=LOTTERY_WINNER_BADGE_ID) for user in participating_users: chance_to_win = user.currently_held_lottery_tickets / len(raffle) * 100 diff --git a/files/mail/__init__.py b/files/mail/__init__.py index 3648375527..391372ada1 100644 --- a/files/mail/__init__.py +++ b/files/mail/__init__.py @@ -7,6 +7,7 @@ from files.helpers.security import * from files.helpers.wrappers import * from files.helpers.const import * from files.helpers.get import * +from files.helpers.actions import * from files.classes import * from files.__main__ import app, mail, limiter from flask_mail import Message @@ -80,12 +81,7 @@ def activate(v): user.email = email user.is_activated = True - if not any(b.badge_id == 2 for b in user.badges): - mail_badge = Badge(user_id=user.id, badge_id=2) - g.db.add(mail_badge) - g.db.flush() - send_notification(user.id, f"@AutoJanny has given you the following profile badge:\n\n![]({mail_badge.path})\n\n{mail_badge.name}") - + badge_grant(user=user, badge_id=2) g.db.add(user) g.db.commit() diff --git a/files/routes/admin.py b/files/routes/admin.py index 02adf31385..098e35e3ea 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -10,6 +10,7 @@ from files.helpers.security import * from files.helpers.get import * from files.helpers.media import * from files.helpers.const import * +from files.helpers.actions import * from files.classes import * from flask import * from files.__main__ import app, cache, limiter @@ -920,12 +921,7 @@ def agendaposter(user_id, v): ) g.db.add(ma) - if not user.has_badge(28): - badge = Badge(user_id=user.id, badge_id=28) - g.db.add(badge) - g.db.flush() - send_notification(user.id, f"@AutoJanny has given you the following profile badge:\n\n![]({badge.path})\n\n{badge.name}") - + badge_grant(user=user, badge_id=28) send_repeatable_notification(user.id, f"@{v.username} has marked you as a chud ({note}).") diff --git a/files/routes/awards.py b/files/routes/awards.py index b4004d22a4..ef28b531f3 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -4,7 +4,7 @@ from files.helpers.alerts import * from files.helpers.get import * from files.helpers.const import * from files.helpers.discord import * -from files.helpers.actions import badge_grant +from files.helpers.actions import * from files.classes.award import * from .front import frontlist from flask import g, request @@ -54,32 +54,17 @@ def buy(v, award): if v.coins < price: return {"error": "Not enough coins."}, 400 v.coins -= price v.coins_spent += price - if v.coins_spent >= 1000000 and not v.has_badge(73): - new_badge = Badge(badge_id=73, user_id=v.id) - g.db.add(new_badge) - g.db.flush() - send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") - elif v.coins_spent >= 500000 and not v.has_badge(72): - new_badge = Badge(badge_id=72, user_id=v.id) - g.db.add(new_badge) - g.db.flush() - send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") - elif v.coins_spent >= 250000 and not v.has_badge(71): + if v.coins_spent >= 1000000: + badge_grant(badge_id=73, user=v) + elif v.coins_spent >= 500000: + badge_grant(badge_id=72, user=v) + elif v.coins_spent >= 250000: - new_badge = Badge(badge_id=71, user_id=v.id) - g.db.add(new_badge) - g.db.flush() - send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") - elif v.coins_spent >= 100000 and not v.has_badge(70): - new_badge = Badge(badge_id=70, user_id=v.id) - g.db.add(new_badge) - g.db.flush() - send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") - elif v.coins_spent >= 10000 and not v.has_badge(69): - new_badge = Badge(badge_id=69, user_id=v.id) - g.db.add(new_badge) - g.db.flush() - send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") + badge_grant(badge_id=71, user=v) + elif v.coins_spent >= 100000: + badge_grant(badge_id=70, user=v) + elif v.coins_spent >= 10000: + badge_grant(badge_id=69, user=v) g.db.add(v) @@ -96,21 +81,12 @@ def buy(v, award): lootbox_msg = "You open your lootbox and receive: " + ', '.join(lootbox_items) send_repeatable_notification(v.id, lootbox_msg) - if v.lootboxes_bought == 10 and not v.has_badge(76): - new_badge = Badge(badge_id=76, user_id=v.id) - g.db.add(new_badge) - g.db.flush() - send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") - elif v.lootboxes_bought == 50 and not v.has_badge(77): - new_badge = Badge(badge_id=77, user_id=v.id) - g.db.add(new_badge) - g.db.flush() - send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") - elif v.lootboxes_bought == 150 and not v.has_badge(78): - new_badge = Badge(badge_id=78, user_id=v.id) - g.db.add(new_badge) - g.db.flush() - send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") + if v.lootboxes_bought == 10: + badge_grant(badge_id=76, user=v) + elif v.lootboxes_bought == 50: + badge_grant(badge_id=77, user=v) + elif v.lootboxes_bought == 150: + badge_grant(badge_id=78, user=v) else: award_object = AwardRelationship(user_id=v.id, kind=award) @@ -244,11 +220,7 @@ def award_thing(v, thing_type, id): if author.agendaposter and time.time() < author.agendaposter: author.agendaposter += 86400 else: author.agendaposter = int(time.time()) + 86400 - if not author.has_badge(28): - badge = Badge(user_id=author.id, badge_id=28) - g.db.add(badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({badge.path})\n\n{badge.name}") + badge_grant(user=author, badge_id=28) elif kind == "flairlock": new_name = note[:100].replace("𒐪","") if not new_name and author.flairchanged: @@ -258,94 +230,49 @@ def award_thing(v, thing_type, id): author.customtitle = filter_emojis_only(new_name) if len(author.customtitle) > 1000: abort(403) author.flairchanged = int(time.time()) + 86400 - if not author.has_badge(96): - badge = Badge(user_id=author.id, badge_id=96) - g.db.add(badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({badge.path})\n\n{badge.name}") + badge_grant(user=author, badge_id=96) elif kind == "pause": author.mute = True - if not author.has_badge(68): - new_badge = Badge(badge_id=68, user_id=author.id) - g.db.add(new_badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") + badge_grant(badge_id=68, user=author) elif kind == "unpausable": author.unmutable = True - if not author.has_badge(67): - new_badge = Badge(badge_id=67, user_id=author.id) - g.db.add(new_badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") + badge_grant(badge_id=67, user=author) elif kind == "marsey": if author.marseyawarded: author.marseyawarded += 86400 else: author.marseyawarded = int(time.time()) + 86400 - if not author.has_badge(98): - badge = Badge(user_id=author.id, badge_id=98) - g.db.add(badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({badge.path})\n\n{badge.name}") + badge_grant(user=author, badge_id=98) elif kind == "pizzashill": if author.bird: return {"error": "This user is the under the effect of a conflicting award: Bird Site award."}, 404 if author.longpost: author.longpost += 86400 else: author.longpost = int(time.time()) + 86400 - if not author.has_badge(97): - badge = Badge(user_id=author.id, badge_id=97) - g.db.add(badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({badge.path})\n\n{badge.name}") + badge_grant(user=author, badge_id=97) elif kind == "bird": if author.longpost: return {"error": "This user is the under the effect of a conflicting award: Pizzashill award."}, 404 if author.bird: author.bird += 86400 else: author.bird = int(time.time()) + 86400 - if not author.has_badge(95): - badge = Badge(user_id=author.id, badge_id=95) - g.db.add(badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({badge.path})\n\n{badge.name}") + badge_grant(user=author, badge_id=95) elif kind == "eye": author.eye = True - if not author.has_badge(83): - new_badge = Badge(badge_id=83, user_id=author.id) - g.db.add(new_badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") + badge_grant(badge_id=83, user=author) elif kind == "offsitementions": author.offsitementions = True - new_badge = badge_grant(user_id=author.id, badge_id=140, commit=False) - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") + badge_grant(user=author, badge_id=140) elif kind == "alt": author.alt = True - if not author.has_badge(84): - new_badge = Badge(badge_id=84, user_id=author.id) - g.db.add(new_badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") + badge_grant(badge_id=84, user=author) elif kind == "unblockable": author.unblockable = True - if not author.has_badge(87): - new_badge = Badge(badge_id=87, user_id=author.id) - g.db.add(new_badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") + badge_grant(badge_id=87, user=author) for block in g.db.query(UserBlock).filter_by(target_id=author.id).all(): g.db.delete(block) elif kind == "fish": author.fish = True - if not author.has_badge(90): - new_badge = Badge(badge_id=90, user_id=author.id) - g.db.add(new_badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") + badge_grant(badge_id=90, user=author) elif kind == "progressivestack": if author.progressivestack: author.progressivestack += 21600 else: author.progressivestack = int(time.time()) + 21600 - if not author.has_badge(94): - badge = Badge(user_id=author.id, badge_id=94) - g.db.add(badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({badge.path})\n\n{badge.name}") + badge_grant(user=author, badge_id=94) elif kind == "benefactor": if author.patron: return {"error": "This user is already a paypig!"}, 400 author.patron = 1 @@ -353,28 +280,16 @@ def award_thing(v, thing_type, id): else: author.patron_utc = int(time.time()) + 2629746 author.procoins += 2500 if author.discord_id: add_role(author, "1") - if not v.has_badge(103): - badge = Badge(user_id=v.id, badge_id=103) - g.db.add(badge) - g.db.flush() - send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({badge.path})\n\n{badge.name}") + badge_grant(user=v, badge_id=103) elif kind == "rehab": if author.rehab: author.rehab += 86400 else: author.rehab = int(time.time()) + 86400 - if not author.has_badge(109): - badge = Badge(user_id=author.id, badge_id=109) - g.db.add(badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({badge.path})\n\n{badge.name}") + badge_grant(user=id, badge_id=109) elif kind == "deflector": if author.deflector: author.deflector += 36000 else: author.deflector = int(time.time()) + 36000 elif kind == "beano": - if not author.has_badge(128): - badge = Badge(user_id=author.id, badge_id=128) - g.db.add(badge) - g.db.flush() - send_notification(author.id, f"@AutoJanny has given you the following profile badge:\n\n![]({badge.path})\n\n{badge.name}") + badge_grant(user=author, badge_id=128) elif kind == "checkmark": author.verified = "Verified" diff --git a/files/routes/comments.py b/files/routes/comments.py index b843248955..5edf7491f4 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -5,6 +5,7 @@ from files.helpers.const import * from files.helpers.slots import * from files.helpers.blackjack import * from files.helpers.treasure import * +from files.helpers.actions import * from files.classes import * from files.routes.front import comment_idlist from files.routes.static import marsey_list @@ -275,30 +276,15 @@ def api_comment(v): marsey = Marsey(name=name, author_id=user.id, tags=tags, count=0) g.db.add(marsey) - g.db.flush() all_by_author = g.db.query(Marsey).filter_by(author_id=user.id).count() - if all_by_author >= 10 and not user.has_badge(16): - new_badge = Badge(badge_id=16, user_id=user.id) + if all_by_author >= 9: + badge_grant(badge_id=16, user=user) - g.db.add(new_badge) - g.db.flush() - - if v.id != user.id: - text = f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}" - send_notification(user.id, text) - - elif all_by_author < 10 and not user.has_badge(17): - new_badge = Badge(badge_id=17, user_id=user.id) - - g.db.add(new_badge) - g.db.flush() - - if v.id != user.id: - text = f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}" - send_notification(user.id, text) + else: + badge_grant(badge_id=17, user=user) requests.post(f'https://api.cloudflare.com/client/v4/zones/{CF_ZONE}/purge_cache', headers=CF_HEADERS, diff --git a/files/routes/login.py b/files/routes/login.py index 59d7139398..da204d87b7 100644 --- a/files/routes/login.py +++ b/files/routes/login.py @@ -2,6 +2,7 @@ from urllib.parse import urlencode from files.mail import * from files.__main__ import app, limiter from files.helpers.const import * +from files.helpers.actions import * import requests @app.get("/login") @@ -336,21 +337,11 @@ def sign_up_post(v): ref_user = g.db.query(User).filter_by(id=ref_id).one_or_none() if ref_user: - if ref_user.referral_count and not ref_user.has_badge(10): - new_badge = Badge(user_id=ref_user.id, badge_id=10) - g.db.add(new_badge) - g.db.flush() - send_notification(ref_user.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") - if ref_user.referral_count >= 10 and not ref_user.has_badge(11): - new_badge = Badge(user_id=ref_user.id, badge_id=11) - g.db.add(new_badge) - g.db.flush() - send_notification(ref_user.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") - if ref_user.referral_count >= 100 and not ref_user.has_badge(12): - new_badge = Badge(user_id=ref_user.id, badge_id=12) - g.db.add(new_badge) - g.db.flush() - send_notification(ref_user.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") + badge_grant(user=ref_user, badge_id=10) + if ref_user.referral_count >= 9: + badge_grant(user=ref_user, badge_id=11) + if ref_user.referral_count >= 99: + badge_grant(user=ref_user, badge_id=12) if email: try: send_verification_email(new_user) diff --git a/files/routes/settings.py b/files/routes/settings.py index 4fe29e5d45..adfbef6a24 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -3,6 +3,7 @@ from files.helpers.alerts import * from files.helpers.sanitize import * from files.helpers.discord import remove_user, set_nick from files.helpers.const import * +from files.helpers.actions import * from files.mail import * from files.__main__ import app, cache, limiter import youtube_dl @@ -405,11 +406,7 @@ def gumroad(v): g.db.add(v) - if not v.has_badge(20+tier): - new_badge = Badge(badge_id=20+tier, user_id=v.id) - g.db.add(new_badge) - g.db.flush() - send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}") + badge_grant(badge_id=20+tier, user=v) g.db.commit() @@ -958,4 +955,4 @@ def settings(v): @app.get("/settings/profile") @auth_required def settings_profile(v): - return render_template("settings_profile.html", v=v) \ No newline at end of file + return render_template("settings_profile.html", v=v)