Skip to content

Commit f5115cf

Browse files
committed
add time to messages
1 parent 1d4aa17 commit f5115cf

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

chat/consumer.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""chat consumer
22
"""
3-
from django.db.models.query import QuerySet
43
import json
4+
from django.db.models.query import QuerySet
55
from asgiref.sync import async_to_sync
66
from channels.generic.websocket import WebsocketConsumer
77
from rest_framework.renderers import JSONRenderer
@@ -28,11 +28,12 @@ def new_message(self, data: dict):
2828
room = Chat.objects.get(room_id=data['room_name'])
2929
type = self.commands[data['command']]
3030
# use `objects.create` because I just want `insert` message!
31-
Message.objects.create(
31+
message: Message = Message.objects.create(
3232
author=author,
3333
content=data['message'],
3434
room=room,
3535
type=type)
36+
return message.get_time()
3637

3738
def fetch_message(self, room_name: str):
3839
query_set: QuerySet[Message] = Message.last_messages(room_name)
@@ -43,19 +44,16 @@ def fetch_message(self, room_name: str):
4344
{
4445
"message": message['content'],
4546
"author": message['author_username'],
46-
"command": self.commands[message['type']]
47+
"command": self.commands[message['type']],
48+
"time": message['time'],
4749
}
4850
)
4951

50-
def send_image(self, data: dict):
51-
self.new_message(data)
52-
self.send_to_room(data)
53-
5452
def notification(self, data: dict):
5553
room: str = data['room_name']
5654
members: list = Chat.get_members_list(room)
5755
async_to_sync(self.channel_layer.group_send)(
58-
"chat_BFoULH5Z",
56+
"chat_BFoULH5Z", # TODO dynamic this url
5957
{
6058
'type': 'chat_message',
6159
'message': data["message"],
@@ -99,13 +97,16 @@ def receive(self, text_data: str):
9997
message: str = data_dict['message']
10098
if not message.strip():
10199
return
102-
self.new_message(data_dict)
100+
time = self.new_message(data_dict)
101+
data_dict['time'] = time
103102
self.send_to_room(data_dict)
104103
elif command == 'fetch_message':
105104
room_name: str = data_dict['room_name']
106105
self.fetch_message(room_name)
107106
elif command == 'send_image':
108-
self.send_image(data_dict)
107+
time = self.new_message(data_dict)
108+
data_dict['time'] = time
109+
self.send_to_room(data_dict)
109110
else:
110111
print(f'Invalid Command: "{command}"')
111112

@@ -118,13 +119,15 @@ def send_to_room(self, data: dict):
118119
message: str = data['message']
119120
author: str = data['username']
120121
command: str = data.get("command", "new_message")
122+
time: str = data['time']
121123
async_to_sync(self.channel_layer.group_send)(
122124
self.room_group_name,
123125
{
124126
'type': 'chat_message',
125127
'message': message,
126128
'author': author,
127129
'command': command,
130+
'time': time,
128131
}
129132
)
130133

chat/models.py

+6
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ def last_messages(room_id: str):
142142
def author_username(self):
143143
return self.author.username
144144

145+
def get_time(self):
146+
time = datetime.strftime(
147+
self.timestamp.astimezone(),
148+
"%I:%M %p")
149+
return time
150+
145151
def __str__(self):
146152
return f'پیام "{self.author}" در گروه "{self.room}"'
147153

chat/serializers.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
from datetime import datetime
12
from rest_framework import serializers
23
from .models import Message
34

45

56
class MessageSerializer(serializers.ModelSerializer):
7+
time = serializers.SerializerMethodField()
8+
69
class Meta:
710
model = Message
8-
fields = ['author_username', 'content', 'type']
11+
fields = ['author_username', 'content', 'type', 'time']
12+
13+
def get_time(self, message: Message):
14+
time = datetime.strftime(
15+
message.timestamp.astimezone(),
16+
"%I:%M %p")
17+
return time

0 commit comments

Comments
 (0)