threadstracker.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. from django.db.transaction import atomic
  2. from django.utils import timezone
  3. from misago.readtracker import categoriestracker, signals
  4. from misago.readtracker.dates import is_date_tracked
  5. from misago.readtracker.models import CategoryRead, ThreadRead
  6. __all__ = ['make_read_aware', 'read_thread']
  7. def make_read_aware(user, target):
  8. if hasattr(target, '__iter__'):
  9. make_threads_read_aware(user, target)
  10. else:
  11. make_thread_read_aware(user, target)
  12. def make_threads_read_aware(user, threads, category=None):
  13. if not threads:
  14. return None
  15. if user.is_anonymous():
  16. make_read(threads)
  17. return None
  18. if category:
  19. make_category_threads_read_aware(user, category, threads)
  20. else:
  21. make_categories_threads_read_aware(user, threads)
  22. def make_read(threads):
  23. for thread in threads:
  24. thread.unread_replies = 0
  25. thread.is_read = True
  26. thread.is_new = False
  27. def make_category_threads_read_aware(user, category, threads):
  28. if category.is_read:
  29. make_read(threads)
  30. else:
  31. threads_dict = {}
  32. for thread in threads:
  33. thread.is_read = not is_date_tracked(
  34. thread.last_post_on, user, category.last_read_on)
  35. thread.is_new = True
  36. if thread.is_read:
  37. thread.unread_replies = 0
  38. else:
  39. thread.unread_replies = thread.replies
  40. threads_dict[thread.pk] = thread
  41. if threads_dict:
  42. make_threads_dict_read_aware(user, threads_dict)
  43. def make_categories_threads_read_aware(user, threads):
  44. categories_cutoffs = fetch_categories_cutoffs_for_threads(user, threads)
  45. threads_dict = {}
  46. for thread in threads:
  47. category_cutoff = categories_cutoffs.get(thread.category_id)
  48. thread.is_read = not is_date_tracked(
  49. thread.last_post_on, user, category_cutoff)
  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_categories_cutoffs_for_threads(user, threads):
  59. categories = []
  60. for thread in threads:
  61. if thread.category_id not in categories:
  62. categories.append(thread.category_id)
  63. categories_dict = {}
  64. for record in user.categoryread_set.filter(category__in=categories):
  65. categories_dict[record.category_id] = record.last_read_on
  66. return categories_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. category_record = user.categoryread_set.get(
  92. category_id=thread.category_id)
  93. if thread.last_post_on > category_record.last_read_on:
  94. try:
  95. thread_record = user.threadread_set.get(thread=thread)
  96. thread.last_read_on = thread_record.last_read_on
  97. thread.is_new = False
  98. if thread.last_post_on <= thread_record.last_read_on:
  99. thread.is_read = True
  100. thread.read_record = thread_record
  101. except ThreadRead.DoesNotExist:
  102. pass
  103. else:
  104. thread.is_read = True
  105. thread.is_new = False
  106. except CategoryRead.DoesNotExist:
  107. categoriestracker.start_record(user, thread.category)
  108. def make_posts_read_aware(user, thread, posts):
  109. try:
  110. is_thread_read = thread.is_read
  111. except AttributeError:
  112. raise ValueError("thread passed make_posts_read_aware should be "
  113. "read aware too via make_thread_read_aware")
  114. if is_thread_read:
  115. for post in posts:
  116. post.is_read = True
  117. else:
  118. for post in posts:
  119. if is_date_tracked(post.posted_on, user):
  120. post.is_read = post.posted_on <= thread.last_read_on
  121. else:
  122. post.is_read = True
  123. def read_thread(user, thread, last_read_reply):
  124. if not thread.is_read:
  125. if thread.last_read_on < last_read_reply.posted_on:
  126. sync_record(user, thread, last_read_reply)
  127. @atomic
  128. def sync_record(user, thread, last_read_reply):
  129. notification_triggers = ['read_thread_%s' % thread.pk]
  130. read_replies = count_read_replies(user, thread, last_read_reply)
  131. if thread.read_record:
  132. thread.read_record.read_replies = read_replies
  133. thread.read_record.last_read_on = last_read_reply.posted_on
  134. thread.read_record.save(update_fields=['read_replies', 'last_read_on'])
  135. else:
  136. user.threadread_set.create(
  137. category=thread.category,
  138. thread=thread,
  139. read_replies=read_replies,
  140. last_read_on=last_read_reply.posted_on)
  141. signals.thread_tracked.send(sender=user, thread=thread)
  142. notification_triggers.append('see_thread_%s' % thread.pk)
  143. if last_read_reply.posted_on == thread.last_post_on:
  144. signals.thread_read.send(sender=user, thread=thread)
  145. categoriestracker.sync_record(user, thread.category)
  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