123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- from django.contrib.auth import get_user_model
- from django.utils.translation import ugettext as _
- from misago.core import deprecations
- from misago.core.mail import build_mail, send_messages
- from .events import record_event
- from .models import ThreadParticipant
- def has_participants(thread):
- return thread.threadparticipant_set.exists()
- def make_participants_aware(user, thread):
- thread.participants_list = []
- thread.participant = None
- participants_qs = ThreadParticipant.objects.filter(thread=thread)
- participants_qs = participants_qs.select_related('user')
- for participant in participants_qs.order_by('-is_owner', 'user__slug'):
- participant.thread = thread
- thread.participants_list.append(participant)
- if participant.user == user:
- thread.participant = participant
- return thread.participants_list
- def set_users_unread_private_threads_sync(users=None, participants=None):
- users_ids = []
- if users:
- users_ids += [u.pk for u in users]
- if participants:
- users_ids += [p.user_id for p in participants]
- User = get_user_model()
- User.objects.filter(id__in=set(users_ids)).update(
- sync_unread_private_threads=True
- )
- def set_owner(thread, user):
- """
- Set user as thread's owner
- """
- ThreadParticipant.objects.set_owner(thread, user)
- def change_owner(thread, user):
- """
- Replace thread's owner with other
- """
- ThreadParticipant.objects.set_owner(thread, user)
- set_users_unread_private_threads_sync(participants=thread.participants_list)
- if thread.participant and thread.participant.is_owner:
- record_event(request, thread, 'changed_owner', {
- 'user': {
- 'username': user.username,
- 'url': user.get_absolute_url(),
- }
- })
- else:
- record_event(request, thread, 'tookover')
- def add_participant(request, thread, user):
- """
- Adds single participant to thread, registers this on the event
- """
- add_participants(request, thread, [user])
- record_event(request, thread, 'added_participant', {
- 'user': {
- 'username': user.username,
- 'url': user.get_absolute_url(),
- }
- })
- def add_participants(request, thread, users):
- """
- Add multiple participants to thread, set "recound private threads" flag on them
- notify them about being added to thread
- """
- ThreadParticipant.objects.add_participants(thread, users)
- try:
- thread_participants = thread.participants_list
- except AttributeError:
- thread_participants = []
- set_users_unread_private_threads_sync(users=users, participants=thread_participants)
- emails = []
- for user in users:
- emails.append(build_noticiation_email(request, thread, user))
- send_messages(emails)
- def build_noticiation_email(request, thread, user):
- subject = _('%(user)s has invited you to participate in private thread "%(thread)s"')
- subject_formats = {
- 'thread': thread.title,
- 'user': request.user.username
- }
- return build_mail(
- request,
- user,
- subject % subject_formats,
- 'misago/emails/privatethread/added',
- {
- 'thread': thread
- }
- )
- def remove_participant(request, thread, user):
- """
- Remove thread participant, set "recound private threads" flag on user
- """
- removed_owner = False
- remaining_participants = []
- for participant in thread.participants_list:
- if participant.user == user:
- removed_owner = participant.is_owner
- else:
- remaining_participants.append(participant.user)
- set_users_unread_private_threads_sync(participants=thread.participants_list)
- if not remaining_participants:
- thread.delete()
- else:
- thread.threadparticipant_set.filter(user=user).delete()
- if removed_owner:
- thread.is_closed = True # flag thread to close
- if request.user == user:
- event_type = 'owner_left'
- else:
- event_type = 'removed_owner'
- else:
- if request.user == user:
- event_type = 'participant_left'
- else:
- event_type = 'removed_participant'
- record_event(request, thread, event_type, {
- 'user': {
- 'username': user.username,
- 'url': user.get_absolute_url(),
- }
- })
|