participants.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. from django.contrib.auth import get_user_model
  2. from django.utils.translation import ugettext as _
  3. from misago.core.mail import build_mail, send_messages
  4. from .events import record_event
  5. from .models import ThreadParticipant
  6. UserModel = get_user_model()
  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(users=None, participants=None, exclude_user=None):
  38. users_ids = []
  39. if users:
  40. users_ids += [u.pk for u in users]
  41. if participants:
  42. users_ids += [p.user_id for p in participants]
  43. if exclude_user:
  44. users_ids = filter(lambda u: u != exclude_user.pk, users_ids)
  45. if not users_ids:
  46. return
  47. UserModel.objects.filter(id__in=set(users_ids)).update(sync_unread_private_threads=True)
  48. def set_owner(thread, user):
  49. ThreadParticipant.objects.set_owner(thread, user)
  50. def change_owner(request, thread, user):
  51. ThreadParticipant.objects.set_owner(thread, user)
  52. set_users_unread_private_threads_sync(
  53. participants=thread.participants_list,
  54. exclude_user=request.user,
  55. )
  56. if thread.participant and thread.participant.is_owner:
  57. record_event(
  58. request,
  59. thread,
  60. 'changed_owner',
  61. {
  62. 'user': {
  63. 'username': user.username,
  64. 'url': user.get_absolute_url(),
  65. },
  66. },
  67. )
  68. else:
  69. record_event(request, thread, 'tookover')
  70. def add_participant(request, thread, user):
  71. """adds single participant to thread, registers this on the event"""
  72. add_participants(request, thread, [user])
  73. if request.user == user:
  74. record_event(request, thread, 'entered_thread')
  75. else:
  76. record_event(
  77. request,
  78. thread,
  79. 'added_participant',
  80. {
  81. 'user': {
  82. 'username': user.username,
  83. 'url': user.get_absolute_url(),
  84. },
  85. },
  86. )
  87. def add_participants(request, thread, users):
  88. """
  89. Add multiple participants to thread, set "recound private threads" flag on them
  90. notify them about being added to thread.
  91. """
  92. ThreadParticipant.objects.add_participants(thread, users)
  93. try:
  94. thread_participants = thread.participants_list
  95. except AttributeError:
  96. thread_participants = []
  97. set_users_unread_private_threads_sync(
  98. users=users,
  99. participants=thread_participants,
  100. exclude_user=request.user,
  101. )
  102. emails = []
  103. for user in users:
  104. if user != request.user:
  105. emails.append(build_noticiation_email(request, thread, user))
  106. if emails:
  107. send_messages(emails)
  108. def build_noticiation_email(request, thread, user):
  109. subject = _('%(user)s has invited you to participate in private thread "%(thread)s"')
  110. subject_formats = {
  111. 'thread': thread.title,
  112. 'user': request.user.username,
  113. }
  114. return build_mail(
  115. request, user, subject % subject_formats, 'misago/emails/privatethread/added', {
  116. 'thread': thread,
  117. }
  118. )
  119. def remove_participant(request, thread, user):
  120. """remove thread participant, set "recound private threads" flag on user"""
  121. removed_owner = False
  122. remaining_participants = []
  123. for participant in thread.participants_list:
  124. if participant.user == user:
  125. removed_owner = participant.is_owner
  126. else:
  127. remaining_participants.append(participant.user)
  128. set_users_unread_private_threads_sync(participants=thread.participants_list)
  129. if not remaining_participants:
  130. thread.delete()
  131. else:
  132. thread.threadparticipant_set.filter(user=user).delete()
  133. thread.subscription_set.filter(user=user).delete()
  134. if removed_owner:
  135. thread.is_closed = True # flag thread to close
  136. if request.user == user:
  137. event_type = 'owner_left'
  138. else:
  139. event_type = 'removed_owner'
  140. else:
  141. if request.user == user:
  142. event_type = 'participant_left'
  143. else:
  144. event_type = 'removed_participant'
  145. record_event(
  146. request,
  147. thread,
  148. event_type,
  149. {
  150. 'user': {
  151. 'username': user.username,
  152. 'url': user.get_absolute_url(),
  153. },
  154. },
  155. )