Register nio callbacks for any class in nio.events
This commit is contained in:
parent
749ebf947c
commit
ca7a47bc19
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import logging as log
|
import logging as log
|
||||||
from contextlib import suppress
|
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import TYPE_CHECKING, Optional, Tuple
|
from typing import TYPE_CHECKING, Optional, Tuple
|
||||||
|
@ -10,9 +9,9 @@ from urllib.parse import quote
|
||||||
|
|
||||||
import nio
|
import nio
|
||||||
|
|
||||||
from . import utils
|
|
||||||
from .html_markdown import HTML_PROCESSOR
|
from .html_markdown import HTML_PROCESSOR
|
||||||
from .models.items import TypeSpecifier
|
from .models.items import TypeSpecifier
|
||||||
|
from .utils import classes_defined_in, plain2html
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .matrix_client import MatrixClient
|
from .matrix_client import MatrixClient
|
||||||
|
@ -20,11 +19,12 @@ if TYPE_CHECKING:
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class NioCallbacks:
|
class NioCallbacks:
|
||||||
"""Register callbacks for nio's request responses and room events.
|
"""Register callbacks for nio's request responses and events.
|
||||||
|
|
||||||
For every `nio.Response` and `nio.Event` subclasses, this class can have a
|
For every class defined in the `nio.responses` and `nio.events` modules,
|
||||||
method named `on<ClassName>` (e.g. `onRoomMessageText`) that will
|
this class can have a method named
|
||||||
automatically be registered in nio.
|
`on<ClassName>` (e.g. `onRoomMessageText`) that will
|
||||||
|
automatically be registered in the `client`'s callbacks.
|
||||||
|
|
||||||
For room event content strings, the `%1` and `%2` placeholders
|
For room event content strings, the `%1` and `%2` placeholders
|
||||||
refer to the event's sender and who this event targets (`state_key`) or
|
refer to the event's sender and who this event targets (`state_key`) or
|
||||||
|
@ -40,19 +40,26 @@ class NioCallbacks:
|
||||||
|
|
||||||
self.models = self.client.models
|
self.models = self.client.models
|
||||||
|
|
||||||
for name, class_ in utils.classes_defined_in(nio.responses).items():
|
for name, response_class in classes_defined_in(nio.responses).items():
|
||||||
with suppress(AttributeError):
|
method = getattr(self, f"on{name}", None)
|
||||||
method = getattr(self, f"on{name}")
|
|
||||||
self.client.add_response_callback(method, class_)
|
|
||||||
|
|
||||||
for name, class_ in utils.classes_defined_in(nio.events).items():
|
if method:
|
||||||
with suppress(AttributeError):
|
self.client.add_response_callback(method, response_class)
|
||||||
method = getattr(self, f"on{name}")
|
|
||||||
self.client.add_event_callback(method, class_)
|
|
||||||
|
|
||||||
self.client.add_ephemeral_callback(
|
for name, event_class in classes_defined_in(nio.events).items():
|
||||||
self.onTypingNoticeEvent, nio.events.TypingNoticeEvent,
|
method = getattr(self, f"on{name}", None)
|
||||||
)
|
|
||||||
|
if not method:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if issubclass(event_class, nio.EphemeralEvent):
|
||||||
|
self.client.add_ephemeral_callback(method, event_class)
|
||||||
|
elif issubclass(event_class, nio.ToDeviceEvent):
|
||||||
|
self.client.add_to_device_callback(method, event_class)
|
||||||
|
elif issubclass(event_class, nio.AccountDataEvent):
|
||||||
|
self.client.add_room_account_data_callback(method, event_class)
|
||||||
|
else:
|
||||||
|
self.client.add_event_callback(method, event_class)
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -106,7 +113,7 @@ class NioCallbacks:
|
||||||
co = HTML_PROCESSOR.filter(
|
co = HTML_PROCESSOR.filter(
|
||||||
ev.formatted_body
|
ev.formatted_body
|
||||||
if ev.format == "org.matrix.custom.html" else
|
if ev.format == "org.matrix.custom.html" else
|
||||||
utils.plain2html(ev.body),
|
plain2html(ev.body),
|
||||||
|
|
||||||
room_id = room.room_id,
|
room_id = room.room_id,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user