Files
Advanced-Community-Bot/community/helpers/decorators.py
T
Dome edd3eee178 feat: native matrix URI pills for {user}/{room} + major rendering & codebase refactor
This change introduces native `matrix:` URI-based rendering for `{user}` and `{room}` placeholders,
replacing previous plaintext and matrix.to-based links. Users and rooms are now rendered as clickable
pills in supporting clients, with a clean display using display names and room names (no @/# prefixes).

Reporting, moderation, and auto-redaction messages have been updated to use the same rendering logic.
Inspect and event links now also use native `matrix:` URIs for direct in-client navigation.

Internally, URI generation and rendering logic have been unified via central helper functions,
ensuring consistent handling of user IDs, room IDs, aliases, and event IDs.

This commit also includes a broader refactor of the codebase:
- decomposed complex flows (e.g. join handling) into smaller helpers
- moved mutable class-level state to instance-level
- reduced duplicate API calls and redundant logic
- improved overall structure and maintainability

Test coverage has been extended for URI helpers and rendering logic to prevent regressions.

No breaking changes to existing template parameters like `{user_link}` or `{room_link}`.
2026-04-11 20:21:33 +02:00

60 lines
1.9 KiB
Python

"""Decorators for common bot operations."""
import functools
from typing import Callable, Any, Optional
from mautrix.types import UserID, MessageEvent
def require_permission(min_level: int = 50, room_id: Optional[str] = None):
"""Decorator to require user permission for command execution.
Args:
min_level: Minimum required power level (default 50 for moderator)
room_id: Room ID to check permissions in (None for parent room)
"""
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
async def wrapper(self, evt: MessageEvent, *args, **kwargs) -> Any:
if not await self.user_permitted(evt.sender, min_level, room_id):
await evt.reply("You don't have permission to use this command")
return
return await func(self, evt, *args, **kwargs)
return wrapper
return decorator
def require_parent_room(func: Callable) -> Callable:
"""Decorator to require parent room to be configured."""
@functools.wraps(func)
async def wrapper(self, evt: MessageEvent, *args, **kwargs) -> Any:
if not await self.check_parent_room(evt):
return
return await func(self, evt, *args, **kwargs)
return wrapper
def handle_errors(error_message: str = "An error occurred"):
"""Decorator to handle common errors in command execution.
Args:
error_message: Default error message to show to user
"""
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
async def wrapper(self, evt: MessageEvent, *args, **kwargs) -> Any:
try:
return await func(self, evt, *args, **kwargs)
except Exception as e:
self.log.error(f"Error in {func.__name__}: {e}")
await evt.reply(f"{error_message}: {e}")
return wrapper
return decorator