kopia lustrzana https://gitlab.com/jaywink/federation
Ensure missing profiles are created on Matrix side
If we try to send a Post to the Matrix appservice, missing profiles need creating. Closes #127merge-requests/165/head
rodzic
d5938186ac
commit
fbaee66da8
|
@ -11,7 +11,7 @@ import requests
|
||||||
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
|
||||||
from federation.entities.mixins import BaseEntity
|
from federation.entities.mixins import BaseEntity
|
||||||
from federation.entities.utils import get_base_attributes
|
from federation.entities.utils import get_base_attributes, get_profile
|
||||||
from federation.utils.matrix import get_matrix_configuration, appservice_auth_header
|
from federation.utils.matrix import get_matrix_configuration, appservice_auth_header
|
||||||
from federation.utils.network import fetch_document, fetch_file
|
from federation.utils.network import fetch_document, fetch_file
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ logger = logging.getLogger("federation")
|
||||||
|
|
||||||
class MatrixEntityMixin(BaseEntity):
|
class MatrixEntityMixin(BaseEntity):
|
||||||
_event_type: str = None
|
_event_type: str = None
|
||||||
|
_payloads: List[Dict] = []
|
||||||
_profile_room_id = None
|
_profile_room_id = None
|
||||||
_txn_id: str = None
|
_txn_id: str = None
|
||||||
|
|
||||||
|
@ -59,11 +60,10 @@ class MatrixEntityMixin(BaseEntity):
|
||||||
if status == 200:
|
if status == 200:
|
||||||
data = json.loads(doc)
|
data = json.loads(doc)
|
||||||
self._profile_room_id = data["room_id"]
|
self._profile_room_id = data["room_id"]
|
||||||
# TODO create?
|
|
||||||
|
|
||||||
# noinspection PyMethodMayBeStatic
|
# noinspection PyMethodMayBeStatic
|
||||||
def payloads(self) -> List[Dict]:
|
def payloads(self) -> List[Dict]:
|
||||||
return []
|
return self._payloads
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def profile_room_alias(self):
|
def profile_room_alias(self):
|
||||||
|
@ -84,6 +84,7 @@ class MatrixRoomMessage(Post, MatrixEntityMixin):
|
||||||
_thread_room_id: str = None
|
_thread_room_id: str = None
|
||||||
|
|
||||||
def create_thread_room(self):
|
def create_thread_room(self):
|
||||||
|
# TODO don't do this immediately here, append to payloads for later pushing out
|
||||||
headers = appservice_auth_header()
|
headers = appservice_auth_header()
|
||||||
# Create the thread room
|
# Create the thread room
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
|
@ -113,6 +114,17 @@ class MatrixRoomMessage(Post, MatrixEntityMixin):
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
self._thread_room_event_id = response.json()["event_id"]
|
self._thread_room_event_id = response.json()["event_id"]
|
||||||
|
|
||||||
|
def get_profile_room_id(self):
|
||||||
|
super().get_profile_room_id()
|
||||||
|
if not self._profile_room_id:
|
||||||
|
from federation.entities.matrix.mappers import get_outbound_entity
|
||||||
|
# Need to also create the profile
|
||||||
|
profile = get_profile(self.actor_id)
|
||||||
|
profile_entity = get_outbound_entity(profile, None)
|
||||||
|
payloads = profile_entity.payloads()
|
||||||
|
if payloads:
|
||||||
|
self._payloads.extend(payloads)
|
||||||
|
|
||||||
def payloads(self) -> List[Dict]:
|
def payloads(self) -> List[Dict]:
|
||||||
payloads = super().payloads()
|
payloads = super().payloads()
|
||||||
payloads.append({
|
payloads.append({
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
import inspect
|
import inspect
|
||||||
from typing import Optional
|
from typing import Optional, TYPE_CHECKING
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from federation.entities.base import Profile
|
||||||
|
|
||||||
|
|
||||||
def get_base_attributes(entity):
|
def get_base_attributes(entity):
|
||||||
|
@ -36,3 +39,20 @@ def get_name_for_profile(fid: str) -> Optional[str]:
|
||||||
return profile.name
|
return profile.name
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def get_profile(fid):
|
||||||
|
# type: (str) -> Profile
|
||||||
|
"""
|
||||||
|
Get a profile via the configured profile getter.
|
||||||
|
|
||||||
|
Currently only works with Django configuration.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
from federation.utils.django import get_function_from_config
|
||||||
|
profile_func = get_function_from_config("get_profile_function")
|
||||||
|
if not profile_func:
|
||||||
|
return
|
||||||
|
return profile_func(fid=fid)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
Ładowanie…
Reference in New Issue