From 2fa71a252b6df040f08bfb7a06199741db5642ef Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Tue, 13 Sep 2022 18:53:19 +0200 Subject: [PATCH] kofi integration --- files/__main__.py | 8 ++-- files/classes/transactions.py | 2 +- files/helpers/const.py | 1 + files/helpers/jinja2.py | 2 +- files/routes/settings.py | 2 +- files/routes/users.py | 56 ++++++++++++++++++++++---- files/templates/settings_security.html | 14 ++++++- sql/20220913-transactions.sql | 2 +- 8 files changed, 70 insertions(+), 17 deletions(-) diff --git a/files/__main__.py b/files/__main__.py index 2002d9d7e9..e9d54d3971 100644 --- a/files/__main__.py +++ b/files/__main__.py @@ -88,9 +88,11 @@ if not path.isfile(f'/site_settings.json'): def before_request(): g.agent = request.headers.get("User-Agent") - if not g.agent: return 'Please use a "User-Agent" header!', 403 + if not g.agent and request.path != '/kofi': + return 'Please use a "User-Agent" header!', 403 - ua = g.agent.lower() + ua = g.agent or '' + ua = ua.lower() with open('/site_settings.json', 'r', encoding='utf_8') as f: app.config['SETTINGS'] = json.load(f) @@ -98,7 +100,7 @@ def before_request(): if request.host != app.config["SERVER_NAME"]: return {"error":"Unauthorized host provided."}, 401 if request.headers.get("CF-Worker"): return {"error":"Cloudflare workers are not allowed to access this website."}, 401 - if not app.config['SETTINGS']['Bots'] and request.headers.get("Authorization"): abort(503) + if not app.config['SETTINGS']['Bots'] and request.headers.get("Authorization"): abort(403) g.db = db_session() g.webview = '; wv) ' in ua diff --git a/files/classes/transactions.py b/files/classes/transactions.py index c7a162b556..6030d98978 100644 --- a/files/classes/transactions.py +++ b/files/classes/transactions.py @@ -6,7 +6,7 @@ if KOFI_TOKEN: class Transaction(Base): - __tablename__ = "kofi" + __tablename__ = "transactions" id = Column(String, primary_key=True) created_utc = Column(Integer) type = Column(String) diff --git a/files/helpers/const.py b/files/helpers/const.py index 867199d4c5..89cc64e918 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -1048,6 +1048,7 @@ GIPHY_KEY = environ.get('GIPHY_KEY').strip() MASTER_KEY = environ.get("MASTER_KEY") FP = environ.get("FP") KOFI_TOKEN = environ.get("KOFI_TOKEN") +KOFI_LINK = environ.get("KOFI_LINK") tiers={ "(Paypig)": 1, diff --git a/files/helpers/jinja2.py b/files/helpers/jinja2.py index 12909d90c0..719f1c6bab 100644 --- a/files/helpers/jinja2.py +++ b/files/helpers/jinja2.py @@ -58,4 +58,4 @@ def inject_constants(): "CASINO_ENABLED":CASINO_ENABLED, "GUMROAD_LINK":GUMROAD_LINK, "DEFAULT_THEME":DEFAULT_THEME, "DESCRIPTION":DESCRIPTION, "has_sidebar":has_sidebar, "has_logo":has_logo, "has_app":has_app, - "FP":FP, "NOTIF_MODACTION_JL_MIN":NOTIF_MODACTION_JL_MIN, "cache":cache, "ONLINE_STR":ONLINE_STR, "patron":patron, "approved_embed_hosts":approved_embed_hosts, "dues":dues, "SIDEBAR_THREAD":SIDEBAR_THREAD, "BANNER_THREAD":BANNER_THREAD, "BADGE_THREAD":BADGE_THREAD, "SNAPPY_THREAD":SNAPPY_THREAD} + "FP":FP, "NOTIF_MODACTION_JL_MIN":NOTIF_MODACTION_JL_MIN, "cache":cache, "ONLINE_STR":ONLINE_STR, "patron":patron, "approved_embed_hosts":approved_embed_hosts, "dues":dues, "SIDEBAR_THREAD":SIDEBAR_THREAD, "BANNER_THREAD":BANNER_THREAD, "BADGE_THREAD":BADGE_THREAD, "SNAPPY_THREAD":SNAPPY_THREAD, "KOFI_TOKEN":KOFI_TOKEN, "KOFI_LINK":KOFI_LINK} diff --git a/files/routes/settings.py b/files/routes/settings.py index a7833d9b82..f44e12070d 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -343,7 +343,7 @@ def themecolor(v): @auth_required def gumroad(v): if not (v.email and v.is_activated): - return {"error": f"You must have a verified email to verify {patron} status and claim your rewards"}, 400 + return {"error": f"You must have a verified email to verify {patron} status and claim your rewards!"}, 400 data = {'access_token': GUMROAD_TOKEN, 'email': v.email} response = requests.get('https://api.gumroad.com/v2/sales', data=data, timeout=5).json()["sales"] diff --git a/files/routes/users.py b/files/routes/users.py index dfc597b28e..265aa0e07e 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -18,7 +18,7 @@ from collections import Counter import gevent from sys import stdout import os - +import json def leaderboard_thread(): db = db_session() @@ -1385,14 +1385,15 @@ def bid_list(v, bid): @app.post("/kofi") def kofi(): - verification_token = request.values.get('verification_token') + data = json.loads(request.values['data']) + verification_token = data['verification_token'] if verification_token != KOFI_TOKEN: abort(400) - id = request.values.get('kofi_transaction_id') - created_utc = time.mktime(time.strptime(request.values.get('timestamp'), "%Y-%m-%dT%H:%M:%SZ")) - type = request.values.get('type') - amount = int(request.values.get('amount')) - email = request.values.get('email') + id = data['kofi_transaction_id'] + created_utc = int(time.mktime(time.strptime(data['timestamp'].split('.')[0], "%Y-%m-%dT%H:%M:%S"))) + type = data['type'] + amount = int(float(data['amount'])) + email = data['email'] transaction = Transaction( id=id, @@ -1403,5 +1404,44 @@ def kofi(): ) g.db.add(transaction) + return '' - return '' \ No newline at end of file +kofi_tiers={ + 5: 1, + 10: 2, + 20: 3, + 50: 4 + } + +@app.post("/settings/kofi") +@auth_required +def settings_kofi(v): + if not (v.email and v.is_activated): + return {"error": f"You must have a verified email to verify {patron} status and claim your rewards!"}, 400 + + transaction = g.db.query(Transaction).filter_by(email=v.email).one_or_none() + + if not transaction: + return {"error": "Email not found"}, 404 + + tier = kofi_tiers[transaction.amount] + if v.patron == tier: return {"error": f"{patron} rewards already claimed"}, 400 + + procoins = procoins_li[tier] - procoins_li[v.patron] + if procoins < 0: return {"error": f"{patron} rewards already claimed"}, 400 + + existing = g.db.query(User.id).filter(User.email == v.email, User.is_activated == True, User.patron >= tier).first() + if existing: return {"error": f"{patron} rewards already claimed on another account"}, 400 + + v.patron = tier + if v.discord_id: add_role(v, f"{tier}") + + v.procoins += procoins + send_repeatable_notification(v.id, f"You have received {procoins} Marseybux! You can use them to buy awards in the [shop](/shop).") + + g.db.add(v) + + badge_grant(badge_id=20+tier, user=v) + + + return {"message": f"{patron} rewards claimed!"} \ No newline at end of file diff --git a/files/templates/settings_security.html b/files/templates/settings_security.html index f2d1085430..76d4ccdb0e 100644 --- a/files/templates/settings_security.html +++ b/files/templates/settings_security.html @@ -45,7 +45,11 @@ diff --git a/sql/20220913-transactions.sql b/sql/20220913-transactions.sql index 188ada617b..f887687963 100644 --- a/sql/20220913-transactions.sql +++ b/sql/20220913-transactions.sql @@ -1,5 +1,5 @@ CREATE TABLE public.transactions ( - id integer PRIMARY KEY, + id character varying(36) PRIMARY KEY, created_utc integer NOT NULL, type character varying(12) NOT NULL, amount integer NOT NULL,