def mark_threads_seen(self, request): """Trigger this endpoint to mark when the user has seen notifications about new messages in threads""" self.check_permissions(request) meta, _ = ConversationMeta.objects.update_or_create( {'threads_marked_at': timezone.now()}, user=request.user) serializer = ConversationMetaSerializer(meta) return Response(serializer.data)
def conversation_meta_saved(sender, instance, **kwargs): meta = instance payload = ConversationMetaSerializer(meta).data for subscription in ChannelSubscription.objects.recent().filter( user=meta.user): send_in_channel(subscription.reply_channel, topic='conversations:meta', payload=payload)
def list(self, request, *args, **kwargs): queryset = self.get_queryset() \ .exclude(conversation__latest_message_id=None) \ .annotate_unread_message_count() \ .annotate(conversation_latest_message_id=F('conversation__latest_message_id')) \ .select_related( 'conversation', 'conversation__latest_message', 'conversation__target_type', ) \ .prefetch_related( 'conversation__latest_message__reactions', 'conversation__participants', ) \ .order_by('-conversation__latest_message_id') queryset = self.filter_queryset(queryset) participations = self.paginate_queryset(queryset) conversations = [p.conversation for p in participations] messages = [ c.latest_message for c in conversations if c.latest_message is not None ] # Prefetch related objects per target type pickup_ct = ContentType.objects.get_for_model(PickupDate) pickup_conversations = [ item for item in conversations if item.target_type == pickup_ct ] pickups = PickupDate.objects. \ filter(id__in=[c.target_id for c in pickup_conversations]). \ prefetch_related('pickupdatecollector_set', 'feedback_given_by') applications_ct = ContentType.objects.get_for_model(Application) application_conversations = [ item for item in conversations if item.target_type == applications_ct ] applications = Application.objects. \ filter(id__in=[c.target_id for c in application_conversations]). \ select_related('user') issues_ct = ContentType.objects.get_for_model(Issue) issue_conversations = [ item for item in conversations if item.target_type == issues_ct ] issues = Issue.objects. \ filter(id__in=[c.target_id for c in issue_conversations]). \ prefetch_for_serializer(user=request.user) # Applicant does not have access to group member profiles, so we attach reduced user profiles my_applications = [a for a in applications if a.user == request.user] def get_conversation(application): return next(c for c in application_conversations if c.target_id == application.id) users = get_user_model().objects. \ filter(conversationparticipant__conversation__in=[get_conversation(a) for a in my_applications]). \ exclude(id=request.user.id) context = self.get_serializer_context() serializer = self.get_serializer(participations, many=True) message_serializer = ConversationMessageSerializer(messages, many=True, context=context) pickups_serializer = PickupDateSerializer(pickups, many=True, context=context) application_serializer = ApplicationSerializer(applications, many=True, context=context) issue_serializer = IssueSerializer(issues, many=True, context=context) user_serializer = UserInfoSerializer(users, many=True, context=context) meta, _ = ConversationMeta.objects.get_or_create(user=request.user) meta_serializer = ConversationMetaSerializer( meta, context=self.get_serializer_context()) return self.get_paginated_response({ 'conversations': serializer.data, 'messages': message_serializer.data, 'pickups': pickups_serializer.data, 'applications': application_serializer.data, 'issues': issue_serializer.data, 'users_info': user_serializer.data, 'meta': meta_serializer.data, })