Start adding basic Matrix entities

matrix-delivery
Jason Robinson 2020-12-24 01:16:31 +02:00
rodzic 82f3aed21b
commit f9d03fd916
3 zmienionych plików z 76 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,37 @@
import logging
from federation.entities.base import Post, Profile
from federation.entities.mixins import BaseEntity
from federation.entities.utils import get_base_attributes
logger = logging.getLogger("federation")
class MatrixEntityMixin(BaseEntity):
_event_type = None
_state_key = None
@property
def event_type(self):
return self._event_type
@classmethod
def from_base(cls, entity):
# noinspection PyArgumentList
return cls(**get_base_attributes(entity))
@property
def state_key(self):
return self._state_key
def to_string(self):
# noinspection PyUnresolvedReferences
return ""
class MatrixRoomMessage(Post, MatrixEntityMixin):
pass
class MatrixProfile(Profile, MatrixEntityMixin):
pass

Wyświetl plik

@ -0,0 +1,38 @@
import logging
from federation.entities.base import Profile, Post
from federation.entities.matrix.entities import MatrixRoomMessage, MatrixProfile
from federation.entities.mixins import BaseEntity
logger = logging.getLogger("federation")
def get_outbound_entity(entity: BaseEntity, private_key):
"""Get the correct outbound entity for this protocol.
:arg entity: An entity instance which can be of a base or protocol entity class.
:arg private_key: Private key of sender in str format
:returns: Protocol specific entity class instance.
:raises ValueError: If conversion cannot be done.
"""
if getattr(entity, "outbound_doc", None):
# If the entity already has an outbound doc, just return the entity as is
return entity
outbound = None
cls = entity.__class__
if cls in [
MatrixRoomMessage,
]:
# Already fine
outbound = entity
elif cls == Post:
outbound = MatrixRoomMessage.from_base(entity)
elif cls == Profile:
outbound = MatrixProfile.from_base(entity)
if not outbound:
raise ValueError("Don't know how to convert this base entity to Matrix protocol entities.")
if hasattr(outbound, "pre_send"):
outbound.pre_send()
# Validate the entity
outbound.validate(direction="outbound")
return outbound

Wyświetl plik

@ -322,6 +322,7 @@ def handle_send(
if not matrix_config:
matrix_config = get_matrix_configuration()
user_id = f"@{author_user.username}:{matrix_config['homeserver_name']}"
# noinspection PyUnresolvedReferences
payloads.append({
"auth": None,
"headers": {