Support Post entities on Matrix side into profile rooms

matrix-profile-room-messages
Jason Robinson 2020-12-27 01:10:31 +02:00
rodzic 0abb33a4fb
commit c40194da95
3 zmienionych plików z 43 dodań i 15 usunięć

Wyświetl plik

@ -133,7 +133,6 @@ class Profile(CreatedAtMixin, OptionalRawContentMixin, PublicMixin, BaseEntity):
url = ""
username = ""
inboxes: Dict = None
mxid = ""
_allowed_children = (Image,)

Wyświetl plik

@ -1,6 +1,7 @@
import json
import logging
from typing import Dict, List
from uuid import uuid4
from federation.entities.base import Post, Profile
from federation.entities.matrix.enums import EventType
@ -14,8 +15,16 @@ logger = logging.getLogger("federation")
class MatrixEntityMixin(BaseEntity):
_event_type: str = None
_profile_room_id = None
_txn_id: str = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# We always require an mxid
self._required.append('mxid')
# Create a transaction ID
self._txn_id = str(uuid4())
@property
def event_type(self) -> str:
return self._event_type
@ -26,7 +35,8 @@ class MatrixEntityMixin(BaseEntity):
# noinspection PyArgumentList
return cls(**get_base_attributes(entity))
def get_endpoint(self, *args, **kwargs) -> str:
# noinspection PyMethodMayBeStatic
def get_endpoint(self) -> str:
config = get_matrix_configuration()
return f"{config['homeserver_base_url']}/_matrix/client/r0"
@ -34,6 +44,10 @@ class MatrixEntityMixin(BaseEntity):
def payloads(self) -> List[Dict]:
return []
@property
def profile_room_alias(self):
return f"#{self.mxid}"
@property
def txn_id(self) -> str:
return self._txn_id
@ -42,21 +56,39 @@ class MatrixEntityMixin(BaseEntity):
class MatrixRoomMessage(Post, MatrixEntityMixin):
_event_type = EventType.ROOM_MESSAGE.value
def get_endpoint(self, fid: str, user_id: str) -> str:
endpoint = super().get_endpoint()
return f"{endpoint}/rooms/{fid}/send/{self.event_type}/{self.txn_id}?user_id={user_id}"
def payloads(self) -> List[Dict]:
payloads = super().payloads()
payloads.append({
"endpoint": f"{super().get_endpoint()}/rooms/{self._profile_room_id}/send/{self.event_type}/"
f"{self.txn_id}?user_id={self.mxid}",
"payload": {
"body": self.raw_content,
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"formatted_body": self.rendered_content,
},
})
return payloads
def pre_send(self):
"""
Get profile room ID.
TODO: we should cache these.
"""
doc, status, error = fetch_document(
url=f"{super().get_endpoint()}/directory/room/{self.profile_room_alias}",
extra_headers=appservice_auth_header(),
)
if status == 200:
data = json.loads(doc)
self._profile_room_id = data["room_id"]
class MatrixProfile(Profile, MatrixEntityMixin):
_profile_room_id = None
_remote_profile_create_needed = False
_remote_room_create_needed = False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# We always require an mxid
self._required.append('mxid')
@property
def localpart(self) -> str:
config = get_matrix_configuration()
@ -115,7 +147,3 @@ class MatrixProfile(Profile, MatrixEntityMixin):
else:
data = json.loads(doc)
self._profile_room_id = data["room_id"]
@property
def profile_room_alias(self):
return f"#{self.mxid}"

Wyświetl plik

@ -29,6 +29,7 @@ class BaseEntity:
guid: str = ""
handle: str = ""
id: str = ""
mxid: str = ""
signature: str = ""
def __init__(self, *args, **kwargs):