Просмотр исходного кода

count unread private threads middleware

Rafał Pitoń 8 лет назад
Родитель
Сommit
1d7cbdea7e

+ 6 - 1
misago/readtracker/signals.py

@@ -1,5 +1,6 @@
 from django.dispatch import Signal, receiver
 
+from misago.categories.models import PRIVATE_THREADS_ROOT_NAME
 from misago.categories.signals import move_category_content
 from misago.threads.signals import move_thread
 
@@ -28,6 +29,10 @@ def delete_thread_tracker(sender, **kwargs):
 def decrease_unread_private_count(sender, **kwargs):
     user = sender
     thread = kwargs['thread']
-    if user.pk != thread.starter_id and user.unread_private_threads:
+
+    if thread.category.thread_type.root_name != PRIVATE_THREADS_ROOT_NAME:
+        return
+
+    if user.unread_private_threads:
         user.unread_private_threads -= 1
         user.save(update_fields=['unread_private_threads'])

+ 2 - 1
misago/threads/api/postendpoints/read.py

@@ -9,4 +9,5 @@ def post_read_endpoint(request, thread, post):
         read_thread(request.user, thread, post)
         if thread.subscription:
             thread.subscription.last_read_on = post.posted_on
-    return Response({'detail': 'ok'})
+        return Response({'thread_is_read': thread.last_post_on <= post.posted_on})
+    return Response({'thread_is_read': True})

+ 0 - 12
misago/threads/counts.py

@@ -1,12 +0,0 @@
-def sync_user_unread_private_threads_count(user):
-    if not user.sync_unread_private_threads:
-        return
-
-    # TODO: USE UTILS FROM PRIV THREADS TO COUNT STUFF
-    user.unread_private_threads = 0
-    user.sync_unread_private_threads = False
-
-    user.save(update_fields=[
-        'unread_private_threads',
-        'sync_unread_private_threads'
-    ])

+ 29 - 4
misago/threads/middleware.py

@@ -1,8 +1,33 @@
-from .counts import sync_user_unread_private_threads_count
+from misago.categories.models import Category
+
+from .models import Thread
+from .permissions import exclude_invisible_threads
+from .viewmodels import filter_read_threads_queryset
 
 
 class UnreadThreadsCountMiddleware(object):
     def process_request(self, request):
-        if request.user.is_authenticated():
-            if request.user.acl['can_use_private_threads']:
-                sync_user_unread_private_threads_count(request.user)
+        if request.user.is_anonymous():
+            return
+
+        if not request.user.acl['can_use_private_threads']:
+            return
+
+        participated_threads = request.user.threadparticipant_set.values('thread_id')
+
+        category = Category.objects.private_threads()
+        threads = Thread.objects.filter(
+            category=category,
+            id__in=participated_threads
+        )
+
+        new_threads = filter_read_threads_queryset(request.user, [category], 'new', threads)
+        unread_threads = filter_read_threads_queryset(request.user, [category], 'unread', threads)
+
+        request.user.unread_private_threads = new_threads.count() + unread_threads.count()
+        request.user.sync_unread_private_threads = False
+
+        request.user.save(update_fields=[
+            'unread_private_threads',
+            'sync_unread_private_threads',
+        ])

+ 2 - 2
misago/threads/tests/test_privatethread_reply_api.py

@@ -7,9 +7,9 @@ from ..models import ThreadParticipant
 from .test_privatethreads import PrivateThreadsTestCase
 
 
-class PrivateThreadreplyApiTestCase(PrivateThreadsTestCase):
+class PrivateThreadReplyApiTestCase(PrivateThreadsTestCase):
     def setUp(self):
-        super(PrivateThreadreplyApiTestCase, self).setUp()
+        super(PrivateThreadReplyApiTestCase, self).setUp()
 
         self.thread = testutils.post_thread(self.category, poster=self.user)
         self.api_link = self.thread.get_posts_api_url()

+ 59 - 0
misago/threads/tests/test_sync_unread_private_threads.py

@@ -0,0 +1,59 @@
+from django.contrib.auth import get_user_model
+
+from .. import testutils
+from ..models import ThreadParticipant
+from .test_privatethreads import PrivateThreadsTestCase
+
+
+class SyncUnreadPrivateThreadsTestCase(PrivateThreadsTestCase):
+    def setUp(self):
+        super(SyncUnreadPrivateThreadsTestCase, self).setUp()
+
+        User = get_user_model()
+        self.other_user = User.objects.create_user(
+            'BobBoberson', 'bob@boberson.com', 'pass123')
+
+        self.thread = testutils.post_thread(self.category, poster=self.user)
+
+        ThreadParticipant.objects.set_owner(self.thread, self.other_user)
+        ThreadParticipant.objects.add_participants(self.thread, [self.user])
+
+    def test_middleware_counts_new_thread(self):
+        """middleware counts new thread"""
+        self.user.sync_unread_private_threads = True
+        self.user.save()
+
+        response = self.client.get('/')
+        self.assertEqual(response.status_code, 200)
+
+        # user was resynced
+        self.reload_user()
+
+        self.assertFalse(self.user.sync_unread_private_threads)
+        self.assertEqual(self.user.unread_private_threads, 1)
+
+    def test_middleware_counts_unread_thread(self):
+        """middleware counts thread with unread reply, post read flags user for recount"""
+        self.user.sync_unread_private_threads = True
+        self.user.save()
+
+        self.client.post(self.thread.last_post.get_read_api_url())
+
+        # post read zeroed list of unread private threads
+        self.reload_user()
+        self.assertFalse(self.user.sync_unread_private_threads)
+        self.assertEqual(self.user.unread_private_threads, 0)
+
+        # reply to thread
+        testutils.reply_thread(self.thread)
+
+        self.user.sync_unread_private_threads = True
+        self.user.save()
+
+        # middleware did recount and accounted for new unread post
+        response = self.client.get('/')
+        self.assertEqual(response.status_code, 200)
+
+        self.reload_user()
+        self.assertFalse(self.user.sync_unread_private_threads)
+        self.assertEqual(self.user.unread_private_threads, 1)

+ 1 - 1
misago/threads/viewmodels/threads.py

@@ -19,7 +19,7 @@ from ..subscriptions import make_subscription_aware
 from ..utils import add_categories_to_items
 
 
-__all__ = ['ForumThreads', 'PrivateThreads']
+__all__ = ['ForumThreads', 'PrivateThreads', 'filter_read_threads_queryset']
 
 
 LISTS_NAMES = {