|
19 | 19 | from typing import Any, Dict, Literal, Optional |
20 | 20 | import asyncio |
21 | 21 |
|
22 | | -from .room import Room, Participant, DataPacket, TextStreamReader, RemoteParticipant, LocalParticipant |
| 22 | +from .room import ( |
| 23 | + Room, |
| 24 | + Participant, |
| 25 | + DataPacket, |
| 26 | + TextStreamReader, |
| 27 | + RemoteParticipant, |
| 28 | + LocalParticipant, |
| 29 | +) |
23 | 30 | from .event_emitter import EventEmitter |
24 | 31 | from ._utils import generate_random_base62 |
25 | 32 |
|
@@ -101,16 +108,27 @@ def _on_data_received(self, dp: DataPacket): |
101 | 108 | self.emit("message_received", msg) |
102 | 109 | except Exception as e: |
103 | 110 | logging.warning("failed to parse chat message: %s", e, exc_info=e) |
104 | | - |
105 | | - def _on_text_stream_received(self, stream: TextStreamReader, participant_identity: str): |
106 | | - task = asyncio.create_task(self._handle_text_stream(stream, participant_identity)) |
| 111 | + |
| 112 | + def _on_text_stream_received( |
| 113 | + self, stream: TextStreamReader, participant_identity: str |
| 114 | + ): |
| 115 | + task = asyncio.create_task( |
| 116 | + self._handle_text_stream(stream, participant_identity) |
| 117 | + ) |
107 | 118 | self._tasks.append(task) |
108 | 119 | task.add_done_callback(self._tasks.remove) |
109 | 120 |
|
110 | | - async def _handle_text_stream(self, stream: TextStreamReader, participant_identity: str): |
| 121 | + async def _handle_text_stream( |
| 122 | + self, stream: TextStreamReader, participant_identity: str |
| 123 | + ): |
111 | 124 | msg = await ChatMessage.from_text_stream(stream) |
112 | | - participant: RemoteParticipant | LocalParticipant | None = self._room._remote_participants.get(participant_identity) |
113 | | - if participant is None and self._room.local_participant.identity == participant_identity: |
| 125 | + participant: RemoteParticipant | LocalParticipant | None = ( |
| 126 | + self._room._remote_participants.get(participant_identity) |
| 127 | + ) |
| 128 | + if ( |
| 129 | + participant is None |
| 130 | + and self._room.local_participant.identity == participant_identity |
| 131 | + ): |
114 | 132 | participant = self._room.local_participant |
115 | 133 | msg.is_local = True |
116 | 134 | msg.participant = participant |
@@ -157,13 +175,13 @@ def asjsondict(self): |
157 | 175 | if self.deleted: |
158 | 176 | d["deleted"] = True |
159 | 177 | return d |
160 | | - |
| 178 | + |
161 | 179 | @classmethod |
162 | 180 | async def from_text_stream(cls, stream: TextStreamReader): |
163 | 181 | message_text = await stream.read_all() |
164 | 182 | timestamp = datetime.fromtimestamp(stream.info.timestamp / 1000.0) |
165 | 183 | return cls( |
166 | | - message=message_text, |
167 | | - timestamp=timestamp, |
168 | | - id=stream.info.stream_id, |
| 184 | + message=message_text, |
| 185 | + timestamp=timestamp, |
| 186 | + id=stream.info.stream_id, |
169 | 187 | ) |
0 commit comments