participants.py 4.6 KB

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