|
30 | 30 | from PyMatcha.utils.success import SuccessOutput |
31 | 31 |
|
32 | 32 |
|
33 | | -REQUIRED_KEYS_NEW_MESSAGE = {"to_id": int, "content": str} |
| 33 | +REQUIRED_KEYS_NEW_MESSAGE = {"to_uid": str, "content": str} |
34 | 34 |
|
35 | 35 | messages_bp = Blueprint("messages", __name__) |
36 | 36 |
|
37 | | - |
38 | | -# TODO: route to receive messages |
| 37 | +""" |
| 38 | +test get all messages |
| 39 | +test when is_unseen is true |
| 40 | +test more than one conv |
| 41 | +test see message |
| 42 | +test like message |
| 43 | +test unseen conversations |
| 44 | +""" |
39 | 45 |
|
40 | 46 |
|
41 | 47 | @messages_bp.route("/conversations", methods=["GET"]) |
42 | 48 | @jwt_required |
43 | 49 | def get_opened_conversations(): |
44 | 50 | conv_list = current_user.get_conversation_list() |
45 | | - returned_list = [c.to_dict() for c in conv_list] |
| 51 | + returned_list = [ |
| 52 | + { |
| 53 | + "last_message_timestamp": c.timestamp, |
| 54 | + "last_message_content": c.content, |
| 55 | + "is_unseen": True if not c.is_seen and c.to_id == current_user.id else False, |
| 56 | + "with_user": get_user(c.to_id if c.to_id != current_user.id else c.from_id).to_dict(), |
| 57 | + } |
| 58 | + for c in conv_list |
| 59 | + ] |
46 | 60 | return SuccessOutput("conversations", returned_list) |
47 | 61 |
|
48 | 62 |
|
| 63 | +@messages_bp.route("/messages/send", methods=["POST"]) |
| 64 | +@jwt_required |
| 65 | +@validate_params(REQUIRED_KEYS_NEW_MESSAGE) |
| 66 | +def send_message(): |
| 67 | + current_app.logger.debug("/messages -> Call") |
| 68 | + data = request.get_json() |
| 69 | + to_uid: str = data["to_uid"] |
| 70 | + content: str = data["content"] |
| 71 | + try: |
| 72 | + to_user = get_user(to_uid) |
| 73 | + except NotFoundError: |
| 74 | + raise NotFoundError(f"Recipient {to_uid} not found", "Try again") |
| 75 | + |
| 76 | + if current_user.id == to_user.id: |
| 77 | + raise BadRequestError("Cannot send a message to yourself.", "Try again") |
| 78 | + |
| 79 | + current_user.send_message(to_id=to_user.id, content=content) |
| 80 | + current_app.logger.debug("/messages -> Message successfully sent to {}.".format(to_uid)) |
| 81 | + return Success("Message successfully sent to {}.".format(to_uid)) |
| 82 | + |
| 83 | + |
49 | 84 | @messages_bp.route("/conversations/<with_uid>", methods=["GET"]) |
50 | 85 | @jwt_required |
51 | 86 | def get_conversation_messsages(with_uid): |
@@ -86,23 +121,6 @@ def like_message(message_id): |
86 | 121 | return Success(f"Liked message {message_id}") |
87 | 122 |
|
88 | 123 |
|
89 | | -@messages_bp.route("/messages/send", methods=["POST"]) |
90 | | -@jwt_required |
91 | | -@validate_params(REQUIRED_KEYS_NEW_MESSAGE) |
92 | | -def send_message(): |
93 | | - current_app.logger.debug("/messages -> Call") |
94 | | - data = request.get_json() |
95 | | - to_id: int = int(data["to_id"]) |
96 | | - content: str = data["content"] |
97 | | - try: |
98 | | - get_user(to_id) |
99 | | - except NotFoundError: |
100 | | - raise NotFoundError("Recipient {} not found", "Try again") |
101 | | - current_user.send_message(to_id=to_id, content=content) |
102 | | - current_app.logger.debug("/messages -> Message successfully sent to {}".format(to_id)) |
103 | | - return Success("Message successfully sent to {}".format(to_id)) |
104 | | - |
105 | | - |
106 | 124 | @messages_bp.route("/messages/unseen", methods=["GET"]) |
107 | 125 | @jwt_required |
108 | 126 | def get_new_messages(): |
|
0 commit comments