diff --git a/src/aioappsrv/app.py b/src/aioappsrv/app.py index de8592b..9208f24 100644 --- a/src/aioappsrv/app.py +++ b/src/aioappsrv/app.py @@ -134,7 +134,7 @@ class AppService: return None res.raise_for_status() doc = await res.json() - return User(**doc) + return User.parse(doc) async def register_mxid(self, mxid: str) -> None: bare_mxid = fmt_mxid(mxid, full=False) @@ -213,6 +213,24 @@ class AppService: res.raise_for_status() self.logger.debug("joined room %s with %s : %s", room, mxid, await res.json()) + async def room_members(self, room: str, mxid: str | None = None) -> dict[str, User]: + out : dict[str, User] = dict() + async with self._client.request( + method="GET", + url=f"{self.client_api}/rooms/{room}/joined_members", + headers=self.api_headers, + params={"user_id": mxid} if mxid else {} + ) as res: + res.raise_for_status() + self.logger.debug("listed room %s users with %s", room, mxid) + doc = await res.json() + for mxid in doc["joined"]: + if not mxid: # wtf mypy?? + self.logger.warn("empty mxid: %s", mxid) + continue + out[mxid] = User.parse(doc["joined"][mxid]) + return out + async def send_message(self, room: str, text: str, mxid: str | None = None) -> str: async with self._client.request( method="PUT", diff --git a/src/aioappsrv/matrix.py b/src/aioappsrv/matrix.py index efbf14b..673057a 100644 --- a/src/aioappsrv/matrix.py +++ b/src/aioappsrv/matrix.py @@ -7,6 +7,13 @@ class User: avatar_url: str = "" displayname: str = "" + @classmethod + def parse(cls, obj: dict[str, Any]) -> 'User': + return cls( + avatar_url = obj.get("avatar_url") or "", + displayname = obj.get("displayname") or obj.get("display_name") or "", + ) + class EventType(StrEnum): MESSAGE = "m.room.message" MEMBER = "m.room.member"