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 = ?"""
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():
return False
if self.is_mod():
return True
if topic['is_locked']:
if thread_or_topic['is_locked']:
return False
return True

View File

@@ -58,15 +58,14 @@ def thread(thread_id, slug):
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)
@bp.post('/<int:thread_id>/reply/')
@bp.post('/<int:thread_id>/')
@login_required
def reply(thread_id):
user = get_active_user()
thread = Threads.find({'id': thread_id})
if not thread:
abort(404)
if thread.locked() and not user.is_mod():
# TODO: flash
if not user.can_post_to_thread_or_topic(thread):
return redirect(url_for('.thread_by_id', thread_id=thread_id))
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}'))
@@ -110,7 +109,7 @@ def new_post():
abort(404)
user = get_active_user()
if not user.can_post_to_topic(topic):
if not user.can_post_to_thread_or_topic(topic):
abort(404)
title = request.form.get('title')

View File

@@ -7,7 +7,7 @@
<label for="topic">Topic</label>
<select name="topic_id" id="topic" autocomplete="off">
{%- 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 -%}
</select>
<label for="title">Title</label>

View File

@@ -70,7 +70,7 @@
{{- pager(page, page_count) -}}
</fieldset>
</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">
<h2 class="info">Reply to "{{thread.title}}"</h2>
{{- babycode_editor_component() -}}

View File

@@ -14,7 +14,7 @@
{%- call() subheader(('Threads in "%s"' % topic.name), td) -%}
<fieldset class="plank even no-shadow minimal thread-actions">
<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>
{%- endif -%}
<a href="{{url_for('topics.feed', topic_id=topic.id)}}" class="linkbutton rss">Subscribe via RSS</a>
@@ -44,7 +44,7 @@
{%- endif -%}
{%- endcall -%}
{%- 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 -%}
{%- for thread in threads -%}
<div class="topic-info plank">