diff --git a/server/messenger_backend/models/conversation.py b/server/messenger_backend/models/conversation.py index 17d5b05..58a4531 100644 --- a/server/messenger_backend/models/conversation.py +++ b/server/messenger_backend/models/conversation.py @@ -1,28 +1,18 @@ from django.db import models -from django.db.models import Q from . import utils from .user import User class Conversation(utils.CustomModel): - - user1 = models.ForeignKey( - User, on_delete=models.CASCADE, db_column="user1Id", related_name="+" - ) - user2 = models.ForeignKey( - User, on_delete=models.CASCADE, db_column="user2Id", related_name="+", - ) + users = models.ManyToManyField(User) createdAt = models.DateTimeField(auto_now_add=True, db_index=True) updatedAt = models.DateTimeField(auto_now=True) - # find conversation given two user Ids - def find_conversation(user1Id, user2Id): + # find conversation given user Ids + def find_conversation(userIds): # return conversation or None if it doesn't exist try: - return Conversation.objects.get( - (Q(user1__id=user1Id) | Q(user1__id=user2Id)), - (Q(user2__id=user1Id) | Q(user2__id=user2Id)), - ) + return Conversation.objects.get(users__exact=userIds) except Conversation.DoesNotExist: return None diff --git a/server/messenger_backend/models/message.py b/server/messenger_backend/models/message.py index ba3082a..9aa30d5 100644 --- a/server/messenger_backend/models/message.py +++ b/server/messenger_backend/models/message.py @@ -2,6 +2,7 @@ from . import utils from .conversation import Conversation +from .user import User class Message(utils.CustomModel): @@ -14,5 +15,6 @@ class Message(utils.CustomModel): related_name="messages", related_query_name="message" ) + readByUsers = models.ManyToMany(User, through='Reads') createdAt = models.DateTimeField(auto_now_add=True, db_index=True) updatedAt = models.DateTimeField(auto_now=True) \ No newline at end of file diff --git a/server/messenger_backend/models/read.py b/server/messenger_backend/models/read.py new file mode 100644 index 0000000..13bb72f --- /dev/null +++ b/server/messenger_backend/models/read.py @@ -0,0 +1,11 @@ +from django.db import models + +from . import utils +from .message import Message +from .user import User + + +class Reads(utils.CustomModel): + message = models.ForeignKey(Message, on_delete=models.CASCADE) + user = models.ForeignKey(User, on_delete=models.CASCADE) + readAt = models.DateTimeField(null=False) \ No newline at end of file