Ensure missing profiles are created on Matrix side

If we try to send a Post to the Matrix appservice, missing profiles need creating.

Closes #127
merge-requests/165/head
Jason Robinson 2021-05-02 00:41:39 +03:00
rodzic d5938186ac
commit fbaee66da8
2 zmienionych plików z 36 dodań i 4 usunięć

Wyświetl plik

@ -11,7 +11,7 @@ import requests
from federation.entities.base import Post, Profile
from federation.entities.matrix.enums import EventType
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.network import fetch_document, fetch_file
@ -20,6 +20,7 @@ logger = logging.getLogger("federation")
class MatrixEntityMixin(BaseEntity):
_event_type: str = None
_payloads: List[Dict] = []
_profile_room_id = None
_txn_id: str = None
@ -59,11 +60,10 @@ class MatrixEntityMixin(BaseEntity):
if status == 200:
data = json.loads(doc)
self._profile_room_id = data["room_id"]
# TODO create?
# noinspection PyMethodMayBeStatic
def payloads(self) -> List[Dict]:
return []
return self._payloads
@property
def profile_room_alias(self):
@ -84,6 +84,7 @@ class MatrixRoomMessage(Post, MatrixEntityMixin):
_thread_room_id: str = None
def create_thread_room(self):
# TODO don't do this immediately here, append to payloads for later pushing out
headers = appservice_auth_header()
# Create the thread room
response = requests.post(
@ -113,6 +114,17 @@ class MatrixRoomMessage(Post, MatrixEntityMixin):
response.raise_for_status()
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]:
payloads = super().payloads()
payloads.append({

Wyświetl plik

@ -1,5 +1,8 @@
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):
@ -36,3 +39,20 @@ def get_name_for_profile(fid: str) -> Optional[str]:
return profile.name
except Exception:
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