-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #816 from Harithmetic1/Harithmetic.fe-dev
Fixing 505 Error
- Loading branch information
Showing
2 changed files
with
283 additions
and
257 deletions.
There are no files selected for viewing
112 changes: 52 additions & 60 deletions
112
backend/channel_plugin/apps/centri/signals/channel_signals.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.