forked from danidee10/Chatire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
115 lines (87 loc) · 3.44 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Views for the chat app."""
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
class ChatSessionView(APIView):
"""Manage Chat sessions."""
permission_classes = (permissions.IsAuthenticated,)
def post(self, request, *args, **kwargs):
"""create a new chat session."""
user = request.user
chat_session = ChatSession.objects.create(owner=user)
return Response({
'status': 'SUCCESS', 'uri': chat_session.uri,
'message': 'New chat session created'
})
def patch(self, request, *args, **kwargs):
"""Add a user to a chat session."""
User = get_user_model()
uri = kwargs['uri']
username = request.data['username']
user = User.objects.get(username=username)
chat_session = ChatSession.objects.get(uri=uri)
owner = chat_session.owner
if owner != user: # Only allow non owners join the room
chat_session.members.get_or_create(
user=user, chat_session=chat_session
)
owner = deserialize_user(owner)
members = [
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 ({
'status': 'SUCCESS', 'members': members,
'message': '%s joined that chat' % user.username,
'user': deserialize_user(user)
})
class ChatSessionMessageView(APIView):
"""Create/Get Chat session messages."""
permission_classes = (permissions.IsAuthenticated,)
def get(self, request, *args, **kwargs):
"""return all messages in a chat session."""
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()]
return Response({
'id': chat_session.id, 'uri': chat_session.uri,
'messages': messages
})
def post(self, request, *args, **kwargs):
"""create a new message in a chat session."""
uri = kwargs['uri']
message = request.data['message']
user = request.user
chat_session = ChatSession.objects.get(uri=uri)
chat_session_message = ChatSessionMessage.objects.create(
user=user, chat_session=chat_session, message=message
)
notif_args = {
'source': user,
'source_display_name': user.get_full_name(),
'category': 'chat', 'action': 'Sent',
'obj': chat_session_message.id,
'short_description': 'You a new message', 'silent': True,
'extra_data': {
'uri': chat_session.uri,
'message': chat_session_message.to_json()
}
}
notify.send(
sender=self.__class__,**notif_args, channels=['websocket']
)
return Response ({
'status': 'SUCCESS', 'uri': chat_session.uri, 'message': message,
'user': deserialize_user(user)
})
def raise_404(request):
"""Raise a 404 Error."""
raise Http404