Skip to content

Commit a27edc9

Browse files
authored
Merge pull request #361 from Seluj78/355-chat
2 parents 194ee30 + 62a82fc commit a27edc9

File tree

23 files changed

+629
-18
lines changed

23 files changed

+629
-18
lines changed

backend/PyMatcha/models/message.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020

2121
import logging
2222
from datetime import datetime
23+
from typing import Dict
2324

2425
from PyMatcha.utils import create_messages_table
2526
from PyMatcha.utils.orm import Field
2627
from PyMatcha.utils.orm import Model
28+
from timeago import format as timeago_format
2729

2830

2931
class Message(Model):
@@ -32,8 +34,8 @@ class Message(Model):
3234
id = Field(int, modifiable=False)
3335
from_id = Field(int)
3436
to_id = Field(int)
35-
timestamp = Field(datetime)
36-
seen_timestamp = Field(datetime)
37+
timestamp = Field(datetime, fmt="%Y-%m-%d %H:%M:%S")
38+
seen_timestamp = Field(datetime, fmt="%Y-%m-%d %H:%M:%S")
3739
content = Field(str)
3840
is_seen = Field(bool)
3941
is_liked = Field(bool)
@@ -61,6 +63,16 @@ def create(
6163
logging.debug("Created new message")
6264
return new_message
6365

66+
def to_dict(self) -> Dict:
67+
returned_dict = super().to_dict()
68+
returned_dict["timestamp_ago"] = timeago_format(self.timestamp, datetime.utcnow())
69+
if self.seen_timestamp:
70+
returned_dict["seen_timestamp_ago"] = timeago_format(self.seen_timestamp, datetime.utcnow())
71+
else:
72+
returned_dict["seen_timestamp_ago"] = None
73+
74+
return returned_dict
75+
6476
@classmethod
6577
def create_table(cls):
6678
create_messages_table(cls.db)

backend/PyMatcha/models/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def get_matches(self):
312312
return match_list
313313

314314
def send_message(self, to_id, content):
315-
Message.create(from_id=self.id, to_id=to_id, content=content)
315+
Message.create(from_id=self.id, to_id=to_id, content=content, timestamp=datetime.datetime.utcnow())
316316

317317
def get_messages(self) -> List[Message]:
318318
with self.db.cursor() as c:

backend/PyMatcha/models/view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class View(Model):
3232
id = Field(int, modifiable=False)
3333
profile_id = Field(int)
3434
viewer_id = Field(int)
35-
dt_seen = Field(datetime.datetime)
35+
dt_seen = Field(datetime.datetime, fmt="%Y-%m-%d %H:%M:%S")
3636

3737
@staticmethod
3838
def create(profile_id: int, viewer_id: int, dt_seen: datetime.datetime = datetime.datetime.utcnow()) -> View:

backend/PyMatcha/routes/api/messages.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
from PyMatcha.utils.errors import NotFoundError
3232
from PyMatcha.utils.success import Success
3333
from PyMatcha.utils.success import SuccessOutput
34+
from PyMatcha.utils.success import SuccessOutputMessage
35+
from timeago import format as timeago_format
3436

3537

3638
REQUIRED_KEYS_NEW_MESSAGE = {"to_uid": str, "content": str}
@@ -45,6 +47,7 @@ def get_opened_conversations():
4547
returned_list = [
4648
{
4749
"last_message_timestamp": c.timestamp,
50+
"last_message_timestamp_ago": timeago_format(c.timestamp, datetime.datetime.utcnow()),
4851
"last_message_content": c.content,
4952
"is_unseen": True if not c.is_seen and c.to_id == current_user.id else False,
5053
"with_user": get_user(c.to_id if c.to_id != current_user.id else c.from_id).to_dict(),
@@ -72,14 +75,18 @@ def send_message():
7275

7376
current_user.send_message(to_id=to_user.id, content=content)
7477
current_app.logger.debug("/messages -> Message successfully sent to {}.".format(to_uid))
78+
79+
new_message = Message.get_multis(to_id=to_user.id, content=content, from_id=current_user.id)[-1]
80+
7581
Notification.create(
7682
trigger_id=current_user.id,
7783
user_id=to_user.id,
7884
content=f"{current_user.first_name} said: {content}",
7985
type="message",
8086
link_to=f"conversation/{current_user.id}",
8187
)
82-
return Success("Message successfully sent to {}.".format(to_uid))
88+
89+
return SuccessOutputMessage("new_message", new_message.to_dict(), "Message successfully sent to {}.".format(to_uid))
8390

8491

8592
@messages_bp.route("/conversations/<with_uid>", methods=["GET"])
@@ -95,6 +102,7 @@ def get_conversation_messsages(with_uid):
95102

96103
message_list = current_user.get_messages_with_user(with_user.id)
97104
message_list = [m.to_dict() for m in message_list]
105+
message_list = sorted(message_list, key=lambda item: item["timestamp"])
98106
return SuccessOutput("messages", message_list)
99107

100108

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ services:
2525
redis:
2626
container_name: redis
2727
image: redis:4.0.5-alpine
28-
command: ["redis-server", "--appendonly", "yes"]
2928
hostname: redis
3029
ports:
3130
- '6379:6379'

frontend/src/assets/chat.png

16.9 KB
Loading

frontend/src/assets/heart.png

7.12 KB
Loading

frontend/src/assets/heartGreen.png

7.29 KB
Loading

frontend/src/assets/link.png

20.3 KB
Loading

frontend/src/assets/send.png

26.6 KB
Loading

0 commit comments

Comments
 (0)