|
| 1 | +# Copyright 2023 LiveKit, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from dataclasses import dataclass, field |
| 16 | +from datetime import datetime |
| 17 | +import json |
| 18 | +import logging |
| 19 | +from typing import Any, Callable, Dict, Literal, Optional |
| 20 | + |
| 21 | +from .room import Room, Participant, DataPacket |
| 22 | +from ._event_emitter import EventEmitter |
| 23 | +from ._proto.room_pb2 import DataPacketKind |
| 24 | +from ._utils import generate_random_base62 |
| 25 | + |
| 26 | +_CHAT_TOPIC = "lk-chat-topic" |
| 27 | +_CHAT_UPDATE_TOPIC = "lk-chat-update-topic" |
| 28 | + |
| 29 | +EventTypes = Literal["message_received",] |
| 30 | + |
| 31 | + |
| 32 | +class ChatManager(EventEmitter[EventTypes]): |
| 33 | + """A utility class that sends and receives chat messages in the active session. |
| 34 | +
|
| 35 | + It implements LiveKit Chat Protocol, and serializes data to/from JSON data packets. |
| 36 | + """ |
| 37 | + |
| 38 | + def __init__(self, room: Room): |
| 39 | + super().__init__() |
| 40 | + self._lp = room.local_participant |
| 41 | + self._room = room |
| 42 | + |
| 43 | + room.on("data_received", self._on_data_received) |
| 44 | + |
| 45 | + def close(self): |
| 46 | + self._room.off("data_received", self._on_data_received) |
| 47 | + |
| 48 | + async def send_message(self, message: str) -> "ChatMessage": |
| 49 | + """Send a chat message to the end user using LiveKit Chat Protocol. |
| 50 | +
|
| 51 | + Args: |
| 52 | + message (str): the message to send |
| 53 | +
|
| 54 | + Returns: |
| 55 | + ChatMessage: the message that was sent |
| 56 | + """ |
| 57 | + msg = ChatMessage( |
| 58 | + message=message, |
| 59 | + is_local=True, |
| 60 | + participant=self._lp, |
| 61 | + ) |
| 62 | + await self._lp.publish_data( |
| 63 | + payload=json.dumps(msg.asjsondict()), |
| 64 | + kind=DataPacketKind.KIND_RELIABLE, |
| 65 | + topic=_CHAT_TOPIC, |
| 66 | + ) |
| 67 | + return msg |
| 68 | + |
| 69 | + async def update_message(self, message: "ChatMessage"): |
| 70 | + """Update a chat message that was previously sent. |
| 71 | +
|
| 72 | + If message.deleted is set to True, we'll signal to remote participants that the message |
| 73 | + should be deleted. |
| 74 | + """ |
| 75 | + await self._lp.publish_data( |
| 76 | + payload=json.dumps(message.asjsondict()), |
| 77 | + kind=DataPacketKind.KIND_RELIABLE, |
| 78 | + topic=_CHAT_UPDATE_TOPIC, |
| 79 | + ) |
| 80 | + |
| 81 | + def on_message(self, callback: Callable[["ChatMessage"], None]): |
| 82 | + """Register a callback to be called when a chat message is received from the end user.""" |
| 83 | + self._callback = callback |
| 84 | + |
| 85 | + def _on_data_received(self, dp: DataPacket): |
| 86 | + # handle both new and updates the same way, as long as the ID is in there |
| 87 | + # the user can decide how to replace the previous message |
| 88 | + if dp.topic == _CHAT_TOPIC or dp.topic == _CHAT_UPDATE_TOPIC: |
| 89 | + try: |
| 90 | + parsed = json.loads(dp.data) |
| 91 | + msg = ChatMessage.from_jsondict(parsed) |
| 92 | + if dp.participant: |
| 93 | + msg.participant = dp.participant |
| 94 | + self.emit("message_received", msg) |
| 95 | + except Exception as e: |
| 96 | + logging.warning("failed to parse chat message: %s", e, exc_info=e) |
| 97 | + |
| 98 | + |
| 99 | +@dataclass |
| 100 | +class ChatMessage: |
| 101 | + message: Optional[str] = None |
| 102 | + id: str = field(default_factory=generate_random_base62) |
| 103 | + timestamp: datetime = field(default_factory=datetime.now) |
| 104 | + deleted: bool = field(default=False) |
| 105 | + |
| 106 | + # These fields are not part of the wire protocol. They are here to provide |
| 107 | + # context for the application. |
| 108 | + participant: Optional[Participant] = None |
| 109 | + is_local: bool = field(default=False) |
| 110 | + |
| 111 | + @classmethod |
| 112 | + def from_jsondict(cls, d: Dict[str, Any]) -> "ChatMessage": |
| 113 | + # older version of the protocol didn't contain a message ID, so we'll create one |
| 114 | + id = d.get("id") or generate_random_base62() |
| 115 | + timestamp = datetime.now() |
| 116 | + if d.get("timestamp"): |
| 117 | + timestamp = datetime.fromtimestamp(d.get("timestamp", 0) / 1000.0) |
| 118 | + msg = cls( |
| 119 | + id=id, |
| 120 | + timestamp=timestamp, |
| 121 | + ) |
| 122 | + msg.update_from_jsondict(d) |
| 123 | + return msg |
| 124 | + |
| 125 | + def update_from_jsondict(self, d: Dict[str, Any]) -> None: |
| 126 | + self.message = d.get("message") |
| 127 | + self.deleted = d.get("deleted", False) |
| 128 | + |
| 129 | + def asjsondict(self): |
| 130 | + """Returns a JSON serializable dictionary representation of the message.""" |
| 131 | + d = { |
| 132 | + "id": self.id, |
| 133 | + "message": self.message, |
| 134 | + "timestamp": int(self.timestamp.timestamp() * 1000), |
| 135 | + } |
| 136 | + if self.deleted: |
| 137 | + d["deleted"] = True |
| 138 | + return d |
0 commit comments