async def set_membership(self, room_id: RoomID, user_id: UserID, membership: Membership) -> None: try: room_members = self.members[room_id] except KeyError: self.members[room_id] = {user_id: Member(membership=membership)} return try: room_members[user_id].membership = membership except (KeyError, TypeError): room_id[user_id] = Member(membership=membership)
async def get_joined_members(self, room_id: RoomID) -> Dict[UserID, Member]: """ Get a user ID -> member info map for a room. The current user must be in the room for it to work, unless it is an Application Service in which case any of the AS's users must be in the room. This API is primarily for Application Services and should be faster to respond than `/members`_ as it can be implemented more efficiently on the server. See also: `/joined_members API reference`_ Args: room_id: The ID of the room to get the members of. Returns: A dictionary from user IDs to Member info objects. .. _/joined_members API reference: https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-rooms-roomid-joined-members .. _/members: https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-rooms-roomid-members """ content = await self.api.request(Method.GET, Path.rooms[room_id].joined_members) try: return { user_id: Member(membership=Membership.JOIN, displayname=member.get("display_name", ""), avatar_url=member.get("avatar_url", "")) for user_id, member in content["joined"].items() } except KeyError: raise MatrixResponseError("`joined` not in response.") except SerializerError as e: raise MatrixResponseError( "Invalid member objects in response") from e
async def get_member(self, room_id: RoomID, user_id: UserID) -> Optional[Member]: res = await self.db.fetchrow("SELECT membership, displayname, avatar_url " "FROM mx_user_profile WHERE room_id=$1 AND user_id=$2", room_id, user_id) if res is None: return None return Member(membership=Membership.deserialize(res["membership"]), displayname=res["displayname"], avatar_url=res["avatar_url"])
def set_membership(self, room_id: RoomID, user_id: UserID, membership: Membership) -> None: if not room_id: raise ValueError("room_id is empty") elif not user_id: raise ValueError("user_id is empty") elif not membership: raise ValueError("membership is empty") self.set_member(room_id, user_id, Member(membership=membership))
async def sync_matrix_members(self) -> None: resp = await self.main_intent.get_room_joined_memberships(self.mxid) members = resp["joined"] for mxid, info in members.items(): member = Member(membership=Membership.JOIN) if "display_name" in info: member.displayname = info["display_name"] if "avatar_url" in info: member.avatar_url = info["avatar_url"] self.az.state_store.set_member(self.mxid, mxid, member)
async def set_member( self, room_id: RoomID, user_id: UserID, member: Union[Member, MemberStateEventContent]) -> None: if not isinstance(member, Member): member = Member(membership=member.membership, avatar_url=member.avatar_url, displayname=member.displayname) try: self.members[room_id][user_id] = member except KeyError: self.members[room_id] = {user_id: member}
async def set_members( self, room_id: RoomID, members: Dict[UserID, Union[Member, MemberStateEventContent]]) -> None: self.members[room_id] = { user_id: (member if isinstance(member, Member) else Member( membership=member.membership, avatar_url=member.avatar_url, displayname=member.displayname)) for user_id, member in members.items() } self.full_member_list[room_id] = True
async def set_members(self, room_id: RoomID, members: Dict[UserID, Union[Member, MemberStateEventContent]], joined_only: bool = False) -> None: # TODO if joined_only is true, don't forget invited/left members self.members[room_id] = { user_id: (member if isinstance(member, Member) else Member( membership=member.membership, avatar_url=member.avatar_url, displayname=member.displayname)) for user_id, member in members.items() } self.full_member_list[room_id] = True
async def set_members( self, room_id: RoomID, members: dict[UserID, Member | MemberStateEventContent], only_membership: Membership | None = None, ) -> None: old_members = {} if only_membership is not None: old_members = { user_id: member for user_id, member in self.members.get(room_id, {}).items() if member.membership != only_membership } self.members[room_id] = { user_id: (member if isinstance(member, Member) else Member( membership=member.membership, avatar_url=member.avatar_url, displayname=member.displayname, )) for user_id, member in members.items() } self.members[room_id].update(old_members) self.full_member_list[room_id] = True
def member(self) -> Member: return Member(membership=self.membership, displayname=self.displayname, avatar_url=self.avatar_url)
async def set_membership(self, room_id: RoomID, user_id: UserID, membership: Membership) -> None: await self.set_member(room_id, user_id, Member(membership=membership))