threadstracker.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. from django.db.transaction import atomic
  2. from django.utils import timezone
  3. from misago.notifications import read_user_notifications
  4. from misago.readtracker import forumstracker, signals
  5. from misago.readtracker.dates import is_date_tracked
  6. from misago.readtracker.models import ForumRead, ThreadRead
  7. __all__ = ['make_read_aware', 'read_thread']
  8. def make_read_aware(user, target):
  9. if hasattr(target, '__iter__'):
  10. make_threads_read_aware(user, target)
  11. else:
  12. make_thread_read_aware(user, target)
  13. def make_threads_read_aware(user, threads, forum=None):
  14. if not threads:
  15. return None
  16. if user.is_anonymous():
  17. make_read(threads)
  18. return None
  19. if forum:
  20. make_forum_threads_read_aware(user, forum, threads)
  21. else:
  22. make_forums_threads_read_aware(user, threads)
  23. def make_read(threads):
  24. for thread in threads:
  25. thread.unread_replies = 0
  26. thread.is_read = True
  27. thread.is_new = False
  28. def make_forum_threads_read_aware(user, forum, threads):
  29. if forum.is_read:
  30. make_read(threads)
  31. else:
  32. threads_dict = {}
  33. for thread in threads:
  34. thread.is_read = not is_date_tracked(
  35. thread.last_post_on, user, forum.last_read_on)
  36. thread.is_new = True
  37. if thread.is_read:
  38. thread.unread_replies = 0
  39. else:
  40. thread.unread_replies = thread.replies
  41. threads_dict[thread.pk] = thread
  42. if threads_dict:
  43. make_threads_dict_read_aware(user, threads_dict)
  44. def make_forums_threads_read_aware(user, threads):
  45. forums_cutoffs = fetch_forums_cutoffs_for_threads(user, threads)
  46. threads_dict = {}
  47. for thread in threads:
  48. thread.is_read = not is_date_tracked(
  49. thread.last_post_on, user, forums_cutoffs.get(thread.forum_id))
  50. thread.is_new = True
  51. if thread.is_read:
  52. thread.unread_replies = 0
  53. else:
  54. thread.unread_replies = thread.replies
  55. threads_dict[thread.pk] = thread
  56. if threads_dict:
  57. make_threads_dict_read_aware(user, threads_dict)
  58. def fetch_forums_cutoffs_for_threads(user, threads):
  59. forums = []
  60. for thread in threads:
  61. if thread.forum_id not in forums:
  62. forums.append(thread.forum_id)
  63. forums_dict = {}
  64. for record in user.forumread_set.filter(forum__in=forums):
  65. forums_dict[record.forum_id] = record.last_read_on
  66. return forums_dict
  67. def make_threads_dict_read_aware(user, threads_dict):
  68. for record in user.threadread_set.filter(thread__in=threads_dict.keys()):
  69. if record.thread_id in threads_dict:
  70. thread = threads_dict[record.thread_id]
  71. thread.is_new = False
  72. thread.is_read = record.last_read_on >= thread.last_post_on
  73. if thread.is_read:
  74. thread.unread_replies = 0
  75. else:
  76. thread.unread_replies = thread.replies - record.read_replies
  77. if thread.unread_replies < 1:
  78. thread.unread_replies = 1
  79. def make_thread_read_aware(user, thread):
  80. thread.is_read = True
  81. thread.is_new = False
  82. thread.read_record = None
  83. if user.is_anonymous():
  84. thread.last_read_on = timezone.now()
  85. else:
  86. thread.last_read_on = user.reads_cutoff
  87. if user.is_authenticated() and is_date_tracked(thread.last_post_on, user):
  88. thread.is_read = False
  89. thread.is_new = True
  90. try:
  91. forum_record = user.forumread_set.get(forum_id=thread.forum_id)
  92. if thread.last_post_on > forum_record.last_read_on:
  93. try:
  94. thread_record = user.threadread_set.get(thread=thread)
  95. thread.last_read_on = thread_record.last_read_on
  96. thread.is_new = False
  97. if thread.last_post_on <= thread_record.last_read_on:
  98. thread.is_read = True
  99. thread.read_record = thread_record
  100. except ThreadRead.DoesNotExist:
  101. pass
  102. else:
  103. thread.is_read = True
  104. thread.is_new = False
  105. except ForumRead.DoesNotExist:
  106. forumstracker.start_record(user, thread.forum)
  107. def make_posts_read_aware(user, thread, posts):
  108. try:
  109. is_thread_read = thread.is_read
  110. except AttributeError:
  111. raise ValueError("thread passed make_posts_read_aware should be "
  112. "read aware too via make_thread_read_aware")
  113. if is_thread_read:
  114. for post in posts:
  115. post.is_read = True
  116. else:
  117. for post in posts:
  118. if is_date_tracked(post.posted_on, user):
  119. post.is_read = post.posted_on <= thread.last_read_on
  120. else:
  121. post.is_read = True
  122. def read_thread(user, thread, last_read_reply):
  123. if not thread.is_read:
  124. if thread.last_read_on < last_read_reply.posted_on:
  125. sync_record(user, thread, last_read_reply)
  126. @atomic
  127. def sync_record(user, thread, last_read_reply):
  128. notification_triggers = ['read_thread_%s' % thread.pk]
  129. read_replies = count_read_replies(user, thread, last_read_reply)
  130. if thread.read_record:
  131. thread.read_record.read_replies = read_replies
  132. thread.read_record.last_read_on = last_read_reply.posted_on
  133. thread.read_record.save(update_fields=['read_replies', 'last_read_on'])
  134. else:
  135. user.threadread_set.create(
  136. forum=thread.forum,
  137. thread=thread,
  138. read_replies=read_replies,
  139. last_read_on=last_read_reply.posted_on)
  140. signals.thread_tracked.send(sender=user, thread=thread)
  141. notification_triggers.append('see_thread_%s' % thread.pk)
  142. if last_read_reply.posted_on == thread.last_post_on:
  143. signals.thread_read.send(sender=user, thread=thread)
  144. forumstracker.sync_record(user, thread.forum)
  145. read_user_notifications(user, notification_triggers, False)
  146. def count_read_replies(user, thread, last_read_reply):
  147. last_reply_date = last_read_reply.posted_on
  148. queryset = thread.post_set.filter(posted_on__lte=last_reply_date)
  149. queryset = queryset.filter(is_moderated=False)
  150. return queryset.count() - 1 # - starters post