diff --git a/db/sqlite.py b/db/sqlite.py index e8d9abc..3e2d2ee 100644 --- a/db/sqlite.py +++ b/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() diff --git a/main.py b/main.py index 07a3651..e27c6e8 100644 --- a/main.py +++ b/main.py @@ -63,14 +63,14 @@ def forum(forum_id): @app.route('/thread//') 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//') 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//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) diff --git a/templates/comment.html b/templates/comment.html index d67ca67..d4b4024 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -1,10 +1,13 @@ +{% from 'moderator.html' import moderate_comment with context -%} + {%- macro author(id, name, ctime, mtime) -%} {{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %} {%- endmacro -%} {%- macro comment_author(comment, thread_id, can_delete) -%} -{{- 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 #} thread {%- if comment.parent_id is not none -%} @@ -15,6 +18,9 @@ {%- if can_delete -%} delete {%- endif -%} +{%- if user.is_moderator() -%} +{{ moderate_comment(comment.id, comment.hidden) }} +{%- endif -%} {%- endif -%} {%- endmacro -%} diff --git a/templates/moderator.html b/templates/moderator.html index dd9ab2d..d6a0bc8 100644 --- a/templates/moderator.html +++ b/templates/moderator.html @@ -5,3 +5,11 @@ {% endmacro %} + +{% macro moderate_comment(id, hidden) %} +
+ + + +
+{% endmacro %}