participants.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. def has_participants(thread):
  7. return thread.threadparticipant_set.exists()
  8. def make_participants_aware(user, thread):
  9. thread.participants_list = []
  10. thread.participant = None
  11. participants_qs = ThreadParticipant.objects.filter(thread=thread)
  12. participants_qs = participants_qs.select_related('user')
  13. for participant in participants_qs.order_by('-is_owner', 'user__slug'):
  14. participant.thread = thread
  15. thread.participants_list.append(participant)
  16. if participant.user == user:
  17. thread.participant = participant
  18. return thread.participants_list
  19. def set_owner(thread, user):
  20. """
  21. Remove user's ownership over thread
  22. """
  23. ThreadParticipant.objects.set_owner(thread, user)
  24. def set_users_unread_private_threads_sync(users):
  25. User = get_user_model()
  26. User.objects.filter(id__in=[u.pk for u in users]).update(
  27. sync_unread_private_threads=True
  28. )
  29. def add_participant(request, thread, user):
  30. """
  31. Adds single participant to thread, registers this on the event
  32. """
  33. add_participants(request, thread, [user])
  34. record_event(request, thread, 'added_participant', {
  35. 'user': {
  36. 'username': user.username,
  37. 'url': user.get_absolute_url(),
  38. }
  39. })
  40. def add_participants(request, thread, users):
  41. """
  42. Add multiple participants to thread, set "recound private threads" flag on them
  43. notify them about being added to thread
  44. """
  45. ThreadParticipant.objects.add_participants(thread, users)
  46. set_users_unread_private_threads_sync(users)
  47. emails = []
  48. for user in users:
  49. emails.append(build_noticiation_email(request, thread, user))
  50. send_messages(emails)
  51. def build_noticiation_email(request, thread, user):
  52. subject = _('%(user)s has invited you to participate in private thread "%(thread)s"')
  53. subject_formats = {
  54. 'thread': thread.title,
  55. 'user': request.user.username
  56. }
  57. return build_mail(
  58. request,
  59. user,
  60. subject % subject_formats,
  61. 'misago/emails/privatethread/added',
  62. {
  63. 'thread': thread
  64. }
  65. )
  66. def remove_participant(request, thread, user):
  67. """
  68. Remove thread participant, set "recound private threads" flag on user
  69. """
  70. removed_owner = False
  71. remaining_participants = []
  72. for participant in thread.participants_list:
  73. if participant.user == user:
  74. removed_owner = participant.is_owner
  75. else:
  76. remaining_participants.append(participant.user)
  77. set_users_unread_private_threads_sync(remaining_participants + [user])
  78. if not remaining_participants:
  79. thread.delete()
  80. else:
  81. thread.threadparticipant_set.filter(user=user).delete()
  82. if removed_owner:
  83. thread.is_closed = True # flag thread to close
  84. if request.user == user:
  85. event_type = 'owner_left'
  86. else:
  87. event_type = 'removed_owner'
  88. else:
  89. if request.user == user:
  90. event_type = 'participant_left'
  91. else:
  92. event_type = 'removed_participant'
  93. record_event(request, thread, event_type, {
  94. 'user': {
  95. 'username': user.username,
  96. 'url': user.get_absolute_url(),
  97. }
  98. })