Skip to content

Commit ef60ba7

Browse files
committed
Added user method to get conversation list
1 parent bb33a97 commit ef60ba7

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

backend/PyMatcha/models/user.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,6 @@ def get_matches(self):
390390
return match_list
391391

392392
def send_message(self, to_id, content):
393-
# TODO: Send notification to the other user
394-
logging.debug("Sending message from {} to {}".format(self.id, to_id))
395393
Message.create(from_id=self.id, to_id=to_id, content=content)
396394

397395
def get_messages(self) -> List[Message]:
@@ -421,6 +419,45 @@ def get_messages(self) -> List[Message]:
421419
logging.debug("Getting all messages sent or received by user {}".format(self.id))
422420
return message_list
423421

422+
def get_conversation_list(self) -> List[Message]:
423+
with self.db.cursor() as c:
424+
c.execute(
425+
"""
426+
SELECT * FROM messages
427+
JOIN
428+
(
429+
SELECT user, max(timestamp) m
430+
FROM
431+
(
432+
(
433+
SELECT id, to_id user, timestamp
434+
FROM messages
435+
WHERE from_id={0}
436+
)
437+
UNION
438+
(
439+
SELECT id, from_id user, timestamp
440+
FROM messages
441+
WHERE to_id={0}
442+
)
443+
) t1
444+
GROUP BY user
445+
) t2
446+
ON
447+
((from_id={0} AND to_id=user) OR
448+
(from_id=user AND to_id={0})) AND
449+
(timestamp = m)
450+
ORDER BY timestamp DESC
451+
""".format(
452+
self.id
453+
)
454+
)
455+
conversations = c.fetchall()
456+
conversation_list = []
457+
for last_message in conversations:
458+
conversation_list.append(Message(last_message))
459+
return conversation_list
460+
424461
def get_messages_with_user(self, with_user_id) -> List[Message]:
425462
# TODO: Create a function to get latest messages only. Maybe https://stackoverflow.com/a/41095528/6350162 ?
426463
# Based on time or amount of messages https://stackoverflow.com/a/3799223/6350162

0 commit comments

Comments
 (0)