Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into dev
  • Loading branch information
Valenteeena committed Oct 3, 2021
2 parents dddd2d7 + 0071385 commit 8344382
Show file tree
Hide file tree
Showing 9 changed files with 556 additions and 428 deletions.
85 changes: 39 additions & 46 deletions backend/channel_plugin/apps/centri/signals/channel_signals.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
from django.core.signals import request_finished
from django.dispatch import receiver
from django.utils import timezone
from django.conf import settings

from cent import CentException

from apps.centri.centwrapper import CentClient
from apps.channels.views import ChannelMemberViewset
from apps.channelmessages.serializers import ChannelMessageSerializer
from apps.centri.helperfuncs import build_room_name

from apps.channelmessages.serializers import ChannelMessageSerializer
from apps.channels.views import ChannelMemberViewset
from cent import CentException
from django.conf import settings
from django.core.signals import request_finished
from django.dispatch import receiver

CLIENT = CentClient(
address=settings.CENTRIFUGO_URL,
api_key=settings.CENTRIFUGO_API_KEY,
timeout=3,
verify=True
verify=True,
)


Expand All @@ -31,39 +27,44 @@ def JoinedChannelSignal(sender, **kwargs):

room_name = build_room_name(org_id, channel_id)

data = {
"user_id": user.get("_id"),
"content": "event",
"files": []
}

event = {
"action": "join:channel",
"recipients": kwargs.get("added", [user])
}
if user:
data = {"user_id": user.get("_id"), "content": "event", "files": []}
else:
if not kwargs.get("added"):
return None
else:
try:
data = {
"user_id": kwargs.get("added")[0].get("_id"),
"content": "event",
"files": [],
}
except: # noqa
return None

event = {"action": "join:channel", "recipients": kwargs.get("added", [user])}

serializer = ChannelMessageSerializer(
data=data,
context={"channel_id": channel_id, "org_id": org_id}
)

serializer.is_valid(raise_exception=True)
channelmessage = serializer.data.get("channelmessage")
try:
serializer = ChannelMessageSerializer(
data=data, context={"channel_id": channel_id, "org_id": org_id}
)

# required
channelmessage.type = "event"
channelmessage.event = event
channelmessage.can_reply = False
serializer.is_valid(raise_exception=True)
channelmessage = serializer.data.get("channelmessage")
channelmessage.type = "event"
channelmessage.event = event
channelmessage.can_reply = False

try:
# required
result = channelmessage.create(org_id)
print("\n")
print(result)
print("\n")
CLIENT.publish(room_name, result)
except:
except: # noqa
pass


@receiver(request_finished, sender=ChannelMemberViewset)
def LeftChannelSignal(sender, **kwargs):
uid = kwargs.get("dispatch_uid")
Expand All @@ -81,20 +82,12 @@ def LeftChannelSignal(sender, **kwargs):
except CentException:
print("client removal failed because channel is not active")

data = {
"user_id": user.get("_id"),
"content": "event",
"files": []
}
data = {"user_id": user.get("_id"), "content": "event", "files": []}

event = {
"action": "leave:channel",
"recipients": kwargs.get("removed", [user])
}
event = {"action": "leave:channel", "recipients": kwargs.get("removed", [user])}

serializer = ChannelMessageSerializer(
data=data,
context={"channel_id": channel_id, "org_id": org_id}
data=data, context={"channel_id": channel_id, "org_id": org_id}
)

serializer.is_valid(raise_exception=True)
Expand All @@ -111,5 +104,5 @@ def LeftChannelSignal(sender, **kwargs):
print(result)
print("\n")
CLIENT.publish(room_name, result)
except:
except: # noqa
pass
92 changes: 70 additions & 22 deletions backend/channel_plugin/apps/channels/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status, throttling
from rest_framework.decorators import action, throttle_classes, api_view, permission_classes
from rest_framework.decorators import action, api_view, throttle_classes
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet

from channel_plugin.utils.customexceptions import ThrottledViewSet
from channel_plugin.utils.customrequest import Request, find_match_in_db, manage_channel_permissions, get_channel_permissions
from channel_plugin.utils.customrequest import (
Request,
find_match_in_db,
manage_channel_permissions,
)
from channel_plugin.utils.wrappers import OrderMixin

from .serializers import ( # SearchMessageQuerySerializer,
ChannelAllFilesSerializer,
ChannelGetSerializer,
ChannelPermissions,
ChannelSerializer,
ChannelUpdateSerializer,
NotificationsSettingSerializer,
RoomSerializer,
SocketSerializer,
UserChannelGetSerializer,
UserSerializer,
RoomSerializer,
ChannelPermissions,
)

# from rest_framework.filters
Expand Down Expand Up @@ -117,6 +121,41 @@ def create_room(self, request):
return Response(result, status=status_code)


