feat: add room_members method to list members

This commit is contained in:
əlemi 2024-03-14 21:34:28 +01:00
parent 6f7ddbbc9e
commit 387d5a25bc
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 26 additions and 1 deletions

View file

@ -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",

View file

@ -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"