reapply dramacoin-transfers on top of master

master
atrc445 2021-08-08 20:26:15 +02:00
parent add068d6d2
commit df364f530c
3 changed files with 82 additions and 6 deletions

View File

@ -34,6 +34,42 @@ def suicide(v, username):
g.db.add(v)
return "", 204
@app.get("/@<username>/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("/@<username>/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):

View File

@ -79,7 +79,7 @@
<div><img src="{{v.profile_url}}" class="profile-pic-35"></div>
<div class="text-left pl-2">
<div style="color: #{{v.namecolor}}" class="text-small font-weight-bold {% if v.animatedname %}{% if v.patron %}patron{% else %}leaderboard{% endif %}{% endif %}">{{v.username}}</div>
<div class="text-small-extra text-purple"><i class="fad fa-coins mr-1"></i>{{v.coins}} {{"SITE_NAME" | app_config}}coins</div>
<div class="text-small-extra text-purple"><i class="fad fa-coins mr-1"></i><span id="user-coins-amount">{{v.coins}}</span> {{"SITE_NAME" | app_config}}coins</div>
</div>
</div>
</a>
@ -176,4 +176,4 @@
{% endif %}
</div>
</div>
</nav>
</nav>

View File

@ -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');
}
</script>
<textarea id="userid" style="display: none;">{% if u.song %}{{u.id}}{% else %}nosong{% endif %}</textarea>
<div class="row d-none d-md-block">
@ -119,7 +151,8 @@
{% if u.customtitle %}<p class="font-weight-bolder" style="color: #{{u.titlecolor}}">{{u.customtitle | safe}}</p>{% endif %}
<div class="font-weight-bolder">
<span>{{u.coins}}</span> {{"SITE_NAME" | app_config}}coins&nbsp;&nbsp; {% if u.stored_subscriber_count >=1 and not u.is_nofollow %}<a href="/@{{u.username}}/followers">{{u.stored_subscriber_count}} follower{{'s' if u.stored_subscriber_count != 1 else ''}}</a>&nbsp;&nbsp; {% endif %}joined <span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="{{u.created_datetime}}">{{u.created_date}}</span>
<span id="profile-coins-amount">{{u.coins}}</span> {{"SITE_NAME" | app_config}}coins&nbsp;&nbsp; {% if u.stored_subscriber_count >=1 and not u.is_nofollow %}<a href="/@{{u.username}}/followers">{{u.stored_subscriber_count}} follower{{'s' if u.stored_subscriber_count != 1 else ''}}</a>&nbsp;&nbsp; {% endif %}joined <span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="{{u.created_datetime}}">{{u.created_date}}</span>
</div>
{% if u.bio_html %}
<pre></pre>
@ -136,9 +169,11 @@
<a id="button-unsub" class="btn btn-secondary {% if not is_following %}d-none{% endif %}" href="javascript:void(0)" onclick="post('/unfollow/{{u.username}}', callback=function(){document.getElementById('button-unsub').classList.toggle('d-none');document.getElementById('button-sub').classList.toggle('d-none');})">Unfollow</a>
<a id="button-sub" class="btn btn-primary {% if is_following or u.is_nofollow or u.is_blocked %}d-none{% endif %}" href="javascript:void(0)" onclick="post('/follow/{{u.username}}', callback=function(){document.getElementById('button-sub').classList.toggle('d-none');document.getElementById('button-unsub').classList.toggle('d-none');})">Follow</a>
<a class="btn btn-primary" href="javascript:void(0)" onclick="document.getElementById('message').classList.toggle('d-none')">Message</a>
<a class="btn btn-primary" href="javascript:void(0)" onclick="toggleElement('profile-toggleable', 'message')">Message</a>
<a class="btn btn-primary" href="javascript:void(0)" onclick="post('/@{{u.username}}/suicide', function(){window.location.reload(true);})">Get them help</a>
<form class="d-none" id="message" action="/@{{u.username}}/message" method="post">
<a class="btn btn-primary" href="javascript:void(0)" onclick="toggleElement('profile-toggleable', 'coin-transfer')">Gift {{"SITE_NAME" | app_config}}coins</a>
<form class="d-none profile-toggleable" id="message" action="/@{{u.username}}/message" method="post">
<pre></pre>
<textarea id="input-message" form="message" name="message" rows="3" class="form-control" required></textarea>
<pre></pre>
@ -155,6 +190,11 @@
<input type="submit" value="Submit" class="btn btn-primary mt-3">
</form>
<div class="d-none mt-3 profile-toggleable" id="coin-transfer">
<input id="coins-transfer-amount" class="form-control" name="amount" type="number">
<button class="btn btn-primary mt-3" onclick="transferCoins()">Gift</button>
</div>
{% elif v and v.id == u.id %}
<a href="/settings/profile" class="btn btn-secondary">Edit profile</a>
<a href="/views" class="btn btn-secondary">Profile views</a>
@ -567,4 +607,4 @@
{% include "emoji_modal.html" %}
{% include "gif_modal.html" %}
{% endif %}
{% endblock %}
{% endblock %}