threads.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. from django.core.exceptions import PermissionDenied
  2. from django.db.models import Q
  3. from django.http import Http404
  4. from django.utils.translation import gettext as _
  5. from django.utils.translation import gettext_lazy
  6. from ...acl.objectacl import add_acl_to_obj
  7. from ...conf import settings
  8. from ...core.cursorpaginator import CursorPaginator
  9. from ...core.shortcuts import paginate, pagination_dict
  10. from ...readtracker import threadstracker
  11. from ...readtracker.dates import get_cutoff_date
  12. from ..models import Post, Thread
  13. from ..participants import make_participants_aware
  14. from ..permissions import exclude_invisible_posts, exclude_invisible_threads
  15. from ..serializers import ThreadsListSerializer
  16. from ..subscriptions import make_subscription_aware
  17. from ..utils import add_categories_to_items
  18. __all__ = ["ForumThreads", "PrivateThreads", "filter_read_threads_queryset"]
  19. LISTS_NAMES = {
  20. "all": None,
  21. "my": gettext_lazy("Your threads"),
  22. "new": gettext_lazy("New threads"),
  23. "unread": gettext_lazy("Unread threads"),
  24. "subscribed": gettext_lazy("Subscribed threads"),
  25. "unapproved": gettext_lazy("Unapproved content"),
  26. }
  27. LIST_DENIED_MESSAGES = {
  28. "my": gettext_lazy(
  29. "You have to sign in to see list of threads that you have started."
  30. ),
  31. "new": gettext_lazy("You have to sign in to see list of threads you haven't read."),
  32. "unread": gettext_lazy(
  33. "You have to sign in to see list of threads with new replies."
  34. ),
  35. "subscribed": gettext_lazy(
  36. "You have to sign in to see list of threads you are subscribing."
  37. ),
  38. "unapproved": gettext_lazy(
  39. "You have to sign in to see list of threads with unapproved posts."
  40. ),
  41. }
  42. class ViewModel:
  43. def __init__(self, request, category, list_type, page):
  44. self.allow_see_list(request, category, list_type)
  45. category_model = category.unwrap()
  46. base_queryset = self.get_base_queryset(request, category.categories, list_type)
  47. base_queryset = base_queryset.select_related("starter", "last_poster")
  48. threads_categories = [category_model] + category.subcategories
  49. threads_queryset = self.get_remaining_threads_queryset(
  50. base_queryset, category_model, threads_categories
  51. )
  52. paginator = CursorPaginator(
  53. threads_queryset,
  54. "-last_post_id",
  55. settings.MISAGO_THREADS_PER_PAGE
  56. )
  57. list_page = paginator.get(0)
  58. if list_page.start:
  59. threads = list(list_page.object_list)
  60. else:
  61. pinned_threads = list(
  62. self.get_pinned_threads(
  63. base_queryset, category_model, threads_categories
  64. )
  65. )
  66. threads = list(pinned_threads) + list(list_page.object_list)
  67. add_categories_to_items(category_model, category.categories, threads)
  68. add_acl_to_obj(request.user_acl, threads)
  69. make_subscription_aware(request.user, threads)
  70. if list_type in ("new", "unread"):
  71. # we already know all threads on list are unread
  72. for thread in threads:
  73. thread.is_read = False
  74. thread.is_new = True
  75. else:
  76. threadstracker.make_read_aware(request.user, request.user_acl, threads)
  77. self.filter_threads(request, threads)
  78. # set state on object for easy access from hooks
  79. self.category = category
  80. self.threads = threads
  81. self.list_type = list_type
  82. self.paginator = paginator
  83. def allow_see_list(self, request, category, list_type):
  84. if list_type not in LISTS_NAMES:
  85. raise Http404()
  86. if request.user.is_anonymous:
  87. if list_type in LIST_DENIED_MESSAGES:
  88. raise PermissionDenied(LIST_DENIED_MESSAGES[list_type])
  89. else:
  90. has_permission = request.user_acl["can_see_unapproved_content_lists"]
  91. if list_type == "unapproved" and not has_permission:
  92. raise PermissionDenied(
  93. _("You don't have permission to see unapproved content lists.")
  94. )
  95. def get_list_name(self, list_type):
  96. return LISTS_NAMES[list_type]
  97. def get_base_queryset(self, request, threads_categories, list_type):
  98. return get_threads_queryset(request, threads_categories, list_type).order_by(
  99. "-last_post_id"
  100. )
  101. def get_pinned_threads(self, queryset, category, threads_categories):
  102. return []
  103. def get_remaining_threads_queryset(self, queryset, category, threads_categories):
  104. return []
  105. def filter_threads(self, request, threads):
  106. pass # hook for custom thread types to add features to extend threads
  107. def get_frontend_context(self):
  108. context = {
  109. "THREADS": {
  110. "results": ThreadsListSerializer(self.threads, many=True).data,
  111. "subcategories": [c.pk for c in self.category.children],
  112. }
  113. }
  114. #context["THREADS"].update(self.paginator)
  115. return context
  116. def get_template_context(self):
  117. return {
  118. "list_name": self.get_list_name(self.list_type),
  119. "list_type": self.list_type,
  120. "threads": self.threads,
  121. "paginator": self.paginator,
  122. }
  123. class ForumThreads(ViewModel):
  124. def get_pinned_threads(self, queryset, category, threads_categories):
  125. if category.level:
  126. return list(queryset.filter(weight=2)) + list(
  127. queryset.filter(weight=1, category__in=threads_categories)
  128. )
  129. return queryset.filter(weight=2)
  130. def get_remaining_threads_queryset(self, queryset, category, threads_categories):
  131. if category.level:
  132. return queryset.filter(weight=0, category__in=threads_categories)
  133. return queryset.filter(weight__lt=2, category__in=threads_categories)
  134. class PrivateThreads(ViewModel):
  135. def get_base_queryset(self, request, threads_categories, list_type):
  136. queryset = super().get_base_queryset(request, threads_categories, list_type)
  137. # limit queryset to threads we are participant of
  138. participated_threads = request.user.threadparticipant_set.values("thread_id")
  139. if request.user_acl["can_moderate_private_threads"]:
  140. queryset = queryset.filter(
  141. Q(id__in=participated_threads) | Q(has_reported_posts=True)
  142. )
  143. else:
  144. queryset = queryset.filter(id__in=participated_threads)
  145. return queryset
  146. def get_remaining_threads_queryset(self, queryset, category, threads_categories):
  147. return queryset.filter(category__in=threads_categories)
  148. def filter_threads(self, request, threads):
  149. make_participants_aware(request.user, threads)
  150. def get_threads_queryset(request, categories, list_type):
  151. queryset = exclude_invisible_threads(request.user_acl, categories, Thread.objects)
  152. if list_type == "all":
  153. return queryset
  154. return filter_threads_queryset(request, categories, list_type, queryset)
  155. def filter_threads_queryset(request, categories, list_type, queryset):
  156. if list_type == "my":
  157. return queryset.filter(starter=request.user)
  158. if list_type == "subscribed":
  159. subscribed_threads = request.user.subscription_set.values("thread_id")
  160. return queryset.filter(id__in=subscribed_threads)
  161. if list_type == "unapproved":
  162. return queryset.filter(has_unapproved_posts=True)
  163. if list_type in ("new", "unread"):
  164. return filter_read_threads_queryset(request, categories, list_type, queryset)
  165. return queryset
  166. def filter_read_threads_queryset(request, categories, list_type, queryset):
  167. # grab cutoffs for categories
  168. user = request.user
  169. cutoff_date = get_cutoff_date(user)
  170. visible_posts = Post.objects.filter(posted_on__gt=cutoff_date)
  171. visible_posts = exclude_invisible_posts(request.user_acl, categories, visible_posts)
  172. queryset = queryset.filter(id__in=visible_posts.distinct().values("thread"))
  173. read_posts = visible_posts.filter(id__in=user.postread_set.values("post"))
  174. if list_type == "new":
  175. # new threads have no entry in reads table
  176. return queryset.exclude(id__in=read_posts.distinct().values("thread"))
  177. if list_type == "unread":
  178. # unread threads were read in past but have new posts
  179. unread_posts = visible_posts.exclude(id__in=user.postread_set.values("post"))
  180. queryset = queryset.filter(id__in=read_posts.distinct().values("thread"))
  181. queryset = queryset.filter(id__in=unread_posts.distinct().values("thread"))
  182. return queryset