add subscribing and unsubscribing, add post editing

This commit is contained in:
2026-04-28 19:03:29 +03:00
parent f3acf64e6d
commit ff2c6606f8
8 changed files with 219 additions and 33 deletions

View File

@@ -1,7 +1,8 @@
from flask import Blueprint, redirect, url_for, render_template, request, abort
from functools import wraps
from ..auth import login_required, get_active_user
from ..models import Threads, Posts, Topics, Users, Reactions
from ..models import Threads, Posts, Topics, Users, Reactions, Subscriptions
from ..util import get_form_checkbox, time_now
import math
bp = Blueprint('threads', __name__, url_prefix='/threads/')
@@ -56,7 +57,15 @@ def thread(thread_id, slug):
page = max(1, min(int(request.args.get('page', default=1)), page_count))
except ValueError:
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)
posts = thread.get_posts(PER_PAGE, page)
last_post = posts[-1]
return render_template(
'threads/thread.html', thread=thread,
posts=posts, page=page,
page_count=page_count, topic=topic,
started_by=started_by, topics=Topics.get_list(),
Reactions=Reactions, last_post=last_post
)
@bp.post('/<int:thread_id>/')
@login_required
@@ -68,6 +77,13 @@ def reply(thread_id):
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'))
if get_form_checkbox('subscribe'):
if not Subscriptions.find({'user_id': user.id, 'thread_id': thread.id}):
Subscriptions.create({
'user_id': user.id,
'thread_id': thread.id,
'last_seen': time_now(),
})
return redirect(url_for('.thread_by_id', thread_id=thread_id, after=post.id, _anchor=f'post-{post.id}'))
@bp.get('/<int:thread_id>/edit/')
@@ -82,6 +98,48 @@ def edit(thread_id):
def edit_post(thread_id):
return 'stub'
@bp.post('/<int:thread_id>/subscribe/')
@login_required
def subscribe(thread_id):
thread = Threads.find({'id': thread_id})
if not thread:
abort(404)
user = get_active_user()
last_post_id = request.form.get('last_post_id', None)
if last_post_id is None:
abort(400)
if user.is_subscribed(thread_id):
return redirect(url_for('.thread_by_id', thread_id=thread_id, after=last_post_id))
Subscriptions.create({
'user_id': user.id,
'thread_id': thread_id,
'last_seen': request.form.get('last_post_timestamp', time_now())
})
return redirect(url_for('.thread_by_id', thread_id=thread_id, after=last_post_id))
@bp.post('/<int:thread_id>/unsubscribe/')
@login_required
def unsubscribe(thread_id):
thread = Threads.find({'id': thread_id})
if not thread:
abort(404)
user = get_active_user()
last_post_id = request.form.get('last_post_id', None)
if last_post_id is None:
abort(400)
subscription = Subscriptions.find({'user_id': user.id, 'thread_id': thread_id})
if not subscription:
return redirect(url_for('.thread_by_id', thread_id=thread_id, after=last_post_id))
subscription.delete()
return redirect(url_for('.thread_by_id', thread_id=thread_id, after=last_post_id))
@bp.get('/<int:thread_id>/feed.atom/')
def feed(thread_id):
return 'stub'