kopia lustrzana https://gitlab.com/jaywink/federation
Support Post entities on Matrix side into profile rooms
rodzic
0abb33a4fb
commit
c40194da95
|
@ -133,7 +133,6 @@ class Profile(CreatedAtMixin, OptionalRawContentMixin, PublicMixin, BaseEntity):
|
||||||
url = ""
|
url = ""
|
||||||
username = ""
|
username = ""
|
||||||
inboxes: Dict = None
|
inboxes: Dict = None
|
||||||
mxid = ""
|
|
||||||
|
|
||||||
_allowed_children = (Image,)
|
_allowed_children = (Image,)
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
from federation.entities.base import Post, Profile
|
from federation.entities.base import Post, Profile
|
||||||
from federation.entities.matrix.enums import EventType
|
from federation.entities.matrix.enums import EventType
|
||||||
|
@ -14,8 +15,16 @@ logger = logging.getLogger("federation")
|
||||||
|
|
||||||
class MatrixEntityMixin(BaseEntity):
|
class MatrixEntityMixin(BaseEntity):
|
||||||
_event_type: str = None
|
_event_type: str = None
|
||||||
|
_profile_room_id = None
|
||||||
_txn_id: str = 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
|
@property
|
||||||
def event_type(self) -> str:
|
def event_type(self) -> str:
|
||||||
return self._event_type
|
return self._event_type
|
||||||
|
@ -26,7 +35,8 @@ class MatrixEntityMixin(BaseEntity):
|
||||||
# noinspection PyArgumentList
|
# noinspection PyArgumentList
|
||||||
return cls(**get_base_attributes(entity))
|
return cls(**get_base_attributes(entity))
|
||||||
|
|
||||||
def get_endpoint(self, *args, **kwargs) -> str:
|
# noinspection PyMethodMayBeStatic
|
||||||
|
def get_endpoint(self) -> str:
|
||||||
config = get_matrix_configuration()
|
config = get_matrix_configuration()
|
||||||
return f"{config['homeserver_base_url']}/_matrix/client/r0"
|
return f"{config['homeserver_base_url']}/_matrix/client/r0"
|
||||||
|
|
||||||
|
@ -34,6 +44,10 @@ class MatrixEntityMixin(BaseEntity):
|
||||||
def payloads(self) -> List[Dict]:
|
def payloads(self) -> List[Dict]:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def profile_room_alias(self):
|
||||||
|
return f"#{self.mxid}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def txn_id(self) -> str:
|
def txn_id(self) -> str:
|
||||||
return self._txn_id
|
return self._txn_id
|
||||||
|
@ -42,21 +56,39 @@ class MatrixEntityMixin(BaseEntity):
|
||||||
class MatrixRoomMessage(Post, MatrixEntityMixin):
|
class MatrixRoomMessage(Post, MatrixEntityMixin):
|
||||||
_event_type = EventType.ROOM_MESSAGE.value
|
_event_type = EventType.ROOM_MESSAGE.value
|
||||||
|
|
||||||
def get_endpoint(self, fid: str, user_id: str) -> str:
|
def payloads(self) -> List[Dict]:
|
||||||
endpoint = super().get_endpoint()
|
payloads = super().payloads()
|
||||||
return f"{endpoint}/rooms/{fid}/send/{self.event_type}/{self.txn_id}?user_id={user_id}"
|
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):
|
class MatrixProfile(Profile, MatrixEntityMixin):
|
||||||
_profile_room_id = None
|
|
||||||
_remote_profile_create_needed = False
|
_remote_profile_create_needed = False
|
||||||
_remote_room_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
|
@property
|
||||||
def localpart(self) -> str:
|
def localpart(self) -> str:
|
||||||
config = get_matrix_configuration()
|
config = get_matrix_configuration()
|
||||||
|
@ -115,7 +147,3 @@ class MatrixProfile(Profile, MatrixEntityMixin):
|
||||||
else:
|
else:
|
||||||
data = json.loads(doc)
|
data = json.loads(doc)
|
||||||
self._profile_room_id = data["room_id"]
|
self._profile_room_id = data["room_id"]
|
||||||
|
|
||||||
@property
|
|
||||||
def profile_room_alias(self):
|
|
||||||
return f"#{self.mxid}"
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ class BaseEntity:
|
||||||
guid: str = ""
|
guid: str = ""
|
||||||
handle: str = ""
|
handle: str = ""
|
||||||
id: str = ""
|
id: str = ""
|
||||||
|
mxid: str = ""
|
||||||
signature: str = ""
|
signature: str = ""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
|
Ładowanie…
Reference in New Issue