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

Fixed unread private threads counting.

Rafał Pitoń 10 лет назад
Родитель
Сommit
700cc26fe8

+ 11 - 2
misago/forums/models.py

@@ -22,10 +22,19 @@ FORUMS_TREE_ID = 1
 
 class ForumManager(TreeManager):
     def private_threads(self):
-        return self.get(special_role='private_threads')
+        return self.get_special('private_threads')
 
     def root_category(self):
-        return self.get(special_role='root_category')
+        return self.get_special('root_category')
+
+    def get_special(self, special_role):
+        cache_name = '%s_%s' % (CACHE_NAME, special_role)
+
+        special_forum = cache.get(cache_name, 'nada')
+        if special_forum == 'nada':
+            special_forum = self.get(special_role='root_category')
+            cache.set(cache_name, special_forum)
+        return special_forum
 
     def all_forums(self, include_root=False):
         qs = self.filter(tree_id=FORUMS_TREE_ID)

+ 2 - 1
misago/readtracker/signals.py

@@ -41,7 +41,8 @@ def decrease_unread_count(sender, **kwargs):
 @receiver(thread_read)
 def decrease_unread_private_count(sender, **kwargs):
     user = sender
-    if user.unread_private_threads:
+    thread = kwargs['thread']
+    if user.pk != thread.starter_id and user.unread_private_threads:
         user.unread_private_threads -= 1
         user.save(update_fields=['unread_private_threads'])
 

+ 6 - 3
misago/threads/counts.py

@@ -4,6 +4,8 @@ from django.conf import settings
 from django.db.models import F
 from django.dispatch import receiver
 
+from misago.forums.models import Forum
+
 from misago.threads.views.moderatedcontent import ModeratedContent
 from misago.threads.views.newthreads import NewThreads
 from misago.threads.views.unreadthreads import UnreadThreads
@@ -89,15 +91,16 @@ class UnreadThreadsCount(BaseCounter):
 
 
 def sync_user_unread_private_threads_count(user):
-    if not user.sync_unread_private_threads:
+    if False and not user.sync_unread_private_threads:
         return
 
     threads_qs = PrivateThreads(user).get_queryset()
 
     all_threads_count = threads_qs.count()
 
-    read_qs = threads_qs.filter(threadread__user=user)
-    read_qs = read_qs.filter(threadread__last_read_on__gte=F('last_post_on'))
+    read_qs = user.threadread_set.filter(forum=Forum.objects.private_threads())
+    read_qs = read_qs.filter(last_read_on__gte=F('thread__last_post_on'))
+    read_qs = threads_qs.filter(id__in=read_qs.values('thread_id'))
     read_threads_count = read_qs.count()
 
     user.unread_private_threads = all_threads_count - read_threads_count

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

@@ -124,7 +124,7 @@ class TestSyncUnreadPrivateThreadsCount(AuthenticatedUserTestCase):
         self.assertEqual(self.user.unread_private_threads, 0)
 
     def test_user_with_new_thread(self):
-        """user has one new private thred"""
+        """user has one new private thread"""
         for i in range(5):
             # post 5 invisible threads
             testutils.post_thread(
@@ -138,7 +138,7 @@ class TestSyncUnreadPrivateThreadsCount(AuthenticatedUserTestCase):
         self.assertEqual(self.user.unread_private_threads, 1)
 
     def test_user_with_new_thread(self):
-        """user has one unread private thred"""
+        """user has one unread private thread"""
         for i in range(5):
             # post 5 invisible threads
             testutils.post_thread(