Skip to content

Commit e22c960

Browse files
committed
Added route to get conversation messages
1 parent c6d7623 commit e22c960

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

backend/PyMatcha/models/user.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,6 @@ def get_conversation_list(self) -> List[Message]:
459459
return conversation_list
460460

461461
def get_messages_with_user(self, with_user_id) -> List[Message]:
462-
# TODO: Create a function to get latest messages only. Maybe https://stackoverflow.com/a/41095528/6350162 ?
463-
# Based on time or amount of messages https://stackoverflow.com/a/3799223/6350162
464462
with self.db.cursor() as c:
465463
c.execute(
466464
"""
@@ -495,7 +493,7 @@ def get_messages_with_user(self, with_user_id) -> List[Message]:
495493
messages = c.fetchall()
496494
message_list = []
497495
for message in messages:
498-
message_list.append(Message(message).get_all_info())
496+
message_list.append(Message(message))
499497
logging.debug(
500498
"Getting all messages between user {} and {} (Total: {})".format(self.id, with_user_id, len(message_list))
501499
)

backend/PyMatcha/routes/api/messages.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,11 @@
3131

3232

3333
REQUIRED_KEYS_NEW_MESSAGE = {"to_id": int, "content": str}
34-
# REQUIRED_KEYS_GET_CONVERSATION = {"with_id": int}
3534

3635
messages_bp = Blueprint("messages", __name__)
3736

38-
# TODO: Route to get the last message for each open conversation
39-
# TODO: Route to send a new message to a conversation
4037

41-
"""
42-
Route to get latest messages with user (then paginated to get the older messages)
43-
route to receive messages
44-
"""
38+
# TODO: route to receive messages
4539

4640

4741
@messages_bp.route("/conversations", methods=["GET"])
@@ -52,6 +46,19 @@ def get_opened_conversations():
5246
return SuccessOutput("conversations", returned_list)
5347

5448

49+
@messages_bp.route("/conversations/<with_uid>", methods=["GET"])
50+
@jwt_required
51+
def get_conversation_messsages(with_uid):
52+
# TODO: Limit to 200 messages and if need be get more
53+
try:
54+
with_user = get_user(with_uid)
55+
except NotFoundError:
56+
raise NotFoundError("With user {} not found", "Try again")
57+
message_list = current_user.get_messages_with_user(with_user.id)
58+
message_list = [m.to_dict for m in message_list]
59+
return SuccessOutput("messages", message_list)
60+
61+
5562
@messages_bp.route("/messages/see/<with_uid>", methods=["POST"])
5663
@jwt_required
5764
def see_conversation_messages(with_uid):
@@ -94,12 +101,3 @@ def send_message():
94101
current_user.send_message(to_id=to_id, content=content)
95102
current_app.logger.debug("/messages -> Message successfully sent to {}".format(to_id))
96103
return Success("Message successfully sent to {}".format(to_id))
97-
98-
99-
# @messages_bp.route("/messages/conversation", methods=["GET"])
100-
# @jwt_required
101-
# @validate_params(REQUIRED_KEYS_GET_CONVERSATION)
102-
# def get_conversation():
103-
# data = request.get_json()
104-
# with_id: int = int(data["with_id"])
105-
# return SuccessOutput("messages", current_user.get_messages_with_user(with_id))

0 commit comments

Comments
 (0)