threadstracker.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from django.db.transaction import atomic
  2. from django.utils import timezone
  3. from . import categoriestracker, signals
  4. from .dates import is_date_tracked
  5. from .models import CategoryRead, ThreadRead
  6. def make_read_aware(user, target):
  7. if hasattr(target, '__iter__'):
  8. make_threads_read_aware(user, target)
  9. else:
  10. make_thread_read_aware(user, target)
  11. def make_threads_read_aware(user, threads):
  12. if not threads:
  13. return
  14. if user.is_anonymous:
  15. make_read(threads)
  16. else:
  17. make_categories_threads_read_aware(user, threads)
  18. def make_read(threads):
  19. for thread in threads:
  20. thread.is_read = True
  21. thread.is_new = False
  22. def make_unread(threads):
  23. for thread in threads:
  24. thread.is_read = False
  25. thread.is_new = True
  26. def make_categories_threads_read_aware(user, threads):
  27. categories_cutoffs = fetch_categories_cutoffs_for_threads(user, threads)
  28. threads_dict = {}
  29. for thread in threads:
  30. category_cutoff = categories_cutoffs.get(thread.category_id)
  31. thread.is_read = not is_date_tracked(thread.last_post_on, user, category_cutoff)
  32. thread.is_new = not thread.is_read
  33. thread.last_read_on = user.joined_on
  34. if not thread.is_read:
  35. threads_dict[thread.pk] = thread
  36. if threads_dict:
  37. make_threads_dict_read_aware(user, threads_dict)
  38. def fetch_categories_cutoffs_for_threads(user, threads):
  39. categories = []
  40. for thread in threads:
  41. if thread.category_id not in categories:
  42. categories.append(thread.category_id)
  43. categories_dict = {}
  44. for record in user.categoryread_set.filter(category__in=categories):
  45. categories_dict[record.category_id] = record.last_read_on
  46. return categories_dict
  47. def make_threads_dict_read_aware(user, threads_dict):
  48. for record in user.threadread_set.filter(thread__in=threads_dict.keys()):
  49. if record.thread_id in threads_dict:
  50. thread = threads_dict[record.thread_id]
  51. thread.is_read = record.last_read_on >= thread.last_post_on
  52. thread.is_new = not thread.is_read
  53. thread.last_read_on = record.last_read_on
  54. def make_thread_read_aware(user, thread):
  55. thread.is_read = True
  56. thread.is_new = False
  57. thread.read_record = None
  58. if user.is_anonymous:
  59. thread.last_read_on = timezone.now()
  60. else:
  61. thread.last_read_on = user.joined_on
  62. if user.is_authenticated and is_date_tracked(thread.last_post_on, user):
  63. thread.is_read = False
  64. thread.is_new = True
  65. try:
  66. category_record = user.categoryread_set.get(category_id=thread.category_id)
  67. thread.last_read_on = category_record.last_read_on
  68. if thread.last_post_on > category_record.last_read_on:
  69. try:
  70. thread_record = user.threadread_set.get(thread=thread)
  71. thread.last_read_on = thread_record.last_read_on
  72. if thread.last_post_on <= thread_record.last_read_on:
  73. thread.is_new = False
  74. thread.is_read = True
  75. thread.read_record = thread_record
  76. except ThreadRead.DoesNotExist:
  77. pass
  78. else:
  79. thread.is_read = True
  80. thread.is_new = False
  81. except CategoryRead.DoesNotExist:
  82. categoriestracker.start_record(user, thread.category)
  83. def make_posts_read_aware(user, thread, posts):
  84. try:
  85. is_thread_read = thread.is_read
  86. except AttributeError:
  87. raise ValueError(
  88. "thread passed make_posts_read_aware should be "
  89. "made read aware via make_thread_read_aware"
  90. )
  91. if is_thread_read:
  92. for post in posts:
  93. post.is_read = True
  94. post.is_new = False
  95. else:
  96. for post in posts:
  97. if is_date_tracked(post.posted_on, user):
  98. post.is_read = post.posted_on <= thread.last_read_on
  99. else:
  100. post.is_read = True
  101. post.is_new = not post.is_read
  102. def read_thread(user, thread, last_read_reply):
  103. if not thread.is_read:
  104. if thread.last_read_on < last_read_reply.posted_on:
  105. sync_record(user, thread, last_read_reply)
  106. @atomic
  107. def sync_record(user, thread, last_read_reply):
  108. notification_triggers = ['read_thread_%s' % thread.pk]
  109. if thread.read_record:
  110. thread.read_record.last_read_on = last_read_reply.posted_on
  111. thread.read_record.save(update_fields=['last_read_on'])
  112. else:
  113. user.threadread_set.create(
  114. category=thread.category,
  115. thread=thread,
  116. last_read_on=last_read_reply.posted_on,
  117. )
  118. signals.thread_tracked.send(sender=user, thread=thread)
  119. notification_triggers.append('see_thread_%s' % thread.pk)
  120. if last_read_reply.posted_on == thread.last_post_on:
  121. signals.thread_read.send(sender=user, thread=thread)
  122. categoriestracker.sync_record(user, thread.category)