-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathmedia.py
141 lines (129 loc) · 5.81 KB
/
media.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
import json
from typing import Dict, List
import uuid
import re
from django.http import response
from django.utils.decorators import method_decorator
from django.http.response import JsonResponse
from django.shortcuts import render
from django.views import generic
from rest_framework.response import Response
from rest_framework.decorators import api_view, parser_classes
from rest_framework import status, generics
import requests
import time
from .utils import send_centrifugo_data
from .db import *
from rest_framework.parsers import MultiPartParser, FormParser
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 import openapi
from drf_yasg.utils import swagger_auto_schema
from datetime import datetime
import datetime as datetimemodule
from .centrifugo_handler import centrifugo_client
from rest_framework.pagination import PageNumberPagination
from .decorators import db_init_with_credentials
from queue import LifoQueue
class SendFile(APIView):
"""
This endpoint is a send message endpoint that can take files, upload them
and return the urls to the uploaded files to the media list in the message
serializer
This endpoint uses form data
The file must be passed in with the key "file"
"""
parser_classes = (MultiPartParser, FormParser)
@swagger_auto_schema(
operation_summary="Sends files as messages in rooms",
responses={
201: "OK: File Created!",
},
)
# @method_decorator(db_init_with_credentials)
def post(self, request, room_id, org_id):
print(request.FILES)
token = request.META.get("HTTP_AUTHORIZATION")
if request.FILES:
file_urls = []
files = request.FILES.getlist("file")
if len(files) == 1:
for file in request.FILES.getlist("file"):
file_data = DB.upload(file=file, token=token)
if file_data["status"] == 200:
for datum in file_data["data"]["files_info"]:
file_urls.append(datum["file_url"])
else:
return Response(file_data)
elif len(files) > 1:
multiple_files = []
for file in files:
multiple_files.append(("file", file))
file_data = DB.upload_more(files=multiple_files, token=token)
if file_data["status"] == 200:
for datum in file_data["data"]["files_info"]:
file_urls.append(datum["file_url"])
else:
return Response(file_data)
request.data["room_id"] = room_id
print(request)
serializer = MessageSerializer(data=request.data)
if serializer.is_valid():
data = serializer.data
room_id = data["room_id"] # room id gotten from client request
room = DB.read("dm_rooms", {"_id": room_id})
if room and room.get("status_code", None) == None:
if data["sender_id"] in room.get("room_user_ids", []):
data["media"] = file_urls
response = DB.write("dm_messages", data=data)
if response.get("status", None) == 200:
response_output = {
"status": response["message"],
"event": "message_create",
"message_id": response["data"]["object_id"],
"room_id": room_id,
"thread": False,
"data": {
"sender_id": data["sender_id"],
"message": data["message"],
"created_at": data["created_at"],
"media": data["media"],
},
}
try:
centrifugo_data = centrifugo_client.publish(
room=room_id, 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="message not sent",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
except:
return Response(
data="centrifugo server not available",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
return Response(
data="message not saved and not sent",
status=status.HTTP_424_FAILED_DEPENDENCY,
)
return Response(
"sender not in room", status=status.HTTP_400_BAD_REQUEST
)
return Response("room not found", status=status.HTTP_400_BAD_REQUEST)
return Response(status=status.HTTP_400_BAD_REQUEST)
return Response(status=status.HTTP_400_BAD_REQUEST)