delete thread when no more posts remain

This commit is contained in:
2025-08-10 06:28:42 +03:00
parent c68ead85c0
commit cf2d605077

View File

@ -5,7 +5,7 @@ from .users import login_required, get_active_user
from ..lib.babycode import babycode_to_html from ..lib.babycode import babycode_to_html
from ..constants import InfoboxKind from ..constants import InfoboxKind
from ..db import db from ..db import db
from ..models import Posts, PostHistory, Threads from ..models import Posts, PostHistory, Threads, Topics
bp = Blueprint("posts", __name__, url_prefix = "/post") bp = Blueprint("posts", __name__, url_prefix = "/post")
@ -50,10 +50,25 @@ def delete(post_id):
post = Posts.find({'id': post_id}) post = Posts.find({'id': post_id})
thread = Threads.find({'id': post.thread_id}) thread = Threads.find({'id': post.thread_id})
user = get_active_user() user = get_active_user()
if not user:
return redirect(url_for('threads.thread', slug=thread.slug))
if user.is_mod() or post.user_id == user.id: if user.is_mod() or post.user_id == user.id:
post.delete() post.delete()
flash("Post deleted.", InfoboxKind.INFO)
post_count = Posts.count({
'thread_id': thread.id,
})
if post_count == 0:
topic = Topics.find({
'id': thread.topic_id,
})
thread.delete()
flash('Thread deleted.', InfoboxKind.INFO)
return redirect(url_for('topics.topic', slug=topic.slug))
flash('Post deleted.', InfoboxKind.INFO)
return redirect(url_for('threads.thread', slug=thread.slug)) return redirect(url_for('threads.thread', slug=thread.slug))