merge.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. from rest_framework.response import Response
  2. from django.core.exceptions import PermissionDenied
  3. from django.http import Http404
  4. from django.utils.six import text_type
  5. from django.utils.translation import ugettext as _
  6. from django.utils.translation import ungettext
  7. from misago.acl import add_acl
  8. from misago.categories import THREADS_ROOT_NAME
  9. from misago.core.utils import clean_ids_list
  10. from misago.threads.events import record_event
  11. from misago.threads.models import Thread
  12. from misago.threads.moderation import threads as moderation
  13. from misago.threads.permissions import allow_merge_thread, can_reply_thread, can_see_thread
  14. from misago.threads.pollmergehandler import PollMergeHandler
  15. from misago.threads.serializers import MergeThreadSerializer, NewThreadSerializer, ThreadsListSerializer
  16. from misago.threads.threadtypes import trees_map
  17. from misago.threads.utils import get_thread_id_from_url
  18. MERGE_LIMIT = 20 # no more than 20 threads can be merged in single action
  19. class MergeError(Exception):
  20. def __init__(self, msg):
  21. self.msg = msg
  22. def thread_merge_endpoint(request, thread, viewmodel):
  23. allow_merge_thread(request.user, thread)
  24. serializer = MergeThreadSerializer(
  25. data=request.data,
  26. context={
  27. 'request': request,
  28. 'thread': thread,
  29. 'viewmodel': viewmodel,
  30. },
  31. )
  32. if not serializer.is_valid():
  33. if 'other_thread' in serializer.errors:
  34. errors = serializer.errors['other_thread']
  35. elif 'poll' in serializer.errors:
  36. errors = serializer.errors['poll']
  37. else:
  38. errors = list(serializer.errors.values())[0]
  39. return Response({'detail': errors[0]}, status=400)
  40. # interrupt merge with request for poll resolution?
  41. if serializer.validated_data.get('polls'):
  42. return Response({'polls': serializer.validated_data['polls']}, status=400)
  43. # merge polls
  44. other_thread = serializer.validated_data['other_thread']
  45. poll = serializer.validated_data.get('poll')
  46. if len(serializer.polls_handler.polls) == 1:
  47. poll.move(other_thread)
  48. elif serializer.polls_handler.is_merge_conflict():
  49. if poll and poll.thread_id != other_thread.id:
  50. other_thread.poll.delete()
  51. poll.move(other_thread)
  52. elif not poll:
  53. other_thread.poll.delete()
  54. # merge thread contents
  55. moderation.merge_thread(request, other_thread, thread)
  56. other_thread.synchronize()
  57. other_thread.save()
  58. other_thread.category.synchronize()
  59. other_thread.category.save()
  60. if thread.category != other_thread.category:
  61. thread.category.synchronize()
  62. thread.category.save()
  63. return Response({
  64. 'id': other_thread.pk,
  65. 'title': other_thread.title,
  66. 'url': other_thread.get_absolute_url(),
  67. })
  68. def threads_merge_endpoint(request):
  69. try:
  70. threads = clean_threads_for_merge(request)
  71. except MergeError as e:
  72. return Response({'detail': e.msg}, status=403)
  73. invalid_threads = []
  74. for thread in threads:
  75. try:
  76. allow_merge_thread(request.user, thread)
  77. except PermissionDenied as e:
  78. invalid_threads.append({
  79. 'id': thread.pk,
  80. 'title': thread.title,
  81. 'errors': [text_type(e)]
  82. })
  83. if invalid_threads:
  84. return Response(invalid_threads, status=403)
  85. serializer = NewThreadSerializer(
  86. data=request.data,
  87. context={'user': request.user},
  88. )
  89. if serializer.is_valid():
  90. polls_handler = PollMergeHandler(threads)
  91. if len(polls_handler.polls) == 1:
  92. poll = polls_handler.polls[0]
  93. elif polls_handler.is_merge_conflict():
  94. if 'poll' in request.data:
  95. polls_handler.set_resolution(request.data.get('poll'))
  96. if polls_handler.is_valid():
  97. poll = polls_handler.get_resolution()
  98. else:
  99. return Response({'detail': _("Invalid choice.")}, status=400)
  100. else:
  101. return Response({'polls': polls_handler.get_available_resolutions()}, status=400)
  102. else:
  103. poll = None
  104. new_thread = merge_threads(request, serializer.validated_data, threads, poll)
  105. return Response(ThreadsListSerializer(new_thread).data)
  106. else:
  107. return Response(serializer.errors, status=400)
  108. def clean_threads_for_merge(request):
  109. threads_ids = clean_ids_list(
  110. request.data.get('threads', []),
  111. _("One or more thread ids received were invalid."),
  112. )
  113. if len(threads_ids) < 2:
  114. raise MergeError(_("You have to select at least two threads to merge."))
  115. elif len(threads_ids) > MERGE_LIMIT:
  116. message = ungettext(
  117. "No more than %(limit)s thread can be merged at single time.",
  118. "No more than %(limit)s threads can be merged at single time.",
  119. MERGE_LIMIT,
  120. )
  121. raise MergeError(message % {'limit': MERGE_LIMIT})
  122. threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)
  123. threads_queryset = Thread.objects.filter(
  124. id__in=threads_ids,
  125. category__tree_id=threads_tree_id,
  126. ).select_related('category').order_by('-id')
  127. threads = []
  128. for thread in threads_queryset:
  129. add_acl(request.user, thread)
  130. if can_see_thread(request.user, thread):
  131. threads.append(thread)
  132. if len(threads) != len(threads_ids):
  133. raise MergeError(_("One or more threads to merge could not be found."))
  134. return threads
  135. def merge_threads(request, validated_data, threads, poll):
  136. new_thread = Thread(
  137. category=validated_data['category'],
  138. started_on=threads[0].started_on,
  139. last_post_on=threads[0].last_post_on,
  140. )
  141. new_thread.set_title(validated_data['title'])
  142. new_thread.save()
  143. if poll:
  144. poll.move(new_thread)
  145. categories = []
  146. for thread in threads:
  147. categories.append(thread.category)
  148. new_thread.merge(thread)
  149. thread.delete()
  150. record_event(
  151. request,
  152. new_thread,
  153. 'merged',
  154. {
  155. 'merged_thread': thread.title,
  156. },
  157. commit=False,
  158. )
  159. new_thread.synchronize()
  160. new_thread.save()
  161. if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
  162. moderation.pin_thread_globally(request, new_thread)
  163. elif validated_data.get('weight'):
  164. moderation.pin_thread_locally(request, new_thread)
  165. if validated_data.get('is_hidden', False):
  166. moderation.hide_thread(request, new_thread)
  167. if validated_data.get('is_closed', False):
  168. moderation.close_thread(request, new_thread)
  169. if new_thread.category not in categories:
  170. categories.append(new_thread.category)
  171. for category in categories:
  172. category.synchronize()
  173. category.save()
  174. # set extra attrs on thread for UI
  175. new_thread.is_read = False
  176. new_thread.subscription = None
  177. add_acl(request.user, new_thread)
  178. return new_thread