commit b446ab9f04c30b95cd9964b7133ff766c1f4e4af
parent 951fff7d88d7eb176a27c7daeb1b1a7efd3e3d4e
Author: parazyd <parazyd@dyne.org>
Date: Wed, 23 Jan 2019 12:38:16 +0100
Implement shoutbox functionality.
Diffstat:
4 files changed, 108 insertions(+), 2 deletions(-)
diff --git a/diaspora.py b/diaspora.py
@@ -36,7 +36,7 @@ from utils import (get_story, makenav, randomstring, getcountryname,
validate_user, get_multiple_users, get_messages,
get_latest_messages, send_message, delete_user,
get_recent_stories, follow_user, unfollow_user,
- is_following, get_following)
+ is_following, get_following, send_shout, get_shouts)
app = Flask(__name__)
@@ -212,6 +212,17 @@ def sendmsg():
return redirect('/messages?from=%s' % request.form.get('Id'))
+@app.route('/sendshout', methods=['POST'])
+@login_required
+def sendshout():
+ """
+ Route for leaving a shout.
+ """
+ send_shout(request.form.get('Id'), request.form.get('Message'),
+ request.form.get('Us'))
+ return redirect('/profile?id=%s' % request.form.get('Id'))
+
+
@app.route('/write', methods=['GET', 'POST'])
def write():
"""
@@ -360,6 +371,7 @@ def profile():
fol = is_following(current_user.username, user['email'])
return render_template('profile.html', name=user['name'], uid=uid,
stories=stories, profiles={},
+ shouts=get_shouts(user['email']),
is_following=fol)
return render_template('fail.html', msg='No such profile.')
diff --git a/templates/messages.html b/templates/messages.html
@@ -54,7 +54,7 @@
{% for i in messages %}
<hr>
<blockquote class="blockquote">
- <p>{{ i['message']}}</p>
+ <p>{{ i['message'] }}</p>
<footer class="blockquote-footer">{{ i['from'] }} - {{ i['time'] }}</footer>
</blockquote>
{% endfor %}
diff --git a/templates/profile.html b/templates/profile.html
@@ -35,9 +35,52 @@
<hr>
<div class="container text-center">
<a href="/messages?from={{ uid }}" class="btn btn-outline-primary">Send PM</a>
+ <button type="button" class="btn btn-outline-primary" data-toggle="modal" data-target="#shoutModal">
+ Leave shout
+ </button>
+ </div>
+
+ <div class="modal fade" id="shoutModal" tabindex="-1" role="dialog" aria-labelledby="shoutModalTitle" aria-hidden="true">
+ <div class="modal-dialog modal-dialog-centered" role="document">
+ <div class="modal-content">
+ <div class="modal-header">
+ <h5 class="modal-title" id="shoutModalLongTitle">Leave shout</h5>
+ <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+ <span aria-hidden="true">×</span>
+ </button>
+ </div>
+ <div class="modal-body">
+ <p>Write shout:</p>
+ <form action="/sendshout" method="POST" id="shoutform">
+ <input type="hidden" name="Id" value="{{ uid }}">
+ <input type="hidden" name="Us" value="{{ current_user.id }}">
+ <textarea name="Message" class="form-control" form="shoutform" required></textarea>
+ </form>
+ </div>
+ <div class="modal-footer">
+ <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">Close</button>
+ <button form="shoutform" type="submit" class="btn btn-outline-success">Shout</button>
+ </div>
+ </div>
+ </div>
</div>
{% endif %}
+ <hr>
+ <h3>Shoutbox</h3>
+ {% if not shouts %}
+ <hr>
+ <p>No shouts.</p>
+ {% endif %}
+ {% for i in shouts %}
+ <hr>
+ <blockquote class="blockquote">
+ <p>{{ i['message'] }}</p>
+ <footer class="blockquote-footer">{{ i['from'] }} - {{ i['time'] }}</footer>
+ </blockquote>
+ {% endfor %}
+ <hr>
+
</main>
{% include 'footer.html' %}
diff --git a/utils.py b/utils.py
@@ -361,6 +361,57 @@ def send_message(id_to, msg, id_us):
json.dump(theirdata, theirmsgs)
+def send_shout(id_to, msg, id_us):
+ """
+ Sends a shout to a user.
+ """
+ if not id_to or not msg or not id_us:
+ return
+ ours = find_user_by_id(id_us)
+ them = find_user_by_id(id_to)
+ if not ours or not them:
+ return
+
+ makedirs('shouts', exist_ok=True)
+ shoutpath = (join('shouts', them['email']))
+
+ msgdict = {
+ 'from': ours['name'],
+ 'email': ours['email'],
+ 'message': Markup(msg).striptags(),
+ 'time': int(time()),
+ }
+
+ theirdata = []
+
+ if isfile(shoutpath):
+ with open(shoutpath) as shouts_file:
+ theirdata = json.load(shouts_file)
+
+ theirdata.append(msgdict)
+ with open(shoutpath, 'w') as shouts_file:
+ json.dump(theirdata, shouts_file)
+
+
+def get_shouts(email):
+ """
+ Gets all shouts for a user.
+ """
+ if not isfile(join('shouts', email)):
+ return []
+
+ theirdata = []
+ with open(join('shouts', email)) as shouts:
+ theirdata = json.load(shouts)
+
+ shouts = []
+ for i in theirdata:
+ i['time'] = strftime('%d.%m.%Y. %H:%M UTC', gmtime(i['time']))
+ shouts.append(i)
+
+ return shouts[::-1]
+
+
def follow_user(id_us, id_them):
"""
Follows a user.