Skip to content

Commit

Permalink
Merge pull request #816 from Harithmetic1/Harithmetic.fe-dev
Browse files Browse the repository at this point in the history
Fixing 505 Error
  • Loading branch information
Harithmetic1 authored Oct 3, 2021
2 parents 9f65335 + a797d77 commit bad82cf
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 257 deletions.
112 changes: 52 additions & 60 deletions backend/channel_plugin/apps/centri/signals/channel_signals.py
Original file line number Diff line number Diff line change
@@ -1,124 +1,116 @@
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
address=settings.CENTRIFUGO_URL,
api_key=settings.CENTRIFUGO_API_KEY,
timeout=3,
verify=True,
)


@receiver(request_finished, sender=ChannelMemberViewset)
def JoinedChannelSignal(sender, **kwargs):
serializer = ChannelMessageSerializer(
data=data, context={"channel_id": channel_id, "org_id": org_id}
)


uid = kwargs.get("dispatch_uid")

if uid == "JoinedChannelSignal":
org_id = kwargs.get("org_id")
channel_id = kwargs.get("channel_id")
user = kwargs.get("user")
user = kwargs.get("user", kwargs.get("added"))

room_name = build_room_name(org_id, channel_id)

data = {
"user_id": user.get("_id"),
"content": "event",
"files": []
}
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:
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")

# required
channelmessage.type = "event"
channelmessage.event = event
channelmessage.can_reply = False
try:
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)
channelmessage = serializer.data.get("channelmessage")
channelmessage.type = "event"
channelmessage.event = event
channelmessage.can_reply = False

# 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")

if uid == "LeftChannelSignal":



org_id = kwargs.get("org_id")
channel_id = kwargs.get("channel_id")
user = kwargs.get("user")

room_name = build_room_name(org_id, channel_id)

try:
CLIENT.unsubscribe(user.get("_id"), room_name)
except CentException:
print("client removal failed because channel is not active")

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

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

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)
channelmessage = serializer.data.get("channelmessage")

# required
channelmessage.type = "event"
channelmessage.event = event
channelmessage.can_reply = False

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

except: # noqa
pass
Loading

0 comments on commit bad82cf

Please sign in to comment.