Browse Source

Fix unread/read tracker

sh4nks 10 years ago
parent
commit
6a95a9671c
2 changed files with 27 additions and 6 deletions
  1. 10 3
      flaskbb/forum/models.py
  2. 17 3
      flaskbb/utils/helpers.py

+ 10 - 3
flaskbb/forum/models.py

@@ -203,6 +203,8 @@ class Post(db.Model):
             self.topic_id = topic.id
             self.date_created = datetime.utcnow()
 
+            topic.last_updated = datetime.utcnow()
+
             # This needs to be done before I update the last_post_id.
             db.session.add(self)
             db.session.commit()
@@ -502,6 +504,9 @@ class Topic(db.Model):
         self.user_id = user.id
         self.username = user.username
 
+        # Set the last_updated time. Needed for the readstracker
+        self.last_updated = datetime.utcnow()
+
         # Insert and commit the topic
         db.session.add(self)
         db.session.commit()
@@ -676,7 +681,8 @@ class Forum(db.Model):
                               ForumsRead.user_id == user.id)).\
             filter(Topic.forum_id == self.id,
                    db.or_(TopicsRead.last_read == None,
-                          TopicsRead.last_read < Topic.last_updated)).\
+                          TopicsRead.last_read < Topic.last_updated),
+                   db.or_(ForumsRead.cleared < Topic.last_updated)).\
             count()
 
         # No unread topics available - trying to mark the forum as read
@@ -685,8 +691,9 @@ class Forum(db.Model):
             if forumsread and forumsread.last_read > topicsread.last_read:
                 return False
 
-            # ForumRead Entry exists - Updating it because a new post
-            # has been submitted that the user hasn't read.
+            # ForumRead Entry exists - Updating it because a new topic/post
+            # has been submitted and has read everything (obviously, else the
+            # unread_count would be useless).
             elif forumsread:
                 forumsread.last_read = datetime.utcnow()
                 forumsread.save()

+ 17 - 3
flaskbb/utils/helpers.py

@@ -142,6 +142,10 @@ def forum_is_unread(forum, forumsread, user):
     read_cutoff = datetime.utcnow() - timedelta(
         days=flaskbb_config["TRACKER_LENGTH"])
 
+    # disable tracker if read_cutoff is set to 0
+    if read_cutoff == 0:
+        return False
+
     # If there are no topics in the forum, mark it as read
     if forum and forum.topic_count == 0:
         return False
@@ -156,7 +160,7 @@ def forum_is_unread(forum, forumsread, user):
 
 
 def topic_is_unread(topic, topicsread, user, forumsread=None):
-    """Checks if a topic is unread
+    """Checks if a topic is unread.
 
     :param topic: The topic that should be checked if it is unread
 
@@ -176,17 +180,27 @@ def topic_is_unread(topic, topicsread, user, forumsread=None):
     read_cutoff = datetime.utcnow() - timedelta(
         days=flaskbb_config["TRACKER_LENGTH"])
 
+    # disable tracker if read_cutoff is set to 0
+    if read_cutoff == 0:
+        return False
+
+    # check read_cutoff
+    if topic.last_post.date_created < read_cutoff:
+        return False
+
     # topicsread is none if the user has marked the forum as read
     # or if he hasn't visited yet
-    if topic and not topicsread and topic.last_post.date_created > read_cutoff:
+    if topicsread is None:
 
         # user has cleared the forum sometime ago - check if there is a new post
         if forumsread and forumsread.cleared is not None:
             return forumsread.cleared < topic.last_post.date_created
 
-        # user hasn't read the topic yet, or it has been cleared
+        # user hasn't read the topic yet, or there is a new post since the user
+        # has marked the forum as read
         return True
 
+    # check if there is a new post since the user's last topic visit
     return topicsread.last_read < topic.last_post.date_created