add unread count to thread title in topic view

This commit is contained in:
2026-05-20 14:21:33 +03:00
parent daed16f099
commit 5db63d6907
2 changed files with 18 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
from flask import Blueprint, redirect, url_for, render_template, request, session, abort from flask import Blueprint, redirect, url_for, render_template, request, session, abort
from ..models import Topics, Threads from ..models import Topics, Threads, Subscriptions
from ..auth import get_active_user
import math import math
bp = Blueprint('topics', __name__, url_prefix = '/topics/') bp = Blueprint('topics', __name__, url_prefix = '/topics/')
@@ -33,7 +34,15 @@ def topic(topic_id, slug):
page = max(1, min(int(request.args.get('page', default=1)), page_count)) page = max(1, min(int(request.args.get('page', default=1)), page_count))
except ValueError: except ValueError:
abort(404) abort(404)
return render_template('topics/topic.html', topic=topic, threads=topic.get_threads(PER_PAGE, page, sort_by), sort_by=sort_by, page=page, page_count=page_count) threads = topic.get_threads(PER_PAGE, page, sort_by)
subscriptions = {}
user = get_active_user()
if user:
for thread in threads:
subscription = Subscriptions.find({'user_id': user.id, 'thread_id': thread['id']})
if subscription:
subscriptions[thread['id']] = subscription.get_unread_count()
return render_template('topics/topic.html', topic=topic, threads=threads, sort_by=sort_by, page=page, page_count=page_count, subscriptions=subscriptions)
@bp.get('/<int:topic_id>/feed.atom/') @bp.get('/<int:topic_id>/feed.atom/')
def feed(topic_id): def feed(topic_id):

View File

@@ -49,7 +49,13 @@
{%- for thread in threads -%} {%- for thread in threads -%}
<div class="topic-info plank"> <div class="topic-info plank">
<div class="title-container"> <div class="title-container">
<span class="info thread-title-counter"><a href="{{url_for('threads.thread_by_id', thread_id=thread.id)}}">{{thread.title}}</a> <span class="info thread-title-counter">
<ul class="horizontal">
<li><a href="{{url_for('threads.thread_by_id', thread_id=thread.id)}}">{{thread.title}}</a></li>
{%- if subscriptions[thread.id] -%}
<li>({{subscriptions[thread.id]}} unread)</li>
{%- endif -%}
</ul>
</span> </span>
<ul class="horizontal"> <ul class="horizontal">
{%- if thread.is_locked -%} {%- if thread.is_locked -%}