-
Notifications
You must be signed in to change notification settings - Fork 24
Send websocket message for comments #3835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
itsisak
wants to merge
5
commits into
master
Choose a base branch
from
comment-websockets
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,4 @@ | ||
SOCKET_ADD_SUCCESS = "Comment.SOCKET_ADD.SUCCESS" | ||
SOCKET_ADD_FAILURE = "Comment.SOCKET_ADD.FAILURE" | ||
SOCKET_DELETE_SUCCESS = "Comment.SOCKET_DELETE.SUCCESS" | ||
SOCKET_DELETE_FAILURE = "Comment.SOCKET_DELETE.FAILURE" |
Empty file.
File renamed without changes.
This file contains hidden or 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,6 @@ | ||
from lego.apps.comments.serializers.comments import CommentSerializer | ||
from lego.apps.websockets.serializers import WebsocketSerializer | ||
|
||
|
||
class CommentSocketSerializer(WebsocketSerializer): | ||
payload = CommentSerializer() |
This file contains hidden or 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 hidden or 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,24 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from lego.apps.comments.serializers.sockets import CommentSocketSerializer | ||
from lego.apps.websockets.groups import group_for_content_model | ||
from lego.apps.websockets.notifiers import notify_group | ||
|
||
if TYPE_CHECKING: | ||
from lego.apps.comments.models import Comment | ||
|
||
|
||
def notify_comment(action_type: str, comment: Comment, **kwargs): | ||
# group = "global" | ||
group = group_for_content_model(comment) | ||
serializer = CommentSocketSerializer( | ||
{ | ||
"type": action_type, | ||
"payload": comment, | ||
"meta": kwargs, | ||
} | ||
) | ||
data = serializer.data | ||
notify_group(group, data) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
WS_STATUS_CONNECTED = "CONNECTED" | ||
WS_STATUS_CLOSED = "CLOSED" | ||
WS_STATUS_ERROR = "ERROR" | ||
|
||
WS_GROUP_TYPES = ["global" "user", "event", "comment"] | ||
|
||
WS_GROUP_JOIN_BEGIN = "Websockets.GROUP_JOIN.BEGIN" | ||
WS_GROUP_JOIN_SUCCESS = "Websockets.GROUP_JOIN.SUCCESS" | ||
WS_GROUP_JOIN_FAILURE = "Websockets.GROUP_JOIN.FAILURE" | ||
|
||
WS_GROUP_LEAVE_BEGIN = "Websockets.GROUP_LEAVE.BEGIN" | ||
WS_GROUP_LEAVE_SUCCESS = "Websockets.GROUP_LEAVE.SUCCESS" | ||
WS_GROUP_LEAVE_FAILURE = "Websockets.GROUP_LEAVE.FAILURE" |
This file contains hidden or 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,24 +1,76 @@ | ||
from asgiref.sync import AsyncToSync | ||
from channels.generic.websocket import WebsocketConsumer | ||
from channels.generic.websocket import JsonWebsocketConsumer | ||
|
||
from lego.apps.events.websockets import find_event_groups | ||
from lego.apps.users.models import User | ||
from lego.apps.websockets.groups import group_for_user | ||
from lego.apps.websockets import constants | ||
from lego.apps.websockets.groups import group_for_user, verify_group_access | ||
|
||
|
||
def find_groups(user: User): | ||
return ["global", group_for_user(user.pk)] + find_event_groups(user) | ||
|
||
|
||
class GroupConsumer(WebsocketConsumer): | ||
class GroupConsumer(JsonWebsocketConsumer): | ||
""" | ||
Custom consumer for handling websocket groups. | ||
|
||
Create own logic for tracking user groups as the WebsocketConsumer groups functionality | ||
does not have access to the user object. | ||
""" | ||
|
||
user_groups = set() | ||
user = None | ||
|
||
def debug(self, message): | ||
if self.user: | ||
print(f"[{self.user.username.upper()}] {message}") | ||
else: | ||
print(f"[NO USER IN SCOPE] {message}") | ||
|
||
def connect(self): | ||
self.accept() | ||
for group in find_groups(self.scope["user"]): | ||
user = self.scope["user"] | ||
for group in find_groups(user): | ||
AsyncToSync(self.channel_layer.group_add)(group, self.channel_name) | ||
self.user = user | ||
self.user_groups = set(find_groups(user)) | ||
self.accept() | ||
|
||
def disconnect(self, message): | ||
for group in find_groups(self.scope["user"]): | ||
def disconnect(self, code): | ||
for group in self.user_groups: | ||
AsyncToSync(self.channel_layer.group_discard)(group, self.channel_name) | ||
self.user_groups.clear() | ||
|
||
def receive_json(self, content, **kwargs): | ||
type = content.get("type") | ||
payload = content.get("payload") | ||
|
||
if type == constants.WS_GROUP_JOIN_BEGIN: | ||
group = payload.get("group") | ||
if self.user and verify_group_access(self.user, group): | ||
AsyncToSync(self.channel_layer.group_add)(group, self.channel_name) | ||
self.user_groups.add(group) | ||
self.send_message( | ||
constants.WS_GROUP_JOIN_SUCCESS, payload={"group": group} | ||
) | ||
else: | ||
self.send_message( | ||
constants.WS_GROUP_JOIN_FAILURE, payload={"group": group} | ||
) | ||
|
||
if type == constants.WS_GROUP_LEAVE_BEGIN: | ||
group = payload.get("group") | ||
if group in self.groups: | ||
AsyncToSync(self.channel_layer.group_discard)(group, self.channel_name) | ||
self.user_groups.remove(group) | ||
self.send_message(constants.WS_GROUP_LEAVE_SUCCESS) | ||
|
||
def send_message(self, type: str, payload=None, meta=None): | ||
""" | ||
Send message on standardised format. | ||
""" | ||
content = {"type": type, "payload": payload, "meta": meta} | ||
self.send_json(content) | ||
|
||
def notification_message(self, event): | ||
self.send(text_data=event["text"]) |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dont know why
GenericRelationField
was write-only, it prevented comments from returningcontent_target
. If it should stay how it was, it is easy enough to add it to the websocket meta data instead of this.