commit b007b707e3ddc20499c6f450f291a8f44a26556c
parent 657e3e9e2dfa3a4503836bb51e1804b747c81ffb
Author: parazyd <parazyd@dyne.org>
Date: Tue, 22 Jan 2019 23:34:44 +0100
Implement user follow/unfollow.
Diffstat:
3 files changed, 98 insertions(+), 4 deletions(-)
diff --git a/diaspora.py b/diaspora.py
@@ -35,7 +35,8 @@ from utils import (get_story, makenav, randomstring, getcountryname,
make_profile, find_user_by_id, find_user_by_email,
validate_user, get_multiple_users, get_messages,
get_latest_messages, send_message, delete_user,
- get_recent_stories)
+ get_recent_stories, follow_user, unfollow_user,
+ is_following)
app = Flask(__name__)
@@ -182,6 +183,21 @@ def messages():
return render_template('messages.html', messages=msgs)
+@app.route('/follow', methods=['POST'])
+@login_required
+def follow():
+ """
+ Route for following a user.
+ """
+
+ if request.form.get('Act') == 'follow':
+ follow_user(current_user.id, request.form.get('Id'))
+ elif request.form.get('Act') == 'unfollow':
+ unfollow_user(current_user.id, request.form.get('Id'))
+
+ return redirect('/profile?id=%s' % request.form.get('Id'))
+
+
@app.route('/sendmsg', methods=['POST'])
@login_required
def sendmsg():
@@ -335,8 +351,13 @@ def profile():
if user:
stories = get_multiple_stories_filtered('email', user['email'],
('visible', 1))
+
+ fol = False
+ if current_user.is_active:
+ fol = is_following(current_user.username, user['email'])
return render_template('profile.html', name=user['name'], uid=uid,
- stories=stories, profiles={})
+ stories=stories, profiles={},
+ is_following=fol)
return render_template('fail.html', msg='No such profile.')
diff --git a/templates/profile.html b/templates/profile.html
@@ -9,10 +9,25 @@
<h1 class="cover-heading">Diaries by {{ name }}</h1>
<hr>
+ <div class="container">
{% if current_user.id == uid %}
- <a href="/changepass" class="btn btn-outline-secondary">Change your password</a>
+ <a href="/following" class="btn btn-outline-primary">People you follow</a>
+ <a href="/changepass" class="btn btn-outline-primary">Change your password</a>
<hr>
+ {% else %}
+ <form action="/follow" method="POST" id="followform">
+ <input type="hidden" name="Id" value="{{ uid }}">
+ {% if is_following %}
+ <input type="hidden" name="Act" value="unfollow">
+ <input type="submit" class="btn btn-primary" value="Following" title="Click to unfollow">
+ {% else %}
+ <input type="hidden" name="Act" value="follow">
+ <input type="submit" class="btn btn-outline-primary" value="Follow">
{% endif %}
+ </form>
+ {% endif %}
+ <hr>
+ </div>
{% include 'gallery.html' %}
diff --git a/utils.py b/utils.py
@@ -18,7 +18,7 @@
Utility functions.
"""
import json
-from os import makedirs, listdir
+from os import makedirs, listdir, remove
from os.path import join
from random import SystemRandom, shuffle
from shutil import rmtree
@@ -249,6 +249,10 @@ def make_profile(name, email):
]
sql_insert(userargs)
+ makedirs('follows', exist_ok=True)
+ with open(join('follows', email), 'w') as follow_file:
+ json.dump([], follow_file)
+
msgpath = join('messages', email)
makedirs(msgpath, exist_ok=True)
@@ -367,10 +371,64 @@ def send_message(id_to, msg, id_us):
json.dump(theirdata, theirmsgs)
+def follow_user(id_us, id_them):
+ """
+ Follows a user.
+ """
+ if not id_us or not id_them:
+ return
+ ours = find_user_by_id(id_us)
+ them = find_user_by_id(id_them)
+ if not ours or not them:
+ return
+
+ our_follow = join('follows', ours['email'])
+ with open(our_follow) as follow_file:
+ ourdata = json.load(follow_file)
+
+ ourdata.append(them['email'])
+ with open(our_follow, 'w') as follow_file:
+ json.dump(ourdata, follow_file)
+
+
+def unfollow_user(id_us, id_them):
+ """
+ Unfollows a user.
+ """
+ if not id_us or not id_them:
+ return
+ ours = find_user_by_id(id_us)
+ them = find_user_by_id(id_them)
+ if not ours or not them:
+ return
+
+ our_follow = join('follows', ours['email'])
+ with open(our_follow) as follow_file:
+ ourdata = json.load(follow_file)
+
+ if ourdata:
+ while them['email'] in ourdata:
+ ourdata.remove(them['email'])
+
+ with open(our_follow, 'w') as follow_file:
+ json.dump(ourdata, follow_file)
+
+
+def is_following(email_ours, email_them):
+ with open(join('follows', email_ours)) as follow_file:
+ ourdata = json.load(follow_file)
+
+ if email_them in ourdata:
+ return True
+
+ return False
+
+
def delete_user(user_id):
"""
Deletes a user and their messages directory.
"""
user = find_user_by_id(user_id)
rmtree(join('messages', user['email']), ignore_errors=True)
+ remove(join('follows', user['email']))
sql_delete_row_where('id', user_id, table='users')