Skip to content

Added Member Class Information retrival#112

Open
chrisdedman wants to merge 4 commits into
mainfrom
member-object
Open

Added Member Class Information retrival#112
chrisdedman wants to merge 4 commits into
mainfrom
member-object

Conversation

@chrisdedman

Copy link
Copy Markdown
Contributor

Description

Add a command to retrieve member information and a new Member class to manage member-related functionalities.

close #102

Type of Change

  • Feature
  • Refactor
  • Bug fix
  • Documentation
  • Other: ___

Pre-merge Checklist

  • Run tests: pytest
  • Run type check: mypy
  • Run formatting: black .

@chrisdedman chrisdedman added the feature A new feature label Jul 7, 2026
@chrisdedman chrisdedman self-assigned this Jul 7, 2026
@chrisdedman chrisdedman requested a review from PenguinBoi12 July 7, 2026 22:29

@PenguinBoi12 PenguinBoi12 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foundation of the PR is solid, it's going to be much easier to interact with users. The code in general is clean but there's a few issues with the tests and calls.

Some of those methods are doing request to matrix, like get_displayname, get_profile, etc, but there's not response validation, so if an error happens it's just going to explode. There's a helper in api called matrix_call that helps with that.

There's cases, like profile, where it might change the behaviour, if you still want to return a default value, although maybe it's better to raise (idk), you can try/catch it.

Additionally, your tests don't seem to have those possible failure covered.

Comment thread examples/client_info.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the file is a bit inaccurate, I think it should be something like member_info.py or just member.py

Comment thread matrix/member.py
def user_id(self) -> str:
return self._user_id

async def get_profile(self) -> dict | None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cleaner to have a dataclass for the MemberProfile instead of returning a plain dictionary; it would give callers type safety and make the fields explicit.

Comment thread matrix/member.py
presence = await self._client.get_presence(self._user_id)
return presence.presence if presence else None

async def has_permission(self, room: Room, permission: str) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with the following method, I think we could rename this has_room_permission

Comment thread matrix/member.py
avatar = await self._client.get_avatar(self._user_id)
return await self._client.mxc_to_http(avatar.avatar_url) if avatar else None

async def get_room_power_level(self, room: Room) -> int:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should move this closer (just above them) to has_permission and has_event_permission since they rely on this

Comment thread matrix/member.py
print(f"Can send messages: {can_send_message}")
```
"""
power_level = await self.get_room_power_level(room)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, does event really use room power level too?

Comment thread matrix/member.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While looking at this, I think one of the most interesting addition to member you could've made is mention method. Something like:

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})"

def __str__(self) -> str:
    return self.mention()

You can see that I also implemented __str__, I think it's a good idea to make it fallback to mention so e can do:

await ctx.reply(f"Welcome {member}!")

But this could be another PR.

Comment thread matrix/member.py
)

content = getattr(power_levels_event, "content", {}) or {}
required_level = content.get(permission, 0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fallback would cause the power level to always be true.

For example, if the response is an error, it will fallback to 0. This could give access to someone when it shouldn't and it's going to be hard to debug because it's doing it silently. In the worst cases, a permission check should never default to "allowed".

If you use matrix_call it will raise and at the same time default to a "not allowed" behaviour.

Also, there was no test covering that and same thing for has_event_permission

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Member Object

2 participants