participants.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from misago.threads.models import ThreadParticipant
  2. def thread_has_participants(thread):
  3. return thread.threadparticipant_set.exists()
  4. def make_thread_participants_aware(user, thread):
  5. thread.participants_list = []
  6. thread.participant = None
  7. participants_qs = ThreadParticipant.objects.filter(thread=thread)
  8. participants_qs = participants_qs.select_related('user')
  9. for participant in participants_qs.order_by('-is_owner', 'user__slug'):
  10. participant.thread = thread
  11. thread.participants_list.append(participant)
  12. if participant.user == user:
  13. thread.participant = participant
  14. return thread.participants_list
  15. def set_thread_owner(thread, user):
  16. ThreadParticipant.objects.set_owner(thread, user)
  17. def set_user_unread_private_threads_sync(user):
  18. user.sync_unread_private_threads = True
  19. user.save(update_fields=['sync_unread_private_threads'])
  20. def add_participant(request, thread, user, is_owner=False):
  21. """
  22. Add participant to thread, set "recound private threads" flag on user,
  23. notify user about being added to thread and mail him about it
  24. """
  25. ThreadParticipant.objects.add_participant(thread, user, is_owner)
  26. set_user_unread_private_threads_sync(user)
  27. def add_owner(thread, user):
  28. """
  29. Add owner to thread, set "recound private threads" flag on user,
  30. notify user about being added to thread
  31. """
  32. ThreadParticipant.objects.add_participant(thread, user, True)
  33. set_user_unread_private_threads_sync(user)
  34. def remove_participant(thread, user):
  35. """
  36. Remove thread participant, set "recound private threads" flag on user
  37. """
  38. thread.threadparticipant_set.filter(user=user).delete()
  39. set_user_unread_private_threads_sync(user)