Implement comment hiding
Currently hides replies too, even comments from users who are logged in.
This commit is contained in:
36
db/sqlite.py
36
db/sqlite.py
@@ -84,7 +84,15 @@ class DB:
|
||||
(thread,)
|
||||
).fetchone()
|
||||
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
|
||||
left join users
|
||||
on author_id = user_id
|
||||
@@ -148,8 +156,21 @@ class DB:
|
||||
union
|
||||
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
|
||||
where id = comment_id and user_id = author_id
|
||||
select
|
||||
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,)
|
||||
)
|
||||
@@ -509,6 +530,15 @@ class DB:
|
||||
(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):
|
||||
db = self._db()
|
||||
c = db.cursor()
|
||||
|
||||
43
main.py
43
main.py
@@ -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)
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
{% from 'moderator.html' import moderate_comment with context -%}
|
||||
|
||||
{%- 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>
|
||||
{%- endmacro -%}
|
||||
|
||||
{%- macro comment_author(comment, thread_id, can_delete) -%}
|
||||
<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 #}
|
||||
<a href="{{ url_for('thread', thread_id = thread_id) }}#"> thread</a>
|
||||
{%- if comment.parent_id is not none -%}
|
||||
@@ -15,6 +18,9 @@
|
||||
{%- if can_delete -%}
|
||||
<a href="{{ url_for('confirm_delete_comment', comment_id = comment.id) }}"> delete</a>
|
||||
{%- endif -%}
|
||||
{%- if user.is_moderator() -%}
|
||||
{{ moderate_comment(comment.id, comment.hidden) }}
|
||||
{%- endif -%}
|
||||
{%- endif -%}
|
||||
</span>
|
||||
{%- endmacro -%}
|
||||
|
||||
@@ -5,3 +5,11 @@
|
||||
<input type=submit value="{{ 'Unhide' if hidden else 'Hide' }}">
|
||||
</form>
|
||||
{% 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 %}
|
||||
|
||||
Reference in New Issue
Block a user