Skip to content

Commit

Permalink
api: Fixed double conversations again
Browse files Browse the repository at this point in the history
  • Loading branch information
okbrandon committed Nov 15, 2024
1 parent 92fdacb commit aad2623
Showing 1 changed file with 1 addition and 41 deletions.
42 changes: 1 addition & 41 deletions backend/api/views/conversations.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,15 @@
import os

from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from django.db.models import Count, Q

from asgiref.sync import async_to_sync

from ..models import Conversation, Relationship, User
from ..serializers import ConversationSerializer
from ..util import get_safe_profile, generate_id
from ..util import get_safe_profile

class ConversationListView(APIView):

def ensure_conversations_exist(self, user):
friends = Relationship.objects.filter(
Q(userA=user.userID) | Q(userB=user.userID)
)
friends = friends.exclude(
Q(status=2) | Q(status=0),
Q(userA=user.userID) | Q(userB=user.userID)
)

user = User.objects.get(userID=user.userID)

for relationship in friends:
friend_id = relationship.userA if relationship.userA != user.userID else relationship.userB

try:
friend = User.objects.get(userID=friend_id)
except User.DoesNotExist:
continue

existing_conversation = Conversation.objects.filter(
participants__userID__in=[user.userID, friend_id],
conversationType='private_message'
).annotate(participant_count=Count('participants')).filter(participant_count=2).exists()

if not existing_conversation:
new_conversation = Conversation.objects.create(conversationID=generate_id("conv"), conversationType='private_message')
new_conversation.receipientID = user.userID
new_conversation.participants.add(user, friend)
new_conversation.save()

def get(self, request, *args, **kwargs):
me = request.user

# Ensuring conversations exist for the user
self.ensure_conversations_exist(me)

conversations = me.conversations.all()
serializer = ConversationSerializer(conversations, many=True)

Expand Down

0 comments on commit aad2623

Please sign in to comment.