Browse Source

cut dropped features in reads tracker, optimised new/unread lists a little, brought in placeholder for unread threads count

Rafał Pitoń 9 years ago
parent
commit
43f451e32b

+ 1 - 1
misago/conf/defaults.py

@@ -125,7 +125,7 @@ MIDDLEWARE_CLASSES = (
     'misago.core.middleware.exceptionhandler.ExceptionHandlerMiddleware',
     'misago.core.middleware.exceptionhandler.ExceptionHandlerMiddleware',
     'misago.users.middleware.OnlineTrackerMiddleware',
     'misago.users.middleware.OnlineTrackerMiddleware',
     'misago.admin.middleware.AdminAuthMiddleware',
     'misago.admin.middleware.AdminAuthMiddleware',
-    #'misago.threads.middleware.UnreadThreadsCountMiddleware',
+    'misago.threads.middleware.UnreadThreadsCountMiddleware',
     'misago.core.middleware.threadstore.ThreadStoreMiddleware',
     'misago.core.middleware.threadstore.ThreadStoreMiddleware',
 )
 )
 
 

+ 0 - 1
misago/readtracker/migrations/0001_initial.py

@@ -29,7 +29,6 @@ class Migration(migrations.Migration):
             name='ThreadRead',
             name='ThreadRead',
             fields=[
             fields=[
                 ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                 ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
-                ('read_replies', models.PositiveIntegerField(default=0)),
                 ('last_read_on', models.DateTimeField()),
                 ('last_read_on', models.DateTimeField()),
                 ('category', models.ForeignKey(to='misago_categories.Category')),
                 ('category', models.ForeignKey(to='misago_categories.Category')),
                 ('thread', models.ForeignKey(to='misago_threads.Thread')),
                 ('thread', models.ForeignKey(to='misago_threads.Thread')),

+ 0 - 1
misago/readtracker/models.py

@@ -15,5 +15,4 @@ class ThreadRead(models.Model):
     user = models.ForeignKey(settings.AUTH_USER_MODEL)
     user = models.ForeignKey(settings.AUTH_USER_MODEL)
     category = models.ForeignKey('misago_categories.Category')
     category = models.ForeignKey('misago_categories.Category')
     thread = models.ForeignKey('misago_threads.Thread')
     thread = models.ForeignKey('misago_threads.Thread')
-    read_replies =  models.PositiveIntegerField(default=0)
     last_read_on = models.DateTimeField()
     last_read_on = models.DateTimeField()

+ 8 - 22
misago/readtracker/threadstracker.py

@@ -28,11 +28,16 @@ def make_threads_read_aware(user, threads):
 
 
 def make_read(threads):
 def make_read(threads):
     for thread in threads:
     for thread in threads:
-        thread.unread_replies = 0
         thread.is_read = True
         thread.is_read = True
         thread.is_new = False
         thread.is_new = False
 
 
 
 
+def make_unread(threads):
+    for thread in threads:
+        thread.is_read = False
+        thread.is_new = True
+
+
 def make_categories_threads_read_aware(user, threads):
 def make_categories_threads_read_aware(user, threads):
     categories_cutoffs = fetch_categories_cutoffs_for_threads(user, threads)
     categories_cutoffs = fetch_categories_cutoffs_for_threads(user, threads)
 
 
@@ -43,10 +48,7 @@ def make_categories_threads_read_aware(user, threads):
             thread.last_post_on, user, category_cutoff)
             thread.last_post_on, user, category_cutoff)
         thread.is_new = True
         thread.is_new = True
 
 
-        if thread.is_read:
-            thread.unread_replies = 0
-        else:
-            thread.unread_replies = thread.replies
+        if not thread.is_read:
             threads_dict[thread.pk] = thread
             threads_dict[thread.pk] = thread
 
 
     if threads_dict:
     if threads_dict:
@@ -71,12 +73,6 @@ def make_threads_dict_read_aware(user, threads_dict):
             thread = threads_dict[record.thread_id]
             thread = threads_dict[record.thread_id]
             thread.is_new = False
             thread.is_new = False
             thread.is_read = record.last_read_on >= thread.last_post_on
             thread.is_read = record.last_read_on >= thread.last_post_on
-            if thread.is_read:
-                thread.unread_replies = 0
-            else:
-                thread.unread_replies = thread.replies - record.read_replies
-                if thread.unread_replies < 1:
-                    thread.unread_replies = 1
 
 
 
 
 def make_thread_read_aware(user, thread):
 def make_thread_read_aware(user, thread):
@@ -142,16 +138,13 @@ def read_thread(user, thread, last_read_reply):
 def sync_record(user, thread, last_read_reply):
 def sync_record(user, thread, last_read_reply):
     notification_triggers = ['read_thread_%s' % thread.pk]
     notification_triggers = ['read_thread_%s' % thread.pk]
 
 
-    read_replies = count_read_replies(user, thread, last_read_reply)
     if thread.read_record:
     if thread.read_record:
-        thread.read_record.read_replies = read_replies
         thread.read_record.last_read_on = last_read_reply.posted_on
         thread.read_record.last_read_on = last_read_reply.posted_on
-        thread.read_record.save(update_fields=['read_replies', 'last_read_on'])
+        thread.read_record.save(update_fields=['last_read_on'])
     else:
     else:
         user.threadread_set.create(
         user.threadread_set.create(
             category=thread.category,
             category=thread.category,
             thread=thread,
             thread=thread,
-            read_replies=read_replies,
             last_read_on=last_read_reply.posted_on)
             last_read_on=last_read_reply.posted_on)
         signals.thread_tracked.send(sender=user, thread=thread)
         signals.thread_tracked.send(sender=user, thread=thread)
         notification_triggers.append('see_thread_%s' % thread.pk)
         notification_triggers.append('see_thread_%s' % thread.pk)
