-
Notifications
You must be signed in to change notification settings - Fork 3
poc: Websockets for user notifications #1460
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ services: | |
build: | ||
context: . | ||
dockerfile: Dockerfile.dev | ||
command: python terraso_backend/manage.py runserver 0.0.0.0:${PORT} | ||
command: daphne -b 0.0.0.0 -p ${PORT} "terraso_backend.config.asgi:application" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: on render, we have
With this change, can we eliminate this override? Or do we change it to something else? |
||
labels: | ||
org.techmatters.project: terraso_backend | ||
env_file: .env | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
asgiref | ||
boto3 | ||
channels | ||
daphne | ||
dj-database-url | ||
django | ||
django-cors-headers | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
from apps.core.gis.mapbox import get_publish_status | ||
from apps.core.models import Group, Landscape | ||
from apps.graphql.exceptions import GraphQLNotAllowedException | ||
from apps.notifications.websocket import notify_user | ||
from apps.shared_data.models.data_entries import DataEntry | ||
from apps.shared_data.models.visualization_config import VisualizationConfig | ||
from apps.shared_data.visualization_tileset_tasks import ( | ||
|
@@ -144,6 +145,18 @@ def get_queryset(cls, queryset, info): | |
def resolve_mapbox_tileset_id(self, info): | ||
if self.mapbox_tileset_id is None: | ||
return None | ||
|
||
shared_resources = self.data_entry.shared_resources.all() | ||
users = [] | ||
for shared_resource in shared_resources: | ||
members = ( | ||
shared_resource.target.membership_list.members.all() | ||
) # TODO Get only approved memberships | ||
ids = [member.id for member in members] | ||
users = set(users + ids) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: |
||
for user_id in users: | ||
notify_user(user_id, f"Updated: {self.mapbox_tileset_id}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: should the message be structured data? suggestion: indicate that a tileset was updated. we may have other kinds of updates in the future. |
||
|
||
if self.mapbox_tileset_status == VisualizationConfig.MAPBOX_TILESET_READY: | ||
return self.mapbox_tileset_id | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright © 2024 Technology Matters | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published | ||
# by the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see https://www.gnu.org/licenses/. | ||
|
||
from django.urls import path | ||
|
||
from apps.notifications.websocket import NotificationsConsumer | ||
|
||
websocket_urlpatterns = [ | ||
path("ws/notifications/", NotificationsConsumer.as_asgi()), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright © 2024 Technology Matters | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published | ||
# by the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see https://www.gnu.org/licenses/. | ||
|
||
import json | ||
|
||
from asgiref.sync import async_to_sync | ||
from channels.generic.websocket import AsyncWebsocketConsumer | ||
from channels.layers import get_channel_layer | ||
|
||
|
||
class NotificationsConsumer(AsyncWebsocketConsumer): | ||
async def connect(self): | ||
user = self.scope["user"] | ||
self.group_name = f"notifications_{user.id}" | ||
|
||
await self.channel_layer.group_add(self.group_name, self.channel_name) | ||
|
||
await self.accept() | ||
|
||
async def disconnect(self, close_code): | ||
await self.channel_layer.group_discard(self.group_name, self.channel_name) | ||
|
||
# Function to send updates | ||
async def send_update(self, event): | ||
message = event["message"] | ||
await self.send(text_data=json.dumps({"message": message})) | ||
|
||
|
||
def notify_user(user_id, message): | ||
channel_layer = get_channel_layer() | ||
async_to_sync(channel_layer.group_send)( | ||
f"notifications_{user_id}", | ||
{"type": "send_update", "message": message}, | ||
) |
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.
question: what do we need
--allow-unsafe
for?