Sending messages and local echo
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
@@ -17,7 +18,15 @@ class Event:
|
||||
# CPython >= 3.6 or any Python >= 3.7 needed for correct dict order
|
||||
args = [
|
||||
# pylint: disable=no-member
|
||||
getattr(self, field)
|
||||
self._process_field(getattr(self, field))
|
||||
for field in self.__dataclass_fields__ # type: ignore
|
||||
]
|
||||
pyotherside.send(type(self).__name__, *args)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _process_field(value: Any) -> Any:
|
||||
if hasattr(value, "__class__") and issubclass(value.__class__, Enum):
|
||||
return value.value
|
||||
|
||||
return value
|
||||
|
@@ -54,27 +54,26 @@ class ContentType(AutoStrEnum):
|
||||
location = auto()
|
||||
|
||||
|
||||
|
||||
|
||||
@dataclass
|
||||
class TimelineEventReceived(Event):
|
||||
event_type: Type[nio.Event] = field()
|
||||
room_id: str = field()
|
||||
event_id: str = field()
|
||||
sender_id: str = field()
|
||||
date: datetime = field()
|
||||
content: str = field()
|
||||
|
||||
content_type: ContentType = ContentType.html
|
||||
is_local_echo: bool = False
|
||||
event_type: Type[nio.Event] = field()
|
||||
room_id: str = field()
|
||||
event_id: str = field()
|
||||
sender_id: str = field()
|
||||
date: datetime = field()
|
||||
content: str = field()
|
||||
content_type: ContentType = ContentType.html
|
||||
is_local_echo: bool = False
|
||||
|
||||
show_name_line: bool = False
|
||||
translatable: Union[bool, Sequence[str]] = True
|
||||
|
||||
target_user_id: Optional[str] = None
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_nio(cls, room: nio.rooms.MatrixRoom, ev: nio.Event, **fields
|
||||
) -> "TimelineEventReceived":
|
||||
def from_nio(cls, room, ev, **fields) -> "TimelineEventReceived":
|
||||
return cls(
|
||||
event_type = type(ev),
|
||||
room_id = room.room_id,
|
||||
|
@@ -5,6 +5,7 @@ import json
|
||||
import logging as log
|
||||
import platform
|
||||
from contextlib import suppress
|
||||
from datetime import datetime
|
||||
from types import ModuleType
|
||||
from typing import Dict, Optional, Type
|
||||
|
||||
@@ -22,9 +23,10 @@ class MatrixClient(nio.AsyncClient):
|
||||
user: str,
|
||||
homeserver: str = "https://matrix.org",
|
||||
device_id: Optional[str] = None) -> None:
|
||||
|
||||
# TODO: ensure homeserver starts with a scheme://
|
||||
|
||||
self.sync_task: Optional[asyncio.Future] = None
|
||||
|
||||
super().__init__(homeserver=homeserver, user=user, device_id=device_id)
|
||||
|
||||
self.connect_callbacks()
|
||||
@@ -113,6 +115,27 @@ class MatrixClient(nio.AsyncClient):
|
||||
)
|
||||
|
||||
|
||||
async def send_markdown(self, room_id: str, text: str) -> None:
|
||||
content = {
|
||||
"body": text,
|
||||
"formatted_body": HTML_FILTER.from_markdown(text),
|
||||
"format": "org.matrix.custom.html",
|
||||
"msgtype": "m.text",
|
||||
}
|
||||
|
||||
TimelineMessageReceived(
|
||||
event_type = nio.RoomMessageText,
|
||||
room_id = room_id,
|
||||
event_id = "local_echo",
|
||||
sender_id = self.user_id,
|
||||
date = datetime.now(),
|
||||
content = content["formatted_body"],
|
||||
is_local_echo = True,
|
||||
)
|
||||
|
||||
await self.room_send(room_id, "m.room.message", content)
|
||||
|
||||
|
||||
# Callbacks for nio responses
|
||||
|
||||
@staticmethod
|
||||
@@ -172,6 +195,7 @@ class MatrixClient(nio.AsyncClient):
|
||||
ev.formatted_body
|
||||
if ev.format == "org.matrix.custom.html" else html.escape(ev.body)
|
||||
)
|
||||
|
||||
TimelineMessageReceived.from_nio(room, ev, content=co)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user