@@ -159,10 +152,3 @@ def sync_record(user, thread, last_read_reply):
     if last_read_reply.posted_on == thread.last_post_on:
     if last_read_reply.posted_on == thread.last_post_on:
         signals.thread_read.send(sender=user, thread=thread)
         signals.thread_read.send(sender=user, thread=thread)
         categoriestracker.sync_record(user, thread.category)
         categoriestracker.sync_record(user, thread.category)
-
-
-def count_read_replies(user, thread, last_read_reply):
-    last_reply_date = last_read_reply.posted_on
-    queryset = thread.post_set.filter(posted_on__lte=last_reply_date)
-    queryset = queryset.filter(is_moderated=False)
-    return queryset.count() - 1 # - starters post

+ 12 - 0
misago/threads/counts.py

@@ -0,0 +1,12 @@
+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'
+    ])

+ 1 - 16
misago/threads/middleware.py

@@ -1,23 +1,8 @@
-from time import time
-
-from django.conf import settings
-
-from misago.threads.counts import (ModeratedCount, NewThreadsCount,
-                                   UnreadThreadsCount,
-                                   sync_user_unread_private_threads_count)
+from misago.threads.counts import sync_user_unread_private_threads_count
 
 
 
 
 class UnreadThreadsCountMiddleware(object):
 class UnreadThreadsCountMiddleware(object):
     def process_request(self, request):
     def process_request(self, request):
         if request.user.is_authenticated():
         if request.user.is_authenticated():
-            if request.user.acl['can_review_moderated_content']:
-                request.user.moderated_content = ModeratedCount(
-                    request.user, request.session)
-            request.user.new_threads = NewThreadsCount(
-                request.user, request.session)
-            request.user.unread_threads = UnreadThreadsCount(
-                request.user, request.session)
-
             if request.user.acl['can_use_private_threads']:
             if request.user.acl['can_use_private_threads']:
-                # special case: count unread threads
                 sync_user_unread_private_threads_count(request.user)
                 sync_user_unread_private_threads_count(request.user)

+ 11 - 2
misago/threads/views/threadslist.py

@@ -51,7 +51,9 @@ def filter_threads_queryset(user, categories, list_type, queryset):
         if list_type == 'new':
         if list_type == 'new':
             # new threads have no entry in reads table
             # new threads have no entry in reads table
             # AND were started after cutoff date
             # AND were started after cutoff date
-            read_threads = user.threadread_set.values('thread_id')
+            read_threads = user.threadread_set.filter(
+                category__in=categories
+            ).values('thread_id')
 
 
             if categories_dict:
             if categories_dict:
                 condition = Q(
                 condition = Q(
@@ -75,6 +77,7 @@ def filter_threads_queryset(user, categories, list_type, queryset):
             # unread threads were read in past but have new posts
             # unread threads were read in past but have new posts
             # after cutoff date
             # after cutoff date
             read_threads = user.threadread_set.filter(
             read_threads = user.threadread_set.filter(
+                category__in=categories,
                 thread__last_post_on__gt=cutoff_date,
                 thread__last_post_on__gt=cutoff_date,
                 last_read_on__lt=F('thread__last_post_on')
                 last_read_on__lt=F('thread__last_post_on')
             ).values('thread_id')
             ).values('thread_id')
@@ -166,7 +169,13 @@ class BaseList(View):
         page = paginate(queryset, page, 24, 6)
         page = paginate(queryset, page, 24, 6)
         paginator = pagination_dict(page)
         paginator = pagination_dict(page)
 
 
-        threadstracker.make_threads_read_aware(request.user, page.object_list)
+        if list_type in ('new', 'unread'):
+            """we already know all threads on list are unread"""
+            threadstracker.make_unread(page.object_list)
+        else:
+            threadstracker.make_threads_read_aware(
+                request.user, page.object_list)
+
         add_categories_to_threads(categories, page.object_list)
         add_categories_to_threads(categories, page.object_list)
 
 
         visible_subcategories = []
         visible_subcategories = []

+ 0 - 2
misago/users/migrations/0001_initial.py

@@ -65,8 +65,6 @@ class Migration(migrations.Migration):
                 ('last_posted_on', models.DateTimeField(null=True, blank=True)),
                 ('last_posted_on', models.DateTimeField(null=True, blank=True)),
                 ('last_searched_on', models.DateTimeField(null=True, blank=True)),
                 ('last_searched_on', models.DateTimeField(null=True, blank=True)),
                 ('reads_cutoff', models.DateTimeField(default=django.utils.timezone.now)),
                 ('reads_cutoff', models.DateTimeField(default=django.utils.timezone.now)),
-                ('new_threads_cutoff', models.DateTimeField(default=django.utils.timezone.now)),
-                ('unread_threads_cutoff', models.DateTimeField(default=django.utils.timezone.now))
             ],
             ],
             options={
             options={
                 'abstract': False,
                 'abstract': False,

+ 0 - 2
misago/users/models/user.py

@@ -235,8 +235,6 @@ class User(AbstractBaseUser, PermissionsMixin):
     last_searched_on = models.DateTimeField(null=True, blank=True)
     last_searched_on = models.DateTimeField(null=True, blank=True)
 
 
     reads_cutoff = models.DateTimeField(default=timezone.now)
     reads_cutoff = models.DateTimeField(default=timezone.now)
-    new_threads_cutoff = models.DateTimeField(default=timezone.now)
-    unread_threads_cutoff = models.DateTimeField(default=timezone.now)
 
 
     USERNAME_FIELD = 'slug'
     USERNAME_FIELD = 'slug'
     REQUIRED_FIELDS = ['email']
     REQUIRED_FIELDS = ['email']