participants.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from misago.threads.models import ThreadParticipant
  2. def make_thread_participants_aware(user, thread):
  3. thread.participants_list = []
  4. thread.participant = None
  5. participants_qs = ThreadParticipant.objects.filter(thread=thread)
  6. participants_qs = participants_qs.select_related('user')
  7. for participant in participants_qs.order_by('-is_owner', 'user__slug'):
  8. participant.thread = thread
  9. thread.participants_list.append(participant)
  10. if participant.user == user:
  11. thread.participant = participant
  12. return thread.participants_list
  13. def thread_has_participants(thread):
  14. return thread.threadparticipant_set.exists()
  15. def set_thread_owner(thread, user):
  16. ThreadParticipant.objects.set_thread_owner(thread, user)
  17. def sync_user_unread_private_threads(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. sync_user_unread_private_threads(user)
  27. def remove_participant(thread, user):
  28. """
  29. Remove thread participant, set "recound private threads" flag on user
  30. """
  31. thread.threadparticipant_set.filter(user=user).delete()
  32. sync_user_unread_private_threads(user)