-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement websocket channel using for monitor report 's comments and …
…new created report.
- Loading branch information
Showing
12 changed files
with
215 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from channels.exceptions import DenyConnection | ||
from channels.generic.websocket import AsyncWebsocketConsumer | ||
|
||
from common.utils import extract_jwt_payload_from_asgi_scope | ||
|
||
|
||
def new_report_group_name(authority_id): | ||
return f"rp_{authority_id}" | ||
|
||
|
||
class NewReportConsumers(AsyncWebsocketConsumer): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(args, kwargs) | ||
self.username = None | ||
self.authority_id = None | ||
self.group_name = None | ||
|
||
async def connect(self): | ||
self.authority_id = self.scope["url_route"]["kwargs"]["authority_id"] | ||
self.group_name = new_report_group_name(self.authority_id) | ||
payload = extract_jwt_payload_from_asgi_scope(self.scope) | ||
self.username = payload["username"] | ||
|
||
if self.username: | ||
await self.channel_layer.group_add( | ||
self.group_name, | ||
self.channel_name, | ||
) | ||
await self.accept() | ||
else: | ||
raise DenyConnection("invalid token") | ||
|
||
async def disconnect(self, code): | ||
await self.channel_layer.group_discard( | ||
self.group_name, | ||
self.channel_name, | ||
) | ||
|
||
async def new_report(self, event): | ||
await self.send(text_data=event["text"]) |
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
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.urls import re_path | ||
|
||
from . import consumers | ||
|
||
websocket_urlpatterns = [ | ||
re_path( | ||
r"ws/reports/(?P<authority_id>\w+)/$", consumers.NewReportConsumers.as_asgi() | ||
), | ||
] |
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import json | ||
|
||
import channels | ||
from asgiref.sync import async_to_sync | ||
from django.db.models.signals import m2m_changed | ||
from django.dispatch import receiver | ||
|
||
from accounts.models import Authority | ||
from reports.consumers import new_report_group_name | ||
from reports.models import IncidentReport | ||
|
||
|
||
@receiver( | ||
m2m_changed, | ||
sender=IncidentReport.relevant_authorities.through, | ||
dispatch_id="report_signal_to_ws", | ||
) | ||
def on_create_report(sender, instance, action, reverse, model, pk_set, **kwargs): | ||
if action == "post_add": | ||
report_id = instance.id | ||
for authority_id in pk_set: | ||
for authority in Authority.objects.get(pk=authority_id).all_inherits_up(): | ||
group_name = new_report_group_name(authority.id) | ||
channel_layer = channels.layers.get_channel_layer() | ||
async_to_sync(channel_layer.group_send)( | ||
group_name, | ||
{ | ||
"type": "new.report", | ||
"text": json.dumps( | ||
{ | ||
"report_id": report_id, | ||
} | ||
), | ||
}, | ||
) |
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import json | ||
|
||
from channels.exceptions import DenyConnection | ||
from channels.generic.websocket import AsyncWebsocketConsumer | ||
from django.http import parse_cookie | ||
from graphql_jwt.utils import get_payload, jwt_decode | ||
|
||
from common.utils import extract_jwt_payload_from_asgi_scope | ||
|
||
|
||
def new_comment_group_name(thread_id): | ||
return f"cm_{thread_id}" | ||
|
||
|
||
class NewCommentConsumers(AsyncWebsocketConsumer): | ||
def __init__(self, *args, **kwargs): | ||
self.username = None | ||
self.authority_id = None | ||
self.group_name = None | ||
super().__init__(*args, **kwargs) | ||
|
||
async def connect(self): | ||
thread_id = self.scope["url_route"]["kwargs"]["thread_id"] | ||
self.group_name = new_comment_group_name(thread_id) | ||
payload = extract_jwt_payload_from_asgi_scope(self.scope) | ||
self.username = payload["username"] | ||
self.authority_id = payload["authority_id"] | ||
|
||
if self.username: | ||
await self.channel_layer.group_add( | ||
self.group_name, | ||
self.channel_name, | ||
) | ||
await self.accept() | ||
else: | ||
raise DenyConnection("invalid token") | ||
|
||
async def disconnect(self, code): | ||
await self.channel_layer.group_discard( | ||
self.group_name, | ||
self.channel_name, | ||
) | ||
|
||
async def update_comment(self, event): | ||
await self.send(text_data=event["text"]) |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.urls import re_path | ||
|
||
from . import consumers | ||
|
||
websocket_urlpatterns = [ | ||
re_path( | ||
r"ws/comments/(?P<thread_id>\w+)/$", consumers.NewCommentConsumers.as_asgi() | ||
), | ||
] |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import json | ||
|
||
import channels.layers | ||
from asgiref.sync import async_to_sync | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
|
||
from threads.consumers import new_comment_group_name | ||
from threads.models import Comment | ||
|
||
|
||
@receiver(post_save, sender=Comment, dispatch_uid="comment_signal_to_ws") | ||
def on_comment_update(sender, instance, **kwargs): | ||
thread_id = instance.thread_id | ||
group_name = new_comment_group_name(thread_id) | ||
channel_layer = channels.layers.get_channel_layer() | ||
async_to_sync(channel_layer.group_send)( | ||
group_name, | ||
{ | ||
"type": "update.comment", | ||
"text": json.dumps( | ||
{ | ||
"thread_id": thread_id, | ||
} | ||
), | ||
}, | ||
) |