From 6dd9f5bf655f4ffcb8e1600ac353487eb9917fb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sun, 17 Aug 2025 15:15:06 +0300 Subject: [PATCH] add unread count to thread title in topic view and thread view --- app/models.py | 9 ++++++++- app/routes/threads.py | 3 +++ app/routes/topics.py | 19 ++++++++++++++++--- app/templates/threads/thread.html | 2 +- app/templates/topics/topic.html | 3 +++ 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/models.py b/app/models.py index f985636..79e7fdc 100644 --- a/app/models.py +++ b/app/models.py @@ -178,7 +178,7 @@ class Topics(Model): q = """ SELECT - threads.title, threads.slug, threads.created_at, threads.is_locked, threads.is_stickied, + threads.id, threads.title, threads.slug, threads.created_at, threads.is_locked, threads.is_stickied, users.username AS started_by, u.username AS latest_post_username, ph.content AS latest_post_content, @@ -251,6 +251,13 @@ class Avatars(Model): class Subscriptions(Model): table = "subscriptions" + def get_unread_count(self): + q = """SELECT COUNT(*) AS unread_count + FROM posts + LEFT JOIN subscriptions ON subscriptions.thread_id = posts.thread_id + WHERE subscriptions.user_id = ? AND posts.created_at > subscriptions.last_seen""" + return db.fetch_one(q, self.user_id)['unread_count'] + class APIRateLimits(Model): table = 'api_rate_limits' diff --git a/app/routes/threads.py b/app/routes/threads.py index ab16fb6..63c51e9 100644 --- a/app/routes/threads.py +++ b/app/routes/threads.py @@ -54,12 +54,14 @@ def thread(slug): other_topics = Topics.select() is_subscribed = False + unread_count = None if is_logged_in(): subscription = Subscriptions.find({ 'thread_id': thread.id, 'user_id': get_active_user().id, }) if subscription: + unread_count = subscription.get_unread_count() if int(posts[-1]['created_at']) > int(subscription.last_seen): subscription.update({ 'last_seen': int(posts[-1]['created_at']) @@ -76,6 +78,7 @@ def thread(slug): topics = other_topics, is_subscribed = is_subscribed, Reactions = Reactions, + unread_count = unread_count, ) diff --git a/app/routes/topics.py b/app/routes/topics.py index b13a899..0cced0e 100644 --- a/app/routes/topics.py +++ b/app/routes/topics.py @@ -1,8 +1,8 @@ from flask import ( Blueprint, render_template, request, redirect, url_for, flash, session ) -from .users import login_required, mod_only -from ..models import Users, Topics, Threads +from .users import login_required, mod_only, get_active_user, is_logged_in +from ..models import Users, Topics, Threads, Subscriptions from ..constants import InfoboxKind from slugify import slugify import time @@ -62,9 +62,22 @@ def topic(slug): page_count = max(math.ceil(threads_count / THREADS_PER_PAGE), 1) page = max(1, min(int(request.args.get('page', default=1)), page_count)) + threads_list = target_topic.get_threads(THREADS_PER_PAGE, page, sort_by) + subscriptions = {} + if is_logged_in(): + for thread in threads_list: + subscription = Subscriptions.find({ + 'user_id': get_active_user().id, + 'thread_id': thread['id'], + }) + if subscription: + print(subscription.get_unread_count()) + subscriptions[subscription.id] = subscription.get_unread_count() + return render_template( "topics/topic.html", - threads_list = target_topic.get_threads(THREADS_PER_PAGE, page, sort_by), + threads_list = threads_list, + subscriptions = subscriptions, topic = target_topic, current_page = page, page_count = page_count diff --git a/app/templates/threads/thread.html b/app/templates/threads/thread.html index b75a99b..e8e7c69 100644 --- a/app/templates/threads/thread.html +++ b/app/templates/threads/thread.html @@ -12,7 +12,7 @@ {% endif %}