diff --git a/files/routes/users.py b/files/routes/users.py index 2790ef440..8abc3654e 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -34,6 +34,42 @@ def suicide(v, username): g.db.add(v) return "", 204 +@app.get("/@/coins") +@auth_required +def get_coins(v, username): + user = get_user(username) + if user is not None: return {"coins": user.coins}, 200 + else: return {"error": "invalid_user"}, 404 + +@app.post("/@/transfer_coins") +@auth_required +@validate_formkey +def transfer_coins(v, username): + receiver = get_user(username) + + if receiver is None: return {"error": "That user doesn't exist."}, 404 + + if receiver.id != v.id: + amount = request.form.get("amount", "") + amount = int(amount) if amount.isdigit() else None + + if amount is None or amount <= 0: return {"error": f"Invalid amount of {app.config['SITE_NAME']}coins."}, 400 + if v.coins < amount: return {"error": f"You don't have enough {app.config['SITE_NAME']}coins"}, 400 + if amount < 100: return {"error": f"You have to gift at least 100 {app.config['SITE_NAME']}coins."}, 400 + + v.coins -= amount + receiver.coins += amount + g.db.add(receiver) + g.db.add(v) + + g.db.commit() + + transfer_message = f"🤑 [@{v.username}]({v.url}) has gifted you {amount} {app.config['SITE_NAME']}coins!" + send_notification(v.id, receiver, transfer_message) + return {"message": f"{amount} {app.config['SITE_NAME']}coins transferred!"}, 200 + + return "", 204 + @app.get("/leaderboard") @auth_desired def leaderboard(v): diff --git a/files/templates/header.html b/files/templates/header.html index 47c5dc4e1..c8ad70b7f 100644 --- a/files/templates/header.html +++ b/files/templates/header.html @@ -79,7 +79,7 @@
{{v.username}}
-
{{v.coins}} {{"SITE_NAME" | app_config}}coins
+
{{v.coins}} {{"SITE_NAME" | app_config}}coins
@@ -176,4 +176,4 @@ {% endif %} - \ No newline at end of file + diff --git a/files/templates/userpage.html b/files/templates/userpage.html index 9612ab698..d5116d5df 100644 --- a/files/templates/userpage.html +++ b/files/templates/userpage.html @@ -54,6 +54,38 @@ if (audio.paused) audio.play(); }, {once : true}); } + + function transferCoins() { + let t = event.target; + t.disabled = true; + + post_toast("/@{{ u.username }}/transfer_coins", + xhr => { + if(xhr.status == 200) { + fetch("/@{{ u.username }}/coins") + .then(r => r.json()) + .then(m => document.getElementById("profile-coins-amount").innerText = m["coins"]) + + fetch("/@{{ v.username }}/coins") + .then(r => r.json()) + .then(m => document.getElementById("user-coins-amount").innerText = m["coins"]) + } + }, + {"amount": document.getElementById("coins-transfer-amount").value} + ); + + setTimeout(_ => t.disabled = false, 2000); + } + + function toggleElement(group, id) { + for(let el of document.getElementsByClassName(group)) { + if(el.id != id) { + el.classList.add('d-none'); + } + } + + document.getElementById(id).classList.toggle('d-none'); + }
@@ -119,7 +151,8 @@ {% if u.customtitle %}

{{u.customtitle | safe}}

{% endif %}
- {{u.coins}} {{"SITE_NAME" | app_config}}coins   {% if u.stored_subscriber_count >=1 and not u.is_nofollow %}{{u.stored_subscriber_count}} follower{{'s' if u.stored_subscriber_count != 1 else ''}}   {% endif %}joined {{u.created_date}} + {{u.coins}} {{"SITE_NAME" | app_config}}coins   {% if u.stored_subscriber_count >=1 and not u.is_nofollow %}{{u.stored_subscriber_count}} follower{{'s' if u.stored_subscriber_count != 1 else ''}}   {% endif %}joined {{u.created_date}} +
{% if u.bio_html %}

@@ -136,9 +169,11 @@
 						Unfollow
 						Follow						
 						
-						Message
+						Message
+
 						Get them help
-						
+ Gift {{"SITE_NAME" | app_config}}coins +

 							
 							

@@ -155,6 +190,11 @@
 							
 						
+
+ + +
+ {% elif v and v.id == u.id %} Edit profile Profile views @@ -567,4 +607,4 @@ {% include "emoji_modal.html" %} {% include "gif_modal.html" %} {% endif %} -{% endblock %} \ No newline at end of file +{% endblock %}