-
Notifications
You must be signed in to change notification settings - Fork 6
Added Member Class Information retrival #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chrisdedman
wants to merge
13
commits into
main
Choose a base branch
from
member-object
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+561
−3
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c36a0db
add profile command example to retrieve member information
chrisdedman ac232c8
Add Member class to manage member-related functionalities
chrisdedman 98e0133
Add unit tests for Member class method
chrisdedman 0ae0df9
Merge branch 'main' into member-object
chrisdedman 51c6133
Merge branch 'main' into member-object
chrisdedman a7ef645
renamed example file
chrisdedman b54ba1b
Add get_state_event method to Room class for fetching room state events
chrisdedman b09df78
Refactor Member class to use matrix_call for API calls
chrisdedman 36404bf
Refactor get_profile method to return MemberProfile and add MemberPro…
chrisdedman 97897ad
Refactor Member class methods to use matrix_call for error handling a…
chrisdedman 19ecbe1
Refactor get_profile method to always return MemberProfile and update…
chrisdedman 95f187d
Add MemberPresence dataclass and update get_presence method to return…
chrisdedman 6d15a35
Refactor get_display_name and get_avatar_url methods for improved res…
chrisdedman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from matrix import Bot | ||
|
|
||
| bot = Bot() | ||
|
|
||
|
|
||
| @bot.command() | ||
| async def profile(ctx): | ||
| member = ctx.member | ||
|
|
||
| name = await member.get_display_name() | ||
| avatar = await member.get_avatar_url() | ||
| level = await member.get_room_power_level(ctx.room) | ||
| presence = await member.get_presence() | ||
| has_permission = await member.has_room_permission(ctx.room, "ban") | ||
| has_event_permission = await member.has_event_permission( | ||
| ctx.room, "m.room.history_visibility" | ||
| ) | ||
|
|
||
| await ctx.reply( | ||
| f"Welcome {member.mention()}!\n" | ||
| f"Display name: {name}\n" | ||
| f"Avatar URL: {avatar}\n" | ||
| f"Power level: {level}\n" | ||
| f"Presence: {presence.presence}\n" | ||
| f"last_active_ago: {presence.last_active_ago}\n" | ||
| f"currently_active: {presence.currently_active}\n" | ||
| f"status_msg: {presence.status_msg}\n" | ||
| f"Has permission to ban users: {has_permission}\n" | ||
| f"Has permission to see history: {has_event_permission}" | ||
| ) | ||
|
|
||
|
|
||
| bot.start(config="config.yaml") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
chrisdedman marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| from matrix.room import Room | ||
| from nio import AsyncClient | ||
| from matrix.api import matrix_call | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
|
|
||
| @dataclass | ||
| class MemberProfile: | ||
| displayname: str | None | ||
| avatar_url: str | None | ||
| other_info: dict[Any, Any] | ||
|
|
||
|
|
||
| @dataclass | ||
| class MemberPresence: | ||
| user_id: str | ||
| presence: str | ||
| last_active_ago: int | None | ||
| currently_active: bool | None | ||
| status_msg: str | None | ||
|
|
||
|
|
||
| class Member: | ||
| def __init__(self, user_id: str, client: AsyncClient) -> None: | ||
| self._user_id: str = user_id | ||
| self._client: AsyncClient = client | ||
|
|
||
| def __str__(self) -> str: | ||
| return self.mention() | ||
|
|
||
| @property | ||
| def user_id(self) -> str: | ||
| return self._user_id | ||
|
|
||
| def mention(self) -> str: | ||
| """Get a Markdown-formatted mention link (matrix.to pill) for this member. | ||
|
|
||
| ## Example | ||
|
|
||
| ```python | ||
| await ctx.reply(f"Welcome {member.mention()}!") | ||
| ``` | ||
| """ | ||
| return f"[{self._user_id}](https://matrix.to/#/{self._user_id})" | ||
|
|
||
| async def get_profile(self) -> MemberProfile: | ||
| """Get the profile information for this member. | ||
|
|
||
| ## Example | ||
|
|
||
| ```python | ||
| profile = await member.get_profile() | ||
| for key, value in profile.other_info.items(): | ||
| print(f"{key}: {value}") | ||
| ``` | ||
| """ | ||
| profile = await matrix_call( | ||
| self._client.get_profile(self._user_id), | ||
| error_message=f"Failed to get profile for user {self._user_id}", | ||
| ) | ||
|
|
||
| return MemberProfile( | ||
| displayname=profile.displayname, | ||
| avatar_url=profile.avatar_url, | ||
| other_info=profile.other_info, | ||
| ) | ||
|
|
||
| async def get_display_name(self) -> str | None: | ||
| """Get the display name for this member. | ||
|
|
||
| ## Example | ||
|
|
||
| ```python | ||
| display_name = await member.get_display_name() | ||
| print(f"Display name: {display_name}") | ||
| ``` | ||
| """ | ||
| response = await matrix_call( | ||
| self._client.get_displayname(self._user_id), | ||
| error_message=f"Failed to get display name for user {self._user_id}", | ||
| ) | ||
|
|
||
| displayname: str | None = response.displayname | ||
| return displayname | ||
|
|
||
| async def get_avatar_url(self) -> str | None: | ||
| """Get the avatar URL for this member. | ||
|
|
||
| ## Example | ||
|
|
||
| ```python | ||
| avatar_url = await member.get_avatar_url() | ||
| print(f"Avatar URL: {avatar_url}") | ||
| ``` | ||
| """ | ||
| response = await matrix_call( | ||
| self._client.get_avatar(self._user_id), | ||
| error_message=f"Failed to get avatar for user {self._user_id}", | ||
| ) | ||
| if not response.avatar_url: | ||
| return None | ||
|
|
||
| avatar_url: str | None = await self._client.mxc_to_http(response.avatar_url) | ||
| return avatar_url | ||
|
|
||
| async def get_presence(self) -> MemberPresence: | ||
| """Get the presence status for this member. | ||
|
|
||
| ## Example | ||
|
|
||
| ```python | ||
| presence = await member.get_presence() | ||
| print(f"State: {presence.presence}") | ||
| print(f"Currently active: {presence.currently_active}") | ||
| print(f"Last active: {presence.last_active_ago} ms ago") | ||
| print(f"Status: {presence.status_msg}") | ||
| ``` | ||
| """ | ||
| presence = await matrix_call( | ||
| self._client.get_presence(self._user_id), | ||
| error_message=f"Failed to get presence for user {self._user_id}", | ||
| ) | ||
|
|
||
| return MemberPresence( | ||
| user_id=presence.user_id, | ||
| presence=presence.presence, | ||
| last_active_ago=presence.last_active_ago, | ||
| currently_active=presence.currently_active, | ||
| status_msg=presence.status_msg, | ||
| ) | ||
|
|
||
| async def get_room_power_level(self, room: Room) -> int: | ||
| """Get the power level for this member in a specific room. | ||
|
|
||
| ## Example | ||
|
|
||
| ```python | ||
| level = await member.get_room_power_level(ctx.room) | ||
| print(f"Power level in room {room.room_id}: {level}") | ||
| ``` | ||
| """ | ||
| power_level = await room.get_state_event("m.room.power_levels", "") | ||
|
|
||
| content = power_level.content | ||
| users = content.get("users", {}) | ||
| default = content.get("users_default", 0) | ||
|
|
||
| return int(users.get(self._user_id, default)) | ||
|
|
||
| async def has_room_permission(self, room: Room, permission: str) -> bool: | ||
|
chrisdedman marked this conversation as resolved.
|
||
| """Check if this member has a specific permission in a room. | ||
|
|
||
| ## Example | ||
|
|
||
| ```python | ||
| has_permission = await member.has_room_permission(ctx.room, "ban") | ||
| print(f"Has ban permission: {has_permission}") | ||
| ``` | ||
| """ | ||
| power_levels_event = await room.get_state_event("m.room.power_levels", "") | ||
|
|
||
| content = power_levels_event.content | ||
| if permission not in content: | ||
| return False # Permission not defined in the room's power levels | ||
|
|
||
| users = content.get("users", {}) | ||
| default = content.get("users_default", 0) | ||
| power_level = int(users.get(self._user_id, default)) | ||
| required_level = content.get(permission) | ||
|
|
||
| return bool(power_level >= required_level) | ||
|
|
||
| async def has_event_permission(self, room: Room, event_type: str) -> bool: | ||
|
chrisdedman marked this conversation as resolved.
|
||
| """Check if this member has permission to send a specific event type in a room. | ||
|
|
||
| ## Example | ||
|
|
||
| ```python | ||
| can_send_message = await member.has_event_permission(ctx.room, "m.room.message") | ||
| print(f"Can send messages: {can_send_message}") | ||
| ``` | ||
| """ | ||
| power_level = await self.get_room_power_level(room) | ||
|
chrisdedman marked this conversation as resolved.
|
||
| power_levels_event = await room.get_state_event("m.room.power_levels", "") | ||
|
|
||
| content = power_levels_event.content | ||
| events = content.get("events", {}) | ||
|
|
||
| if event_type not in events: | ||
| return False | ||
|
|
||
| return bool(power_level >= events[event_type]) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.