only show reply form if the user can reply

This commit is contained in:
2026-04-25 23:56:46 +03:00
parent 286a3641eb
commit 612d69c157
5 changed files with 9 additions and 10 deletions

View File

@@ -61,14 +61,14 @@ class Users(Model):
subscriptions.user_id = ?""" subscriptions.user_id = ?"""
return db.query(q, self.id) return db.query(q, self.id)
def can_post_to_topic(self, topic): def can_post_to_thread_or_topic(self, thread_or_topic):
if self.is_guest(): if self.is_guest():
return False return False
if self.is_mod(): if self.is_mod():
return True return True
if topic['is_locked']: if thread_or_topic['is_locked']:
return False return False
return True return True

View File

@@ -58,15 +58,14 @@ def thread(thread_id, slug):
abort(404) abort(404)
return render_template('threads/thread.html', thread=thread, posts=thread.get_posts(PER_PAGE, page), page=page, page_count=page_count, topic=topic, started_by=started_by, topics=Topics.get_list(), Reactions=Reactions) return render_template('threads/thread.html', thread=thread, posts=thread.get_posts(PER_PAGE, page), page=page, page_count=page_count, topic=topic, started_by=started_by, topics=Topics.get_list(), Reactions=Reactions)
@bp.post('/<int:thread_id>/reply/') @bp.post('/<int:thread_id>/')
@login_required @login_required
def reply(thread_id): def reply(thread_id):
user = get_active_user() user = get_active_user()
thread = Threads.find({'id': thread_id}) thread = Threads.find({'id': thread_id})
if not thread: if not thread:
abort(404) abort(404)
if thread.locked() and not user.is_mod(): if not user.can_post_to_thread_or_topic(thread):
# TODO: flash
return redirect(url_for('.thread_by_id', thread_id=thread_id)) return redirect(url_for('.thread_by_id', thread_id=thread_id))
post = Posts.new(user.id, thread.id, request.form.get('babycode_content')) post = Posts.new(user.id, thread.id, request.form.get('babycode_content'))
return redirect(url_for('.thread_by_id', thread_id=thread_id, after=post.id, _anchor=f'post-{post.id}')) return redirect(url_for('.thread_by_id', thread_id=thread_id, after=post.id, _anchor=f'post-{post.id}'))
@@ -110,7 +109,7 @@ def new_post():
abort(404) abort(404)
user = get_active_user() user = get_active_user()
if not user.can_post_to_topic(topic): if not user.can_post_to_thread_or_topic(topic):
abort(404) abort(404)
title = request.form.get('title') title = request.form.get('title')

View File

@@ -7,7 +7,7 @@
<label for="topic">Topic</label> <label for="topic">Topic</label>
<select name="topic_id" id="topic" autocomplete="off"> <select name="topic_id" id="topic" autocomplete="off">
{%- for topic in topics -%} {%- for topic in topics -%}
<option value="{{topic.id}}" {{'selected' if selected_topic == topic.id else ''}} {{'disabled' if not get_active_user().can_post_to_topic(topic) else ''}}>{{topic.name}}{{ ' (locked)' if topic.locked() else ''}}</option> <option value="{{topic.id}}" {{'selected' if selected_topic == topic.id else ''}} {{'disabled' if not get_active_user().can_post_to_thread_or_topic(topic) else ''}}>{{topic.name}}{{ ' (locked)' if topic.locked() else ''}}</option>
{%- endfor -%} {%- endfor -%}
</select> </select>
<label for="title">Title</label> <label for="title">Title</label>

View File

@@ -70,7 +70,7 @@
{{- pager(page, page_count) -}} {{- pager(page, page_count) -}}
</fieldset> </fieldset>
</div> </div>
{%- if is_logged_in() -%} {%- if is_logged_in() and get_active_user().can_post_to_thread_or_topic(thread) -%}
<form action="{{url_for('threads.reply', thread_id=thread.id)}}" method="POST" class="plank post-edit-form"> <form action="{{url_for('threads.reply', thread_id=thread.id)}}" method="POST" class="plank post-edit-form">
<h2 class="info">Reply to "{{thread.title}}"</h2> <h2 class="info">Reply to "{{thread.title}}"</h2>
{{- babycode_editor_component() -}} {{- babycode_editor_component() -}}

View File

@@ -14,7 +14,7 @@
{%- call() subheader(('Threads in "%s"' % topic.name), td) -%} {%- call() subheader(('Threads in "%s"' % topic.name), td) -%}
<fieldset class="plank even no-shadow minimal thread-actions"> <fieldset class="plank even no-shadow minimal thread-actions">
<legend>Actions</legend> <legend>Actions</legend>
{%- if is_logged_in() and get_active_user().can_post_to_topic(topic) -%} {%- if is_logged_in() and get_active_user().can_post_to_thread_or_topic(topic) -%}
<a href="{{url_for('threads.new', topic_id=topic.id)}}" class="linkbutton">New thread</a> <a href="{{url_for('threads.new', topic_id=topic.id)}}" class="linkbutton">New thread</a>
{%- endif -%} {%- endif -%}
<a href="{{url_for('topics.feed', topic_id=topic.id)}}" class="linkbutton rss">Subscribe via RSS</a> <a href="{{url_for('topics.feed', topic_id=topic.id)}}" class="linkbutton rss">Subscribe via RSS</a>
@@ -44,7 +44,7 @@
{%- endif -%} {%- endif -%}
{%- endcall -%} {%- endcall -%}
{%- if threads | length == 0 -%} {%- if threads | length == 0 -%}
<div class="plank"><p>There are no threads in this topic yet.{%- if is_logged_in() and get_active_user().can_post_to_topic(topic) %} Be the first to start a discussion!{%- endif -%}</p></div> <div class="plank"><p>There are no threads in this topic yet.{%- if is_logged_in() and get_active_user().can_post_to_thread_or_topic(topic) %} Be the first to start a discussion!{%- endif -%}</p></div>
{%- endif -%} {%- endif -%}
{%- for thread in threads -%} {%- for thread in threads -%}
<div class="topic-info plank"> <div class="topic-info plank">