diff --git a/files/assets/js/all_js.js b/files/assets/js/all_js.js index a79191231..4b4ce5da7 100644 --- a/files/assets/js/all_js.js +++ b/files/assets/js/all_js.js @@ -261,10 +261,18 @@ function post(url, callback, errortext) { xhr.send(form); }; -function post_toast(url, callback) { +function post_toast(url, callback, data) { var xhr = new XMLHttpRequest(); xhr.open("POST", url, true); var form = new FormData() + + if(typeof data === 'object' && data !== null) { + for(let k of Object.keys(data)) { + form.append(k, data[k]); + } + } + + form.append("formkey", formkey()); xhr.withCredentials=true; @@ -280,13 +288,20 @@ function post_toast(url, callback) { } else if (xhr.status >= 300 && xhr.status < 400) { window.location.href = JSON.parse(xhr.response)["redirect"] } else { - data=JSON.parse(xhr.response); - - $('#toast-post-error').toast('dispose'); - $('#toast-post-error').toast('show'); - document.getElementById('toast-post-error-text').innerText = data["error"]; - return false - + try { + data=JSON.parse(xhr.response); + + $('#toast-post-error').toast('dispose'); + $('#toast-post-error').toast('show'); + document.getElementById('toast-post-error-text').innerText = data["error"]; + return false + } catch(e) { + $('#toast-post-success').toast('dispose'); + $('#toast-post-error').toast('dispose'); + $('#toast-post-error').toast('show'); + document.getElementById('toast-post-error-text').innerText = "Error. Try again later."; + return false + } } }; @@ -396,4 +411,4 @@ $('.mention-user').click(function (event) { window.location.href='/@' + $(this).data('original-name'); -}); \ No newline at end of file +}); 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 %}