-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathrooms.py
704 lines (618 loc) · 27.9 KB
/
rooms.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
import requests
from typing import Dict, List
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,
)
from django.core.files.storage import default_storage
# 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 rest_framework.pagination import PageNumberPagination
from .decorators import db_init_with_credentials
from .utils import SearchPagination
from asgiref.sync import sync_to_async
from django.views.decorators.cache import cache_page
# from django.http.response import JsonResponse
@swagger_auto_schema(
methods=["post"],
request_body=RoomSerializer,
operation_summary="Creates a new room between users",
responses={
200: "ok: Room already exist",
201: CreateRoomResponse,
400: "Error: Bad Request",
},
)
@api_view(["POST"])
@db_init_with_credentials
def create_room(request, member_id):
"""
Creates a room between users.
It takes the id of the users involved, sends a write request to the database .
Then returns the room id when a room is successfully created
"""
serializer = RoomSerializer(data=request.data)
if serializer.is_valid():
user_ids = serializer.data["room_member_ids"]
if len(user_ids) > 2:
# print(" --------MUKHTAR------- \n\r")
response = group_room(request, member_id)
if response.get('get_group_data'):
return Response(data={"room_id" : response['room_id']}, status=response['status_code'])
else:
# print(" --------FAE------- \n\r")
user_ids = serializer.data["room_member_ids"]
user_rooms = get_user_rooms(user_ids[0], DB.organization_id)
if isinstance(user_rooms, list):
for room in user_rooms:
room_users = room["room_user_ids"]
if set(room_users) == set(user_ids):
response_output = {
"room_id": room["_id"]
}
return Response(data=response_output, status=status.HTTP_200_OK)
elif user_rooms is None:
return Response("unable to read database", status=status.HTTP_424_FAILED_DEPENDENCY)
elif user_rooms.get("status_code") != 404 or user_rooms.get("status_code") != 200:
return Response("unable to read database", status=status.HTTP_424_FAILED_DEPENDENCY)
fields = {"org_id": serializer.data["org_id"],
"room_user_ids": serializer.data["room_member_ids"],
"room_name": serializer.data["room_name"],
"private": serializer.data["private"],
"created_at": serializer.data["created_at"],
"bookmark": [],
"pinned": [],
"starred": [],
"closed": False
}
response = DB.write("dm_rooms", data=fields)
# ===============================
if response.get("status") == 200:
room_id = response.get("data").get("object_id")
response_output = sidebar_emitter(org_id=DB.organization_id, member_id=member_id, group_room_name=serializer.data["room_name"])
try:
centrifugo_data = centrifugo_client.publish(
room=f"{DB.organization_id}_{member_id}_sidebar",
data=response_output,
) # publish data to centrifugo
if centrifugo_data and centrifugo_data.get("status_code") == 200:
return Response(
data={"room_id":room_id,"message":"success"}, status=status.HTTP_201_CREATED
)
else:
return Response(
data="room created but centrifugo failed",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
except:
return Response(
data="centrifugo server not available",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
return Response(f"unable to create room. Reason: {response}", status=status.HTTP_424_FAILED_DEPENDENCY)
return Response(data="Invalid data", status=status.HTTP_400_BAD_REQUEST)
@swagger_auto_schema(
methods=["get"],
operation_summary="Retrieves all rooms linked to a user id",
query_serializer=UserRoomsSerializer,
responses={
200: "OK: Success",
204: "No Rooms Available",
400: "Error: Bad Request",
},
)
@api_view(["GET"])
@db_init_with_credentials
def user_rooms(request, user_id):
"""
Retrieves all rooms a user is currently active in.
if there is no room for the user_id it returns a 204 status response.
"""
if request.method == "GET":
res = get_user_rooms(user_id, DB.organization_id)
if res is None:
return Response(
data="No rooms available", status=status.HTTP_204_NO_CONTENT
)
return Response(res, status=status.HTTP_200_OK)
return Response(status=status.HTTP_400_BAD_REQUEST)
@swagger_auto_schema(
methods=["get"],
operation_summary="Retrieves all the information about a room",
query_serializer=RoomInfoSerializer,
responses={
200: RoomInfoResponse,
400: "Error: Bad Request",
404: "Error: Room Not Found",
},
)
@api_view(["GET"])
@db_init_with_credentials
def room_info(request, room_id):
"""
Retrieves information about a room.
It takes the room id and searches the dm_rooms collection
If the room exists, a json response of the room details is returned
Else a 404 response is returned with a "No such room" message
"""
# room_id = request.GET.get("room_id", None)
org_id = DB.organization_id
room_collection = "dm_rooms"
current_room = DB.read(room_collection, {"_id": room_id})
if current_room and current_room.get("status_code", None) is None:
if "room_user_ids" in current_room:
room_user_ids = current_room["room_user_ids"]
elif "room_member_ids" in current_room:
room_user_ids = current_room["room_member_ids"]
else:
room_user_ids = ""
starred = current_room.get("starred","")
pinned = current_room.get("pinned","")
bookmark = current_room.get("bookmark","")
private = current_room.get("private","")
created_at = current_room.get("created_at","")
if "org_id" in current_room:
org_id = current_room["org_id"]
room_name = current_room.get("room_name","")
if len(room_user_ids) > 3:
text = f" and {len(room_user_ids)-2} others"
elif len(room_user_ids) == 3:
text = " and 1 other"
else:
text = " only"
if len(room_user_ids) >= 1:
user1 = get_user_profile(org_id=org_id, user_id=room_user_ids[0])
if user1["status"] == 200:
user_name_1 = user1["data"]["user_name"]
else:
user_name_1 = room_user_ids[0]
else:
user_name_1 = "Some user"
if len(room_user_ids) > 1:
user2 = get_user_profile(org_id=org_id, user_id=room_user_ids[1])
if user2["status"] == 200:
user_name_2 = user2["data"]["user_name"]
else:
user_name_2 = room_user_ids[1]
else:
user_name_2 = "Some user"
room_data = {
"room_id": room_id,
"org_id": org_id,
"room_name": room_name,
"room_user_ids": room_user_ids,
"created_at": created_at,
"description": f"This room contains the coversation between You and {user_name_2}{text}",
"starred": starred,
"pinned": pinned,
"private": private,
"bookmarks": bookmark,
"Number of users": f"{len(room_user_ids)}",
}
return Response(data=room_data, status=status.HTTP_200_OK)
return Response(data="Room not found", status=status.HTTP_404_NOT_FOUND)
@db_init_with_credentials
def group_room(request, member_id):
serializer = RoomSerializer(data=request.data)
if serializer.is_valid():
user_ids = serializer.data["room_member_ids"]
if len(user_ids) > 9:
response = {
"get_group_data": True,
"status_code": 400,
"room_id": "Group cannot have over 9 total users"
}
return response
else:
all_rooms = DB.read("dm_rooms")
if all_rooms and isinstance(all_rooms, list):
for room_obj in all_rooms:
try:
room_members = room_obj['room_user_ids']
if len(room_members) > 2 and set(room_members) == set(user_ids):
response = {
"get_group_data": True,
"status_code": 200,
"room_id": room_obj["_id"]
}
return response
except KeyError:
pass
fields = {
"org_id": serializer.data["org_id"],
"room_user_ids": serializer.data["room_member_ids"],
"room_name": serializer.data["room_name"],
"private": serializer.data["private"],
"created_at": serializer.data["created_at"],
"bookmark": [],
"pinned": [],
"starred": [],
"closed": False
}
response = DB.write("dm_rooms", data=fields)
return response
@api_view(["PUT","GET"])
@db_init_with_credentials
def star_room(request, room_id, member_id):
"""
Endpoint for starring and unstarring a user, it moves the user from the dm list to a starred dm list
"""
if request.method == "PUT":
room = DB.read("dm_rooms", {"_id": room_id})
if room:
if member_id in room.get("room_member_ids", []) or member_id in room.get(
"room_user_ids", []
):
data = room.get("starred", [])
if member_id in data:
data.remove(member_id)
else:
data.append(member_id)
response = DB.update("dm_rooms", room_id,{"starred":data})
if response and response.get("status_code", None) is None:
response_output = sidebar_emitter(org_id=DB.organization_id, member_id=member_id)
try:
centrifugo_data = centrifugo_client.publish (
room=f"{DB.organization_id}_{member_id}_sidebar", data=response_output ) # publish data to centrifugo
if centrifugo_data and centrifugo_data.get ( "status_code" ) == 200:
return Response ( data={"message":"success"}, status=status.HTTP_201_CREATED )
else:
return Response(
data="starred status updated centrifugo unavailable",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
except:
return Response(
data="centrifugo server not available",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
# return Response(
# "Success", status=status.HTTP_200_OK
# )
return Response(
data="Room not updated", status=status.HTTP_424_FAILED_DEPENDENCY
)
return Response(data="User not in room", status=status.HTTP_404_NOT_FOUND)
return Response("Invalid room", status=status.HTTP_400_BAD_REQUEST)
elif request.method == "GET":
room = DB.read("dm_rooms", {"_id": room_id})
if room:
if member_id in room.get("room_member_ids", []) or member_id in room.get(
"room_user_ids", []
):
data = room.get("starred", [])
if member_id in data:
return Response({"status": True}, status=status.HTTP_200_OK)
return Response({"status": False}, status=status.HTTP_200_OK)
return Response(data="User not in room", status=status.HTTP_404_NOT_FOUND)
return Response("Invalid room", status=status.HTTP_400_BAD_REQUEST)
return Response(status=status.HTTP_400_BAD_REQUEST)
@swagger_auto_schema(
methods=["post"],
operation_summary="Adds a member to a room",
responses={
201: "Created",
406: "Not Acceptable, You can only add users to a group dm",
400: "Bad Request",
424: "Failed Dependency",
},
)
@api_view(["POST"])
@db_init_with_credentials
def group_member_add(request, room_id, member_id):
"""
Adds a user to a group dm
returns 201 response if succesful or the appropriate response otherwise
:params: org id and room_id
:payload: room_id: str, members_id: list & room_name: str
"""
ORG_ID = DB.organization_id
serializer = AddMemberSerializer(data=request.data)
if serializer.is_valid():
members_id = serializer.data['members_id']
room_id = serializer.data['room_id']
room = DB.read('dm_rooms', {"_id": room_id})
if room and isinstance(room, dict):
room_members = room['room_user_ids']
room_creator = room_members[0]
# print("ROOM CREATOR", room_creator)
# print("ROOM MEMBERS BEFORE", room_members)
if len(room_members) > 2:
# if member_id not in room_members:
# room_members.append(member_id)
room_members.extend(members_id)
room_members = list(set(room_members))
if len(room_members) > 9:
return Response("Max Number For Members In a Group is 9", status=status.HTTP_400_BAD_REQUEST)
# print("ROOM MEMBERS AFTER", room_members)
# =====================================================
# =====================================================
user_rooms = get_user_rooms(room_creator, DB.organization_id)
if user_rooms and isinstance(user_rooms, list):
for room in user_rooms:
room_users = room["room_user_ids"]
if set(room_users) == set(room_members):
response_output = {
"room_id": room["_id"]
}
return Response(data=response_output, status=status.HTTP_200_OK)
elif user_rooms.get("status_code") != 404:
if user_rooms is None or user_rooms.get("status_code") != 200:
return Response("unable to read database", status=status.HTTP_424_FAILED_DEPENDENCY)
fields = {
"org_id": f"{ORG_ID}",
"room_user_ids": room_members,
"room_name": serializer.data["room_name"],
"private": serializer.data["private"],
"created_at": serializer.data["created_at"],
"bookmark": [],
"pinned": [],
"starred": [],
"closed": False
}
response = DB.write("dm_rooms", data=fields)
# ===============================
data_ID = response.get("data").get("object_id")
if response.get("status") == 200:
response_output = {
"event": "sidebar_update",
"plugin_id": "dm.zuri.chat",
"data": {
"group_name": "DM",
"name": "DM Plugin",
"category": "direct messages",
"show_group": False,
"button_url": "/dm",
"public_rooms": [],
"joined_rooms": sidebar_emitter(org_id=DB.organization_id, member_id=room_creator, group_room_name=serializer.data["room_name"])
}
}
try:
centrifugo_data = centrifugo_client.publish(
room=f"{DB.organization_id}_{room_creator}_sidebar",
data=response_output) # publish data to centrifugo
if centrifugo_data and centrifugo_data.get("status_code") == 200:
return Response(data=response_output, status=status.HTTP_201_CREATED)
else:
return Response(
data="room created but centrifugo failed",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
except:
return Response(
data="centrifugo server not available",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
return Response("data not sent", status=status.HTTP_424_FAILED_DEPENDENCY)
# =====================================================
# =====================================================
else:
err_response = {"error": "Room is not a group room, Can only add users to group dm"}
return Response(err_response, status=status.HTTP_406_NOT_ACCEPTABLE)
else:
return Response({"error": "Unable to read DB"}, status=status.HTTP_424_FAILED_DEPENDENCY)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@swagger_auto_schema(
methods=["put"],
operation_summary="Closes DM on the sidebar",
responses={
200: "success",
404: "room not found",
405: "Method Not Allowed",
424: "failed",
},
)
@api_view(["PUT"])
def close_conversation(request, org_id, room_id, member_id):
"""
This function allows for the Closing of a DM room on the sidebar
The params taken are the organization ID, room ID and the ID of the member initiating the command
The HTTPS method is PUT
A request to this function matches a valid room to a user in the room users list
It then removes the room from the sidebar in realtime by toggling the closed status of the room from False to True
"""
data = {}
try:
room = DB.read("dm_rooms", {"_id": room_id})
except Exception:
return None
if room:
if member_id in room["room_user_ids"]:
if room["closed"] == False:
data = {
"closed": True
}
else:
data = {
"closed": False
}
response = DB.update("dm_rooms", room_id, data)
if response:
output_data = {
"event": "sidebar_update",
"plugin_id": "dm.zuri.chat",
"data": {
"name": "DM Plugin",
"description": "closes a DM conversation",
"group_name": "DM",
"category": "direct messages",
"show_group": False,
"button_url": "/dm",
"public_rooms": [],
"joined_rooms": sidebar_emitter(org_id=org_id, member_id=member_id),
"starred_rooms": []
}
}
try:
centrifugo_data = centrifugo_client.publish (
room=f"{org_id}_{member_id}_sidebar", data=output_data )
if centrifugo_data and centrifugo_data.get ( "status_code" ) == 200:
return Response ( data=output_data, status=status.HTTP_200_OK )
else:
return Response(
data="conversation closed but centrifugo unavailable",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
except:
return Response(
data="centrifugo server not available",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
else:
return Response(
"failed, conversation not closed", status=status.HTTP_424_FAILED_DEPENDENCY
)
return Response("member not in room", status=status.HTTP_403_FORBIDDEN)
return Response("room not found or not in users list", status=status.HTTP_404_NOT_FOUND)
@swagger_auto_schema(
methods=["get"],
operation_summary="searches for message by a user",
responses={404: "Error: Not Found"},
)
@sync_to_async
@api_view(["GET"])
@db_init_with_credentials
def search_DM(request, member_id):
key = request.query_params.get("q", "")
users = request.query_params.getlist("filter", [])
limit = request.query_params.get("limit", 20)
try:
if type(limit) == str:
limit = int(limit)
except ValueError:
limit = 20
paginator = SearchPagination()
paginator.page_size = limit
try:
if users != []:
rooms_query = {
"$and": [
{"room_user_ids": member_id},
{"room_user_ids": {"$in": users}},
]
}
else:
rooms_query = {"room_user_ids": member_id}
options = {"sort": {"created_at": -1},}
rooms = DB.read_query("dm_rooms", query=rooms_query, options=options)
org_id = DB.organization_id
room_ids = list(map(lambda room: room["_id"], rooms)) if rooms else []
if rooms:
members = get_all_organization_members(org_id)
messages_query = {
"$and":[
{"message":{"$regex":key}},
{"room_id":{"$in":room_ids}}
]
}
messages = DB.read_query("dm_messages", query = messages_query)
if messages:
members_found = {}
for message in messages:
if message['sender_id'] not in members_found:
members_found[message['sender_id']] = get_member(members,message.get('sender_id')) #get profile
if 'read' in message.keys(): del message['read']
if 'pinned' in message.keys():del message['pinned']
if 'saved_by' in message.keys():del message['saved_by']
if 'threads' in message.keys(): del message['threads']
if 'thread' not in message.keys(): message['thread'] = False
if 'notes' in message.keys(): del message['notes']
if 'replied_message' in message.keys(): del message['replied_message']
if 'sent_from_thread' in message.keys(): del message['sent_from_thread']
if 'reactions' in message.keys(): del message['reactions']
if 'thread' in message.keys(): del message['thread']
message['destination_url'] = f"/dm/{message['room_id']}"
message['room_name'] = members_found[message['sender_id']]['user_name'] if members_found[message['sender_id']] else None
message['title'] = members_found[message['sender_id']]['user_name'] if members_found[message['sender_id']] else None
message['created_by'] = members_found[message['sender_id']]['user_name'] if members_found[message['sender_id']] else None
message['images_url'] = [members_found[message['sender_id']]['image_url'] if members_found[message['sender_id']] else None]
message['content'] = message['message']
result = paginator.paginate_queryset(messages, request)
return paginator.get_paginated_response(result,key,users,request)
result = paginator.paginate_queryset([], request)
return paginator.get_paginated_response(result,key,users,request)
except Exception as e:
print(e)
result = paginator.paginate_queryset([], request)
return paginator.get_paginated_response(result,key,users,request)
@swagger_auto_schema(
methods=["get"],
operation_summary="gets search suggestion for a user",
responses={404: "Error: Not Found"},
)
@sync_to_async
@api_view(["GET"])
@db_init_with_credentials
def search_suggestions(request, member_id):
query = {"room_user_ids":member_id}
options = {
"sort":{"created_at": -1},
"projection":{"room_user_ids":1, "_id":0}
}
data = {}
org_id = DB.organization_id
try:
members = get_all_organization_members(org_id)
rooms = DB.read_query("dm_rooms",query=query,options=options)
member_ids = []
if rooms:
for room in rooms:
room['room_user_ids'].remove(member_id)
if None in room['room_user_ids']:
room['room_user_ids'].remove(None)
member_ids.extend(room['room_user_ids'])
for member in members:
if member['_id'] in member_ids:
username = member.get('user_name')
data[member['_id']] = username
response = {
"status":"ok",
"type":"suggestions",
"data":data
}
except Exception as e:
print(e)
response = {
"status":"ok",
"type":"suggestions",
"data":data
}
return Response(response, status=status.HTTP_200_OK)
@sync_to_async
@api_view(["GET"])
@db_init_with_credentials
def all_dms(request, member_id):
"""
Retrieves the all latest dm in a room that the user had been active and
also retrives the latest messages in the displayed dms.
"""
if request.method =="GET":
paginator = PageNumberPagination()
paginator.page_size = 20
rooms=DB.read_query("dm_rooms", query = {"room_user_ids":member_id} )
all_messages=[] #holds data to render
try:
room_ids = [room['_id'] for room in rooms ]
query = {
"room_id":{"$in":room_ids}
}
options = {
"sort":{'created_at': -1}
}
messages=DB.read_query("dm_messages", query=query, options = options)
for message in messages:
if message['room_id'] in room_ids:
all_messages.append(message)
room_ids.remove(message['room_id'])
response = paginator.paginate_queryset(all_messages, request)
return paginator.get_paginated_response(response)
except:
response = paginator.paginate_queryset(all_messages, request)
return paginator.get_paginated_response(response)