Implement comment hiding

Currently hides replies too, even comments from users who are logged in.
This commit is contained in:
David Hoppenbrouwers
2022-10-12 22:02:31 +02:00
parent 8f53d143db
commit 8e54c95c40
4 changed files with 78 additions and 17 deletions

43
main.py
View File

@@ -63,14 +63,14 @@ def forum(forum_id):
@app.route('/thread/<int:thread_id>/')
def thread(thread_id):
user_id = session.get('user_id')
user = get_user()
title, text, author, author_id, create_time, modify_time, comments, hidden = db.get_thread(thread_id)
comments = create_comment_tree(comments)
comments = create_comment_tree(comments, user)
return render_template(
'thread.html',
title = title,
config = config,
user = get_user(),
user = user,
text = text,
author = author,
author_id = author_id,
@@ -83,8 +83,9 @@ def thread(thread_id):
@app.route('/comment/<int:comment_id>/')
def comment(comment_id):
user = get_user()
thread_id, parent_id, title, comments = db.get_subcomments(comment_id)
comments = create_comment_tree(comments)
comments = create_comment_tree(comments, user)
reply_comment, = comments
comments = reply_comment.children
reply_comment.children = []
@@ -92,7 +93,7 @@ def comment(comment_id):
'comments.html',
title = title,
config = config,
user = get_user(),
user = user,
reply_comment = reply_comment,
comments = comments,
parent_id = parent_id,
@@ -583,7 +584,6 @@ def set_hide_thread(thread_id):
return user
try:
print(request.form['hide'])
hide = request.form['hide'] != '0'
hide_str = 'Hidden' if hide else 'Unhidden'
if db.set_thread_hidden(thread_id, hide):
@@ -595,6 +595,24 @@ def set_hide_thread(thread_id):
return redirect(request.form['redirect'])
@app.route('/comment/<int:comment_id>/hide/', methods = ['POST'])
def set_hide_comment(comment_id):
chk, user = _moderator_check()
if not chk:
return user
try:
hide = request.form['hide'] != '0'
hide_str = 'Hidden' if hide else 'Unhidden'
if db.set_comment_hidden(comment_id, hide):
flash(f'{hide_str} comment', 'success')
else:
flash(f'Failed to {hide_str.lower()} comment', 'error')
except Exception as e:
flash(str(e), 'error')
return redirect(request.form['redirect'])
# TODO can probably be a static-esque page, maybe?
@app.route('/help/')
def help():
@@ -622,7 +640,7 @@ def _admin_check():
class Comment:
def __init__(self, id, author_id, author, text, create_time, modify_time, parent_id):
def __init__(self, id, parent_id, author_id, author, text, create_time, modify_time, hidden):
self.id = id
self.author_id = author_id
self.author = author
@@ -631,18 +649,17 @@ class Comment:
self.create_time = create_time
self.modify_time = modify_time
self.parent_id = parent_id
self.hidden = hidden
def create_comment_tree(comments):
def create_comment_tree(comments, user):
start = time.time();
# Collect comments first, then build the tree in case we encounter a child before a parent
comment_map = {
comment_id: Comment(comment_id, author_id, author, text, create_time, modify_time, parent_id)
for comment_id, parent_id, author_id, author, text, create_time, modify_time
in comments
}
comment_map = { v[0]: Comment(*v) for v in comments }
root = []
# Build tree
for comment in comment_map.values():
if comment.hidden and not user.is_moderator():
continue
parent = comment_map.get(comment.parent_id)
if parent is not None:
parent.children.append(comment)