diff --git a/app/models.py b/app/models.py index 731c576..9ae3001 100644 --- a/app/models.py +++ b/app/models.py @@ -117,6 +117,24 @@ class Users(Model): def is_subscribed(self, thread_id): 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): table = 'topics' diff --git a/app/routes/threads.py b/app/routes/threads.py index 13af218..a0d0f9d 100644 --- a/app/routes/threads.py +++ b/app/routes/threads.py @@ -1,6 +1,6 @@ from flask import Blueprint, redirect, url_for, render_template, request, abort 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 ..util import get_form_checkbox, time_now import math @@ -59,6 +59,11 @@ def thread(thread_id, slug): abort(404) posts = thread.get_posts(PER_PAGE, page) 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( 'threads/thread.html', thread=thread, posts=posts, page=page, diff --git a/app/templates/common/topnav.html b/app/templates/common/topnav.html index f80a4fa..90b6d99 100644 --- a/app/templates/common/topnav.html +++ b/app/templates/common/topnav.html @@ -3,10 +3,11 @@ anti-social media {%- if is_logged_in() -%} {%- with user = get_active_user() -%} + {%- set uc = user.get_unread_count() -%}