Skip to content

Commit

Permalink
api: Fixed double Relationship creation
Browse files Browse the repository at this point in the history
  • Loading branch information
okbrandon committed Nov 15, 2024
1 parent 49c9ff7 commit 92fdacb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
9 changes: 1 addition & 8 deletions backend/api/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def create_test_accounts(sender, **kwargs):
for i in range(1, 5):
username = f'test{i}'
if not User.objects.filter(username=username).exists():
user = User.objects.create(
User.objects.create(
userID=generate_id('user'),
username=username,
email=f'{username}@example.com',
Expand All @@ -105,10 +105,3 @@ def create_test_accounts(sender, **kwargs):
flags=1, # Set EMAIL_VERIFIED flag
money=100000
)

Relationship.objects.create(
relationshipID=generate_id("rel"),
userA=user.userID,
userB="user_ai",
status=1 # Accepted/friends status
)
13 changes: 11 additions & 2 deletions backend/api/consumers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ def add_message_to_conversation(self, conversation_id, user, content):

@sync_to_async
def ensure_conversations_exist(self, user):
# If the user is not friend with AI yet, create a Relationship
try:
ai_user = User.objects.get(userID="user_ai")

if not Relationship.objects.filter(
Q(userA=user.userID, userB=ai_user.userID) | Q(userA=ai_user.userID, userB=user.userID)
).exists():
Relationship.objects.create(userA=user.userID, userB=ai_user.userID, status=1)
except Exception as _:
pass

friends = Relationship.objects.filter(
Q(userA=user.userID) | Q(userB=user.userID)
)
Expand All @@ -199,8 +210,6 @@ def ensure_conversations_exist(self, user):
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

Expand Down
14 changes: 2 additions & 12 deletions backend/api/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
from rest_framework.decorators import permission_classes
from rest_framework_simplejwt.tokens import RefreshToken

from django.db.models import Count
from django.contrib.auth import login, authenticate
from django.contrib.auth import login
from django.core.exceptions import ValidationError
from django.contrib.auth.hashers import make_password
from django.utils import timezone
from django.contrib.auth.hashers import check_password

from ..models import User, VerificationCode, Relationship, Conversation
from ..serializers import UserSerializer
from ..models import User, VerificationCode
from ..util import generate_id, send_verification_email, send_otp_via_email, send_otp_via_sms
from ..backends import AuthBackend
from ..validators import validate_username, validate_email, validate_password, validate_lang
Expand Down Expand Up @@ -53,14 +51,6 @@ def post(self, request, *args, **kwargs):
flags=1 if skip_email_verification else 0 # Set EMAIL_VERIFIED flag if skipping verification
)

# Create a relationship with the AI user
Relationship.objects.create(
relationshipID=generate_id("rel"),
userA=user.userID,
userB="user_ai",
status=1 # Accepted/friends status
)

if not skip_email_verification:
verification_code = generate_id('code')
VerificationCode.objects.create(
Expand Down

0 comments on commit 92fdacb

Please sign in to comment.