participants.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. from django.contrib.auth import get_user_model
  2. from django.utils.translation import ugettext as _
  3. from misago.core import deprecations
  4. from misago.core.mail import build_mail, send_messages
  5. from .events import record_event
  6. from .models import ThreadParticipant
  7. def has_participants(thread):
  8. return thread.threadparticipant_set.exists()
  9. def make_participants_aware(user, target):
  10. if hasattr(target, '__iter__'):
  11. make_threads_participants_aware(user, target)
  12. else:
  13. make_thread_participants_aware(user, target)
  14. def make_threads_participants_aware(user, threads):
  15. threads_dict = {}
  16. for thread in threads:
  17. thread.participant = None
  18. threads_dict[thread.pk] = thread
  19. participants_qs = ThreadParticipant.objects.filter(
  20. user=user,
  21. thread_id__in=threads_dict.keys()
  22. )
  23. for participant in participants_qs:
  24. participant.user = user
  25. threads_dict[participant.thread_id].participant = participant
  26. def make_thread_participants_aware(user, thread):
  27. thread.participants_list = []
  28. thread.participant = None
  29. participants_qs = ThreadParticipant.objects.filter(thread=thread)
  30. participants_qs = participants_qs.select_related('user')
  31. for participant in participants_qs.order_by('-is_owner', 'user__slug'):
  32. participant.thread = thread
  33. thread.participants_list.append(participant)
  34. if participant.user == user:
  35. thread.participant = participant
  36. return thread.participants_list
  37. def set_users_unread_private_threads_sync(
  38. users=None, participants=None, exclude_user=None):
  39. users_ids = []
  40. if users:
  41. users_ids += [u.pk for u in users]
  42. if participants:
  43. users_ids += [p.user_id for p in participants]
  44. if exclude_user:
  45. users_ids = filter(lambda u: u != exclude_user.pk, users_ids)
  46. if not users_ids:
  47. return
  48. User = get_user_model()
  49. User.objects.filter(id__in=set(users_ids)).update(
  50. sync_unread_private_threads=True
  51. )
  52. def set_owner(thread, user):
  53. """
  54. Set user as thread's owner
  55. """
  56. ThreadParticipant.objects.set_owner(thread, user)
  57. def change_owner(request, thread, user):
  58. """
  59. Replace thread's owner with other
  60. """
  61. ThreadParticipant.objects.set_owner(thread, user)
  62. set_users_unread_private_threads_sync(
  63. participants=thread.participants_list,
  64. exclude_user=request.user
  65. )
  66. if thread.participant and thread.participant.is_owner:
  67. record_event(request, thread, 'changed_owner', {
  68. 'user': {
  69. 'username': user.username,
  70. 'url': user.get_absolute_url(),
  71. }
  72. })
  73. else:
  74. record_event(request, thread, 'tookover')
  75. def add_participant(request, thread, user):
  76. """
  77. Adds single participant to thread, registers this on the event
  78. """
  79. add_participants(request, thread, [user])
  80. if request.user == user:
  81. record_event(request, thread, 'entered_thread')
  82. else:
  83. record_event(request, thread, 'added_participant', {
  84. 'user': {
  85. 'username': user.username,
  86. 'url': user.get_absolute_url(),
  87. }
  88. })
  89. def add_participants(request, thread, users):
  90. """
  91. Add multiple participants to thread, set "recound private threads" flag on them
  92. notify them about being added to thread
  93. """
  94. ThreadParticipant.objects.add_participants(thread, users)
  95. try:
  96. thread_participants = thread.participants_list
  97. except AttributeError:
  98. thread_participants = []
  99. set_users_unread_private_threads_sync(
  100. users=users,
  101. participants=thread_participants,
  102. exclude_user=request.user
  103. )
  104. emails = []
  105. for user in users:
  106. if user != request.user:
  107. emails.append(build_noticiation_email(request, thread, user))
  108. if emails:
  109. send_messages(emails)
  110. def build_noticiation_email(request, thread, user):
  111. subject = _('%(user)s has invited you to participate in private thread "%(thread)s"')
  112. subject_formats = {
  113. 'thread': thread.title,
  114. 'user': request.user.username
  115. }
  116. return build_mail(
  117. request,
  118. user,
  119. subject % subject_formats,
  120. 'misago/emails/privatethread/added',
  121. {
  122. 'thread': thread
  123. }
  124. )
  125. def remove_participant(request, thread, user):
  126. """
  127. Remove thread participant, set "recound private threads" flag on user
  128. """
  129. removed_owner = False
  130. remaining_participants = []
  131. for participant in thread.participants_list:
  132. if participant.user == user:
  133. removed_owner = participant.is_owner
  134. else:
  135. remaining_participants.append(participant.user)
  136. set_users_unread_private_threads_sync(participants=thread.participants_list)
  137. if not remaining_participants:
  138. thread.delete()
  139. else:
  140. thread.threadparticipant_set.filter(user=user).delete()
  141. if removed_owner:
  142. thread.is_closed = True # flag thread to close
  143. if request.user == user:
  144. event_type = 'owner_left'
  145. else:
  146. event_type = 'removed_owner'
  147. else:
  148. if request.user == user:
  149. event_type = 'participant_left'
  150. else:
  151. event_type = 'removed_participant'
  152. record_event(request, thread, event_type, {
  153. 'user': {
  154. 'username': user.username,
  155. 'url': user.get_absolute_url(),
  156. }
  157. })