Skip to content

Commit

Permalink
do stuff?
Browse files Browse the repository at this point in the history
  • Loading branch information
realsuayip committed Sep 2, 2024
1 parent e7a3464 commit 91e8700
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
55 changes: 39 additions & 16 deletions asu/messaging/models/event.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from __future__ import annotations

from functools import partial
from typing import TYPE_CHECKING, Literal, TypedDict

from django.db import models, transaction
from django.db.models import Q
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from rest_framework import serializers

from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer

from asu.messaging.models.conversation import Conversation
from asu.messaging.models.message import Message
from asu.messaging.models.participation import Participation
from asu.messaging.models.request import ConversationRequest

if TYPE_CHECKING:
Expand All @@ -22,7 +24,7 @@
channel_layer = get_channel_layer()


class MessageEvent(TypedDict):
class WebsocketEvent(TypedDict):
id: int
conversation_id: int
type: Literal["conversation.message"]
Expand Down Expand Up @@ -81,8 +83,7 @@ def dispatch(
)
# Relay events via WebSocket.
for event in events:
relay = partial(event.websocket_send, target.pk)
transaction.on_commit(relay)
transaction.on_commit(event.websocket_send)
return instance


Expand Down Expand Up @@ -135,16 +136,38 @@ class Meta:
def __str__(self) -> str:
return str(self.pk)

def websocket_send(self, target_conversation_id: int) -> None:
send = async_to_sync(channel_layer.group_send)
if self.conversation.is_group:
pass
else:
group = "conversations_%s" % self.conversation.target_id
event = MessageEvent(
id=self.pk,
conversation_id=target_conversation_id,
type="conversation.message",
timestamp=self.date_created.isoformat()[:-6] + "Z",
def timestamp(self) -> str:
return serializers.DateTimeField().to_representation(self.date_created)

def as_websocket_event(self) -> WebsocketEvent:
# todo this will change depending on content type
return WebsocketEvent(
id=self.pk,
conversation_id=self.conversation_id,
type="conversation.message",
timestamp=self.timestamp(),
)

@staticmethod
def get_group(user_id: int) -> str:
return "conversations_%s" % user_id

def websocket_send(self) -> None:
# todo testme
send, event, conversation = (
async_to_sync(channel_layer.group_send),
self.as_websocket_event(),
self.conversation,
)
if conversation.is_group:
# todo: might get more complicated than that
recipients = (
Participation.objects.filter(conversation=conversation)
.values_list("user_id", flat=True)
.iterator()
)
send(group, event)
for recipient in recipients:
send(self.get_group(recipient), event)
else:
assert conversation.holder_id
send(self.get_group(conversation.holder_id), event)
6 changes: 3 additions & 3 deletions asu/messaging/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from channels.generic.websocket import AsyncJsonWebsocketConsumer

from asu.messaging.models.event import MessageEvent
from asu.messaging.models.event import Event, WebsocketEvent


class ConversationConsumer(AsyncJsonWebsocketConsumer):
def get_group(self) -> str:
return "conversations_%s" % self.scope["user_id"]
return Event.get_group(self.scope["user_id"])

async def connect(self) -> None:
await self.channel_layer.group_add(self.get_group(), self.channel_name)
Expand All @@ -16,7 +16,7 @@ async def connect(self) -> None:
async def disconnect(self, code: int) -> None:
await self.channel_layer.group_discard(self.get_group(), self.channel_name)

async def conversation_message(self, event: MessageEvent) -> None:
async def conversation_message(self, event: WebsocketEvent) -> None:
await self.send_json(event)

async def websocket_receive(self, message: dict[str, Any]) -> None:
Expand Down

0 comments on commit 91e8700

Please sign in to comment.