participants.py 5.4 KB

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