Skip to content

Commit

Permalink
Completed part 7
Browse files Browse the repository at this point in the history
  • Loading branch information
danidee10 committed Mar 13, 2021
1 parent 8c578e9 commit c67aaa6
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 69 deletions.
2 changes: 1 addition & 1 deletion chat/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class BroadCastWebSocketChannel(BaseNotificationChannel):
"""Fanout notification for RabbitMQ."""
"""Fanout notification channel with RabbitMQ."""

def _connect(self):
"""Connect to the RabbitMQ server."""
Expand Down
33 changes: 16 additions & 17 deletions chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from django.http import Http404
from django.contrib.auth import get_user_model

from .models import (
ChatSession, ChatSessionMember, ChatSessionMessage, deserialize_user
)

from rest_framework import permissions
from rest_framework.views import APIView
from rest_framework.response import Response
from notifications.signals import notify

from notifications.utils import notify
from notifications import default_settings as notifs_settings

from .models import ChatSession, ChatSessionMessage, deserialize_user


class ChatSessionView(APIView):
Expand Down Expand Up @@ -47,17 +47,17 @@ def patch(self, request, *args, **kwargs):

owner = deserialize_user(owner)
members = [
deserialize_user(chat_session.user)
deserialize_user(chat_session.user)
for chat_session in chat_session.members.all()
]
members.insert(0, owner) # Make the owner the first member

return Response ({
return Response({
'status': 'SUCCESS', 'members': members,
'message': '%s joined that chat' % user.username,
'message': '%s joined the chat' % user.username,
'user': deserialize_user(user)
})


class ChatSessionMessageView(APIView):
"""Create/Get Chat session messages."""
Expand All @@ -69,8 +69,8 @@ def get(self, request, *args, **kwargs):
uri = kwargs['uri']

chat_session = ChatSession.objects.get(uri=uri)
messages = [chat_session_message.to_json()
for chat_session_message in chat_session.messages.all()]
messages = [chat_session_message.to_json()
for chat_session_message in chat_session.messages.all()]

return Response({
'id': chat_session.id, 'uri': chat_session.uri,
Expand All @@ -96,20 +96,19 @@ def post(self, request, *args, **kwargs):
'obj': chat_session_message.id,
'short_description': 'You a new message', 'silent': True,
'extra_data': {
'uri': chat_session.uri,
notifs_settings.NOTIFICATIONS_WEBSOCKET_URL_PARAM:
chat_session.uri,
'message': chat_session_message.to_json()
}
}
notify.send(
sender=self.__class__,**notif_args, channels=['websocket']
)
notify(**notif_args, channels=['websocket'])

return Response ({
return Response({
'status': 'SUCCESS', 'uri': chat_session.uri, 'message': message,
'user': deserialize_user(user)
})


def raise_404(request):
"""Raise a 404 Error."""
raise Http404
raise Http404
Loading

0 comments on commit c67aaa6

Please sign in to comment.