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

View File

@@ -84,7 +84,15 @@ class DB:
(thread,) (thread,)
).fetchone() ).fetchone()
comments = db.execute(''' comments = db.execute('''
select comment_id, parent_id, author_id, name, text, create_time, modify_time select
comment_id,
parent_id,
author_id,
name,
text,
create_time,
modify_time,
hidden
from comments from comments
left join users left join users
on author_id = user_id on author_id = user_id
@@ -148,8 +156,21 @@ class DB:
union union
select comment_id from descendant_of, comments where id = parent_id select comment_id from descendant_of, comments where id = parent_id
) )
select id, parent_id, author_id, name, text, create_time, modify_time from descendant_of, comments, users select
where id = comment_id and user_id = author_id id,
parent_id,
author_id,
name,
text,
create_time,
modify_time,
hidden
from
descendant_of,
comments,
users
where id = comment_id
and user_id = author_id
''', ''',
(comment_id,) (comment_id,)
) )
@@ -509,6 +530,15 @@ class DB:
(hide, thread_id) (hide, thread_id)
) )
def set_comment_hidden(self, comment_id, hide):
return self.change_one('''
update comments
set hidden = ?
where comment_id = ?
''',
(hide, comment_id)
)
def change_one(self, query, values): def change_one(self, query, values):
db = self._db() db = self._db()
c = db.cursor() c = db.cursor()

43
main.py
View File

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

View File

@@ -1,10 +1,13 @@
{% from 'moderator.html' import moderate_comment with context -%}
{%- macro author(id, name, ctime, mtime) -%} {%- macro author(id, name, ctime, mtime) -%}
<i><a href="{{ url_for('user_info', user_id = id) }}">{{ name }}</a> - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %}</i> <i><a href="{{ url_for('user_info', user_id = id) }}">{{ name }}</a> - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %}</i>
{%- endmacro -%} {%- endmacro -%}
{%- macro comment_author(comment, thread_id, can_delete) -%} {%- macro comment_author(comment, thread_id, can_delete) -%}
<span class=small> <span class=small>
{{- author(comment.author_id, comment.author, comment.create_time, comment.modify_time) }} | {{- '[hidden]' if comment.hidden else '' }}
{{ author(comment.author_id, comment.author, comment.create_time, comment.modify_time) }} |
{# Suffixing a # prevents unnecessary reloads #} {# Suffixing a # prevents unnecessary reloads #}
<a href="{{ url_for('thread', thread_id = thread_id) }}#"> thread</a> <a href="{{ url_for('thread', thread_id = thread_id) }}#"> thread</a>
{%- if comment.parent_id is not none -%} {%- if comment.parent_id is not none -%}
@@ -15,6 +18,9 @@
{%- if can_delete -%} {%- if can_delete -%}
<a href="{{ url_for('confirm_delete_comment', comment_id = comment.id) }}"> delete</a> <a href="{{ url_for('confirm_delete_comment', comment_id = comment.id) }}"> delete</a>
{%- endif -%} {%- endif -%}
{%- if user.is_moderator() -%}
{{ moderate_comment(comment.id, comment.hidden) }}
{%- endif -%}
{%- endif -%} {%- endif -%}
</span> </span>
{%- endmacro -%} {%- endmacro -%}

View File

@@ -5,3 +5,11 @@
<input type=submit value="{{ 'Unhide' if hidden else 'Hide' }}"> <input type=submit value="{{ 'Unhide' if hidden else 'Hide' }}">
</form> </form>
{% endmacro %} {% endmacro %}
{% macro moderate_comment(id, hidden) %}
<form method=post action="{{ url_for('set_hide_comment', comment_id = id) }}" style=display:inline>
<input name=redirect value="{{ request.full_path }}" hidden>
<input name=hide value={{ 0 if hidden else 1 }} hidden>
<input type=submit value="{{ 'Unhide' if hidden else 'Hide' }}">
</form>
{% endmacro %}