participants.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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(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. record_event(request, thread, 'added_participant', {
  56. 'user': {
  57. 'username': user.username,
  58. 'url': user.get_absolute_url(),
  59. }
  60. })
  61. def add_participants(request, thread, users):
  62. """
  63. Add multiple participants to thread, set "recound private threads" flag on them
  64. notify them about being added to thread
  65. """
  66. ThreadParticipant.objects.add_participants(thread, users)
  67. try:
  68. thread_participants = thread.participants_list
  69. except AttributeError:
  70. thread_participants = []
  71. set_users_unread_private_threads_sync(users=users, participants=thread_participants)
  72. emails = []
  73. for user in users:
  74. emails.append(build_noticiation_email(request, thread, user))
  75. send_messages(emails)
  76. def build_noticiation_email(request, thread, user):
  77. subject = _('%(user)s has invited you to participate in private thread "%(thread)s"')
  78. subject_formats = {
  79. 'thread': thread.title,
  80. 'user': request.user.username
  81. }
  82. return build_mail(
  83. request,
  84. user,
  85. subject % subject_formats,
  86. 'misago/emails/privatethread/added',
  87. {
  88. 'thread': thread
  89. }
  90. )
  91. def remove_participant(request, thread, user):
  92. """
  93. Remove thread participant, set "recound private threads" flag on user
  94. """
  95. removed_owner = False
  96. remaining_participants = []
  97. for participant in thread.participants_list:
  98. if participant.user == user:
  99. removed_owner = participant.is_owner
  100. else:
  101. remaining_participants.append(participant.user)
  102. set_users_unread_private_threads_sync(participants=thread.participants_list)
  103. if not remaining_participants:
  104. thread.delete()
  105. else:
  106. thread.threadparticipant_set.filter(user=user).delete()
  107. if removed_owner:
  108. thread.is_closed = True # flag thread to close
  109. if request.user == user:
  110. event_type = 'owner_left'
  111. else:
  112. event_type = 'removed_owner'
  113. else:
  114. if request.user == user:
  115. event_type = 'participant_left'
  116. else:
  117. event_type = 'removed_participant'
  118. record_event(request, thread, event_type, {
  119. 'user': {
  120. 'username': user.username,
  121. 'url': user.get_absolute_url(),
  122. }
  123. })