-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathreactions.py
272 lines (257 loc) · 11.2 KB
/
reactions.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import uuid
from django.http import response
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework import status
from .db import *
from rest_framework.views import (
APIView,
exception_handler,
)
# Import Read Write function to Zuri Core
from .resmodels import *
from .serializers import *
from drf_yasg.utils import swagger_auto_schema
from .centrifugo_handler import centrifugo_client
from .decorators import db_init_with_credentials
class Emoji(APIView):
"""
List all Emoji reactions, or create a new Emoji reaction.
"""
@swagger_auto_schema(
operation_summary="Retrieves reactions to messages",
responses={
200: "OK: Success!",
400: "Error: Bad Request",
},
)
# @method_decorator(db_init_with_credentials)
def get(self, request, org_id: str, room_id: str, message_id: str):
# fetch message related to that reaction
message = DB.read("dm_messages", {"_id": message_id, "room_id": room_id})
if message:
print(message)
if response:
return Response(
data={
"status": message["message"],
"event": "get_message_reactions",
"room_id": message["room_id"],
"message_id": message["_id"],
"data": {
"reactions": message["reactions"],
},
},
status=status.HTTP_200_OK,
)
return Response(
data="Data not retrieved", status=status.HTTP_424_FAILED_DEPENDENCY
)
return Response("No such message or room", status=status.HTTP_404_NOT_FOUND)
@swagger_auto_schema(
request_body=EmojiSerializer,
operation_summary="Creates and keeps tracks of reactions to messages",
responses={201: "OK: Success!", 400: "Error: Bad Request"},
)
# @method_decorator(db_init_with_credentials)
def post(self, request, org_id: str, room_id: str, message_id: str):
request.data["message_id"] = message_id
serializer = EmojiSerializer(data=request.data)
if serializer.is_valid():
data = serializer.data
data["count"] += 1
message_id = data["message_id"]
sender_id = data["sender_id"]
# fetch message related to that reaction
message = DB.read("dm_messages", {"_id": message_id, "room_id": room_id})
if message:
# get reactions
reactions = message.get("reactions", [])
reactions.append(data)
room = DB.read(ROOMS, {"_id": message["room_id"]})
if room:
# update reactions for a message
response = DB.update(MESSAGES, message_id, {"reactions": reactions})
if response.get("status", None) == 200:
response_output = {
"status": response["message"],
"event": "add_message_reaction",
"reaction_id": str(uuid.uuid1()),
"room_id": message["room_id"],
"message_id": message["_id"],
"data": {
"sender_id": sender_id,
"reaction": data["data"],
"created_at": data["created_at"],
},
}
centrifugo_data = centrifugo_client.publish(
room=message["room_id"], data=response_output
) # publish data to centrifugo
if centrifugo_data["message"].get("error", None) == None:
return Response(
data=response_output, status=status.HTTP_201_CREATED
)
return Response(
"Data not sent", status=status.HTTP_424_FAILED_DEPENDENCY
)
return Response("Unknown room", status=status.HTTP_404_NOT_FOUND)
return Response(
"Message or room not found", status=status.HTTP_404_NOT_FOUND
)
return Response(
data=serializer.errors,
status=status.HTTP_400_BAD_REQUEST,
)
class ThreadEmoji(APIView):
"""
List all Emoji reactions, or create a new Emoji reaction.
"""
def get(
self,
request,
org_id: str,
room_id: str,
message_id: str,
thread_message_id: str,
):
data_storage = DataStorage()
data_storage.organization_id = org_id
message = data_storage.read(
"dm_messages", {"_id": message_id, "room_id": room_id}
)
if message:
if "status_code" in message:
return Response(
data="Unable to retrieve data from zc core",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
current_thread_message = [
thread
for thread in message["threads"]
if thread["_id"] == thread_message_id
]
if current_thread_message:
return Response(
data={
"status": "200",
"event": "get_thread_message_reactions",
"room_id": message["room_id"],
"message_id": message["_id"],
"thread_message_id": current_thread_message[0]["_id"],
"data": {
"reactions": current_thread_message[0]["reactions"],
},
},
status=status.HTTP_200_OK,
)
return Response(
data="No such thread message", status=status.HTTP_404_NOT_FOUND
)
return Response("No such message or room", status=status.HTTP_404_NOT_FOUND)
def post(
self,
request,
org_id: str,
room_id: str,
message_id: str,
thread_message_id: str,
):
request.data["message_id"] = thread_message_id
serializer = EmojiSerializer(data=request.data)
if serializer.is_valid():
data = serializer.data
thread_message_id = data["message_id"]
sender_id = data["sender_id"]
data_storage = DataStorage()
data_storage.organization_id = org_id
# fetch message related to that reaction
message = data_storage.read(
"dm_messages", {"_id": message_id, "room_id": room_id}
)
if message:
if "status_code" in message:
return Response(
data="Unable to retrieve data from zc core",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
# get reactions
current_thread_message = [
thread
for thread in message["threads"]
if thread["_id"] == thread_message_id
]
if current_thread_message:
reactions = current_thread_message[0].get("reactions", [])
old_reactions = reactions.copy()
all_emojis = [emoji["data"] for emoji in old_reactions]
if data["data"] in all_emojis:
for emoji in old_reactions:
if data["data"] == emoji["data"]:
if sender_id in emoji["reacted_by_users_id"]:
emoji["count"] -= 1
emoji["reacted_by_users_id"].remove(sender_id)
action = "delete"
if emoji["count"] == 0:
reactions.remove(emoji)
else:
emoji["count"] += 1
emoji["reacted_by_users_id"].append(sender_id)
data["_id"] = emoji["_id"]
action = "create"
break
else:
action = "create"
data["count"] += 1
data["_id"] = str(uuid.uuid1())
data["reacted_by_users_id"] = []
data["reacted_by_users_id"].append(sender_id)
reactions.append(data)
response = data_storage.update(
"dm_messages", message_id, {"threads": message["threads"]}
)
if response.get("status", None) == 200:
if action == "create":
response_output = {
"status": response["message"],
"event": "add_thread_message_reaction",
"reaction_id": data["_id"],
"parent_message_id": message["_id"],
"thread_message_id": current_thread_message[0]["_id"],
"data": {
"sender_id": sender_id,
"reaction": data["data"],
"created_at": data["created_at"],
},
}
else:
response_output = {
"status": response["message"],
"event": "remove_thread_message_reaction",
"parent_message_id": message["_id"],
"data": {"response": "Reaction successfully deleted"},
}
centrifugo_data = centrifugo_client.publish(
room=message["room_id"], data=response_output
) # publish data to centrifugo
if centrifugo_data["message"].get("error", None) == None:
return Response(
data=response_output, status=status.HTTP_201_CREATED
)
return Response(
data="Centrifugo server not available",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
return Response(
"Data not sent", status=status.HTTP_424_FAILED_DEPENDENCY
)
return Response(
data="Not such thread message", status=status.HTTP_404_NOT_FOUND
)
return Response(
"Message or room not found", status=status.HTTP_404_NOT_FOUND
)
return Response(
data=serializer.errors,
status=status.HTTP_400_BAD_REQUEST,
)