@swagger_auto_schema(
operation_id="create-room",
request_body=RoomSerializer,
responses={
201: openapi.Response("Response", RoomSerializer),
404: openapi.Response("Error Response", ErrorSerializer),
},
)
@throttle_classes([throttling.AnonRateThrottle])
@action(
methods=["POST"],
detail=False,
)
def create_room(self, request):
serializer = RoomSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
channel_serializer = serializer.convert_to_channel_serializer()
channel_serializer.is_valid()
print(channel_serializer)
channel = channel_serializer.data.get("channel")
result = channel.create(serializer.data.get("ord_id"))
status_code = status.HTTP_404_NOT_FOUND

if result.__contains__("_id"):
request_finished.send(
sender=None,
dispatch_uid="UpdateSidebarSignal",
org_id=channel_serializer.data.get("ord_id"),
user_id=result.get("owner"),
)
status_code = status.HTTP_201_CREATED
return Response(serializer.data, status=status_code)
else:
return Response(result, status=status_code)

@swagger_auto_schema(
operation_id="retrieve-channels",
responses={
Expand Down Expand Up @@ -289,7 +328,7 @@ def channel_update(self, request, org_id, channel_id):
):
if result.__contains__("_id"):
result.update({"members": len(result["users"].keys())})
#TODO: make this block asynchronus
# TODO: make this block asynchronus
# for user_id in result["users"].keys():
# request_finished.send(
# sender=None,
Expand Down Expand Up @@ -435,11 +474,9 @@ def get_channel_socket_name(self, request, org_id, channel_id):
}
)

channel_list_zc_main_views = ChannelViewset.as_view(
{
"post": "create_room"
}
)

channel_list_zc_main_views = ChannelViewset.as_view({"post": "create_room"})


channel_retrieve_update_delete_view = ChannelViewset.as_view(
{"get": "channel_retrieve", "put": "channel_update", "delete": "channel_delete"}
Expand Down Expand Up @@ -605,6 +642,15 @@ def update_notification(self, request, org_id, channel_id, member_id):
404: openapi.Response("Collection Not Found"),
},
operation_id="add-channel-members",
# manual_parameters=[
# openapi.Parameter(
# "user_id",
# openapi.IN_QUERY,
# description="User ID (id of active user)",
# required=True,
# type=openapi.TYPE_STRING,
# ),
# ],
)
@action(
methods=["POST"],
Expand Down Expand Up @@ -691,7 +737,7 @@ def add_member(self, request, org_id, channel_id):
# add all users not in group
for user in user_list:
if channel["users"].get(user["_id"]):
user_list.pop(user)
user_list.remove(user)
else:
channel["users"].update({f"{user['_id']}": user})

Expand Down Expand Up @@ -734,15 +780,15 @@ def add_member(self, request, org_id, channel_id):
channel_id=channel_id,
user=output,
)

try:
request_finished.send(
sender=None,
dispatch_uid="UpdateSidebarSignal",
org_id=org_id,
user_id=user.get("_id"),
)
except:
except: # noqa
pass

else:
Expand All @@ -752,10 +798,11 @@ def add_member(self, request, org_id, channel_id):
dispatch_uid="JoinedChannelSignal",
org_id=org_id,
channel_id=channel_id,
added_by="logged-in-user_id",
# added_by=request.query_params.get("user_id"),
added=output,
)
return Response(output, status=status.HTTP_201_CREATED)
status_code = status.HTTP_201_CREATED if output else status.HTTP_200_OK
return Response(output, status=status_code)
else:
return Response(
result.get("error"), status=status.HTTP_400_BAD_REQUEST
Expand Down Expand Up @@ -1036,7 +1083,7 @@ def remove_member(self, request, org_id, channel_id, member_id):
org_id=org_id,
user_id=user_data.get("_id"),
)
except:
except: # noqa
pass

status_code = (
Expand Down Expand Up @@ -1076,18 +1123,19 @@ def remove_member(self, request, org_id, channel_id, member_id):
)


@api_view(["POST","GET"])
@api_view(["POST", "GET"])
# @permission_classes(["IsAdmin"])
def handle_channel_permissions(request, org_id, channel_id):
if request.method == "GET":
data = find_match_in_db(org_id, "channelpermissions", "channel_id", channel_id,return_data=True)
data = find_match_in_db(
org_id, "channelpermissions", "channel_id", channel_id, return_data=True
)
return Response(data, status=status.HTTP_200_OK)
serializer = ChannelPermissions(data = request.data)
serializer = ChannelPermissions(data=request.data)
if serializer.is_valid():
payload = dict(serializer.validated_data)
payload.update({"channel_id":channel_id})
payload.update({"channel_id": channel_id})
data = manage_channel_permissions(org_id, channel_id, payload)
response = payload

return Response(data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
1 change: 1 addition & 0 deletions backend/channel_plugin/channel_plugin/info/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def info_sidebar(self, request):
data = {
"name": "Channels Plugin",
"description": description,
"button_url": "/channels",
"plugin_id": settings.PLUGIN_ID,
}
if org_id is not None and user_id is not None:
Expand Down
Loading

0 comments on commit 8344382

Please sign in to comment.