commit 910ceb501a962491ff62900949d7b487ced3b1c9
parent 785a028f78c1589060982c14cf74df3b000b2721
Author: parazyd <parazyd@dyne.org>
Date: Wed, 23 Jan 2019 16:44:06 +0100
Allow deleting shouts from own shoutbox.
Diffstat:
3 files changed, 51 insertions(+), 3 deletions(-)
diff --git a/diaspora.py b/diaspora.py
@@ -37,7 +37,7 @@ from utils import (get_story, makenav, randomstring, getcountryname,
get_latest_messages, send_message, delete_user,
get_recent_stories, follow_user, unfollow_user,
is_following, get_following, send_shout, get_shouts,
- get_profiles_from_stories, parsetime)
+ get_profiles_from_stories, parsetime, delete_shout)
app = Flask(__name__)
@@ -225,6 +225,16 @@ def sendshout():
return redirect('/profile?id=%s' % request.form.get('Id'))
+@app.route('/delshout', methods=['POST'])
+@login_required
+def delshout():
+ """
+ Route for deleting a shout.
+ """
+ delete_shout(request.form.get('Id'), request.form.get('ShoutId'))
+ return redirect('/profile?id=%s' % request.form.get('Id'))
+
+
@app.route('/write', methods=['GET', 'POST'])
def write():
"""
diff --git a/templates/profile.html b/templates/profile.html
@@ -77,7 +77,18 @@
<hr>
<blockquote class="blockquote">
<p>{{ i['message'] }}</p>
- <footer class="blockquote-footer">{{ i['from'] }} - {{ parsetime(i['time']) }}</footer>
+ <footer class="blockquote-footer">
+ {{ i['from'] }} - {{ parsetime(i['time']) }}
+ {% if current_user.id == uid or current_user.is_admin %}
+ <form action="/delshout" method="POST" id="shoutdel_{{ loop.index }}">
+ <input type="hidden" name="Id" value="{{ uid }}">
+ <input type="hidden" name="ShoutId" value="{{ i['id'] }}">
+ <button type="submit" class="btn btn-sm" title="Delete this shout" form="shoutdel_{{ loop.index }}">
+ <span class="fa">×</span>
+ </button>
+ </form>
+ {% endif %}
+ </footer>
</blockquote>
{% endfor %}
<hr>
diff --git a/utils.py b/utils.py
@@ -18,6 +18,7 @@
Utility functions.
"""
import json
+from hashlib import sha256
from os import makedirs, listdir, remove
from os.path import join, isfile
from random import SystemRandom, shuffle
@@ -260,7 +261,7 @@ def make_profile(name, email):
welcome_msg = "Welcome to Diaspora Diaries! Enjoy your stay :)"
with open(join(msgpath, initial_user['email']), 'w') as msgfile:
json.dump([{'from': 'Diaspora Diaries', 'message': welcome_msg,
- 'time': int(time())}], msgfile, 'unread': 1)
+ 'time': int(time()), 'unread': 1}], msgfile)
return plain_pw
@@ -377,11 +378,14 @@ def send_shout(id_to, msg, id_us):
makedirs('shouts', exist_ok=True)
shoutpath = (join('shouts', them['email']))
+ m = sha256()
+ m.update(Markup(msg).striptags().encode())
msgdict = {
'from': ours['name'],
'email': ours['email'],
'message': Markup(msg).striptags(),
'time': int(time()),
+ 'id': m.hexdigest(),
}
theirdata = []
@@ -395,6 +399,29 @@ def send_shout(id_to, msg, id_us):
json.dump(theirdata, shouts_file)
+def delete_shout(id_to, id_shout):
+ """
+ Deletes a specific shout.
+ """
+ if not id_to or not id_shout:
+ return
+ ours = find_user_by_id(id_to)
+ if not ours:
+ return
+
+ shoutpath = join('shouts', ours['email'])
+ if isfile(shoutpath):
+ with open(shoutpath) as shouts_file:
+ shouts = json.load(shouts_file)
+
+ for i in shouts:
+ if i['id'] == id_shout:
+ shouts.remove(i)
+
+ with open(shoutpath, 'w') as shouts_file:
+ json.dump(shouts, shouts_file)
+
+
def get_shouts(email):
"""
Gets all shouts for a user.