threads.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from misago.readtracker.dates import is_date_tracked
  2. __all__ = ['make_threads_read_aware', 'make_threads_read']
  3. def make_threads_read_aware(user, threads):
  4. if user.is_anonymous():
  5. make_threads_read(threads)
  6. return None
  7. threads_dict = {}
  8. for thread in threads:
  9. thread.is_read = not is_date_tracked(thread.last_post_on)
  10. if thread.is_read:
  11. thread.unread_posts = 0
  12. else:
  13. thread.unread_posts = thread.replies
  14. threads_dict[thread.pk] = thread
  15. for record in user.threadread_set.filter(thread__in=threads_dict.keys()):
  16. if record.thread_id in threads_dict:
  17. thread = threads_dict[record.thread_id]
  18. thread.is_read = record.last_read_on >= thread.last_post_on
  19. if thread.is_read:
  20. thread.unread_posts = 0
  21. else:
  22. thread.unread_posts = thread.replies - record.read_replies
  23. def make_threads_read(threads):
  24. for thread in threads:
  25. thread.unread_posts = 0
  26. thread.is_read = True