add unread count to topnav and update subscription on thread view

This commit is contained in:
2026-05-20 14:07:15 +03:00
parent a5a3565496
commit daed16f099
3 changed files with 26 additions and 2 deletions

View File

@@ -117,6 +117,24 @@ class Users(Model):
def is_subscribed(self, thread_id): def is_subscribed(self, thread_id):
return Subscriptions.count({'user_id': self.id, 'thread_id': thread_id}) > 0 return Subscriptions.count({'user_id': self.id, 'thread_id': thread_id}) > 0
def get_unread_count(self):
q = """
SELECT
COUNT(posts.id) as c
FROM
subscriptions
JOIN
threads ON subscriptions.thread_id = threads.id
JOIN
posts ON threads.id = posts.thread_id
WHERE
subscriptions.user_id = ?
AND
posts.created_at > subscriptions.last_seen
"""
res = db.fetch_one(q, self.id)
return res["c"] or 0
class Topics(Model): class Topics(Model):
table = 'topics' table = 'topics'

View File

@@ -1,6 +1,6 @@
from flask import Blueprint, redirect, url_for, render_template, request, abort from flask import Blueprint, redirect, url_for, render_template, request, abort
from functools import wraps from functools import wraps
from ..auth import login_required, get_active_user from ..auth import login_required, get_active_user, is_logged_in
from ..models import Threads, Posts, Topics, Users, Reactions, Subscriptions from ..models import Threads, Posts, Topics, Users, Reactions, Subscriptions
from ..util import get_form_checkbox, time_now from ..util import get_form_checkbox, time_now
import math import math
@@ -59,6 +59,11 @@ def thread(thread_id, slug):
abort(404) abort(404)
posts = thread.get_posts(PER_PAGE, page) posts = thread.get_posts(PER_PAGE, page)
last_post = posts[-1] last_post = posts[-1]
user = get_active_user()
if user:
subscription = Subscriptions.find({'user_id': user.id, 'thread_id': thread.id})
if subscription:
subscription.update({'last_seen': last_post['created_at']})
return render_template( return render_template(
'threads/thread.html', thread=thread, 'threads/thread.html', thread=thread,
posts=posts, page=page, posts=posts, page=page,

View File

@@ -3,10 +3,11 @@
<span>anti-social media</span> <span>anti-social media</span>
{%- if is_logged_in() -%} {%- if is_logged_in() -%}
{%- with user = get_active_user() -%} {%- with user = get_active_user() -%}
{%- set uc = user.get_unread_count() -%}
<ul class="horizontal wrap"> <ul class="horizontal wrap">
<li class="mobile-fill-flex">Welcome, <a href="{{url_for('users.user_page', username=user.username)}}">{{ user.get_readable_name() }}</a></li> <li class="mobile-fill-flex">Welcome, <a href="{{url_for('users.user_page', username=user.username)}}">{{ user.get_readable_name() }}</a></li>
<li><a class="linkbutton" href="{{url_for('users.settings', username=user.username)}}">Settings</a></li> <li><a class="linkbutton" href="{{url_for('users.settings', username=user.username)}}">Settings</a></li>
<li><a class="linkbutton" href="{{url_for('users.inbox', username=user.username)}}">Inbox</a></li> <li><a class="linkbutton" href="{{url_for('users.inbox', username=user.username)}}">Inbox{{' (%s)' % uc if uc else ''}}</a></li>
<li><a class="linkbutton" href="{{url_for('users.bookmarks', username=user.username)}}">Bookmarks</a></li> <li><a class="linkbutton" href="{{url_for('users.bookmarks', username=user.username)}}">Bookmarks</a></li>
{% if user.is_mod() -%} {% if user.is_mod() -%}
<li><a class="linkbutton" href="{{url_for('mod.index')}}">Moderation</a></li> <li><a class="linkbutton" href="{{url_for('mod.index')}}">Moderation</a></li>