Skip to content

Commit

Permalink
inform the user when an agent is writing a message
Browse files Browse the repository at this point in the history
  • Loading branch information
iamlydial committed Nov 18, 2023
1 parent 1cc1f18 commit 8270b1b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
62 changes: 59 additions & 3 deletions chat/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,36 @@
from .models import Room, Message
from .templatetags.chatextras import initials


class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = f'chat_{self.room_name}'
self.user = self.scope['user']

# join room group
# Join room group
await self.get_room()
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
await self.accept()

# Inform user
if self.user.is_staff:
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'users_update'
}
)


async def disconnect(self, close_code):
# leave room
# Leave room
await self.channel_layer.group_discard(self.room_group_name, self.channel_name)


if not self.user.is_staff:
await self.set_room_closed()


async def receive(self, text_data):
# Receive message from WebSocket (front end)
text_data_json = json.loads(text_data)
Expand All @@ -48,7 +64,20 @@ async def receive(self, text_data):
'created_at': timesince(new_message.created_at),
}
)
elif type == 'update':
print('is update')
# Send update to the room
await self.channel_layer.group_send(
self.room_group_name, {
'type': 'writing_active',
'message': message,
'name': name,
'agent': agent,
'initials': initials(name),
}
)


async def chat_message(self, event):
# Send message to WebSocket (front end)
await self.send(text_data=json.dumps({
Expand All @@ -60,10 +89,37 @@ async def chat_message(self, event):
'created_at': event['created_at'],
}))


async def writing_active(self, event):
# Send writing is active to room
await self.send(text_data=json.dumps({
'type': event['type'],
'message': event['message'],
'name': event['name'],
'agent': event['agent'],
'initials': event['initials'],
}))


async def users_update(self, event):
# Send information to the web socket (front end)
await self.send(text_data=json.dumps({
'type': 'users_update'
}))


@sync_to_async
def get_room(self):
self.room = Room.objects.get(uuid=self.room_name)


@sync_to_async
def set_room_closed(self):
self.room = Room.objects.get(uuid=self.room_name)
self.room.status = Room.CLOSED
self.room.save()


@sync_to_async
def create_message(self, sent_by, message, agent):
message = Message.objects.create(body=message, sent_by=sent_by)
Expand Down
10 changes: 5 additions & 5 deletions chat/templates/chat/room.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ <h1 class="text-2xl">Room "{{ room.uuid }}"</h1>
>
Send
</button>
{% endif %} {% endblock %} {% block scripts %} {% if room.status == 'active' %}
{{ room.uuid|json_script:'room_uuid' }} {{
request.user.name|json_script:'user_name' }} {{
request.user.id|json_script:'user_id' }}

{% endif %} {% endblock %} {% block scripts %}
{% if room.status == 'active' %}
{{ room.uuid|json_script:'room_uuid' }}
{{ request.user.name|json_script:'user_name' }}
{{ request.user.id|json_script:'user_id' }}
<script src="{% static 'js/main_admin.js' %}"></script>
{% endif %} {% endblock %}
Binary file modified db.sqlite3
Binary file not shown.

0 comments on commit 8270b1b

Please sign in to comment.