"""Response building utilities for the community bot.""" from typing import List, Dict, Any, Optional from mautrix.types import MessageEvent class ResponseBuilder: """Builder for consistent response formatting.""" @staticmethod def build_html_response(title: str, content: str, allow_html: bool = True) -> str: """Build an HTML formatted response. Args: title: Response title content: Response content allow_html: Whether to allow HTML formatting Returns: str: Formatted response """ if allow_html: return f"

{title}
{content}

" else: return f"{title}\n{content}" @staticmethod def build_error_response(error: str, allow_html: bool = True) -> str: """Build an error response. Args: error: Error message allow_html: Whether to allow HTML formatting Returns: str: Formatted error response """ if allow_html: return f"

Error: {error}

" else: return f"Error: {error}" @staticmethod def build_success_response(message: str, allow_html: bool = True) -> str: """Build a success response. Args: message: Success message allow_html: Whether to allow HTML formatting Returns: str: Formatted success response """ if allow_html: return f"

Success: {message}

" else: return f"Success: {message}" @staticmethod def build_list_response(title: str, items: List[str], allow_html: bool = True) -> str: """Build a list response. Args: title: List title items: List items allow_html: Whether to allow HTML formatting Returns: str: Formatted list response """ if not items: return ResponseBuilder.build_html_response(title, "No items found.", allow_html) if allow_html: items_html = "
".join(items) return f"

{title}
{items_html}

" else: items_text = "\n".join(f"- {item}" for item in items) return f"{title}\n{items_text}" @staticmethod def build_room_link(alias: str, server: str) -> str: """Build a Matrix room link. Args: alias: Room alias server: Server name Returns: str: HTML room link """ return f"#{alias}:{server}" @staticmethod def build_user_link(user_id: str) -> str: """Build a Matrix user link. Args: user_id: User ID Returns: str: HTML user link """ return f"{user_id}" @staticmethod def build_activity_report_response(report: Dict[str, List[str]], config: Dict[str, Any]) -> str: """Build an activity report response. Args: report: Activity report data config: Bot configuration Returns: str: Formatted activity report """ warn_threshold = config.get("warn_threshold_days", 30) kick_threshold = config.get("kick_threshold_days", 60) response_parts = [] if report.get("warn_inactive"): warn_list = "
".join(report["warn_inactive"]) response_parts.append( f"

Users inactive for between {warn_threshold} and {kick_threshold} days:
" f"{warn_list}

" ) if report.get("kick_inactive"): kick_list = "
".join(report["kick_inactive"]) response_parts.append( f"

Users inactive for at least {kick_threshold} days:
" f"{kick_list}

" ) if report.get("ignored"): ignored_list = "
".join(report["ignored"]) response_parts.append( f"

Ignored users:
{ignored_list}

" ) return "".join(response_parts) @staticmethod def build_ban_results_response(results: Dict[str, Any]) -> str: """Build a ban results response. Args: results: Ban results data Returns: str: Formatted ban results """ ban_list = results.get("ban_list", []) error_list = results.get("error_list", []) response_parts = [] if ban_list: ban_list_html = "
".join(ban_list) response_parts.append(f"

Users banned:
{ban_list_html}

") if error_list: error_list_html = "
".join(error_list) response_parts.append(f"

Errors:
{error_list_html}

") if not response_parts: response_parts.append("

No users were banned.

") return "".join(response_parts) @staticmethod def build_sync_results_response(results: Dict[str, List[str]]) -> str: """Build a sync results response. Args: results: Sync results data Returns: str: Formatted sync results """ added = results.get("added", []) dropped = results.get("dropped", []) response_parts = [] if added: added_html = "
".join(added) response_parts.append(f"

Added:
{added_html}

") if dropped: dropped_html = "
".join(dropped) response_parts.append(f"

Dropped:
{dropped_html}

") if not response_parts: response_parts.append("

No changes made.

") return "".join(response_parts) @staticmethod def build_doctor_report_response(report: Dict[str, Any]) -> str: """Build a doctor report response. Args: report: Doctor report data Returns: str: Formatted doctor report """ response_parts = [] # Space information if report.get("space"): space = report["space"] space_info = f"Space: {space.get('room_id', 'Unknown')}
" space_info += f"Bot Power Level: {space.get('bot_power_level', 'Unknown')}
" space_info += f"Has Admin: {space.get('has_admin', False)}
" response_parts.append(f"

{space_info}

") # Room information if report.get("rooms"): rooms_info = "Rooms:
" for room_id, room_data in report["rooms"].items(): rooms_info += f"- {room_id}: {room_data.get('status', 'Unknown')}
" response_parts.append(f"

{rooms_info}

") # Issues if report.get("issues"): issues_html = "
".join(report["issues"]) response_parts.append(f"

Issues:
{issues_html}

") # Warnings if report.get("warnings"): warnings_html = "
".join(report["warnings"]) response_parts.append(f"

Warnings:
{warnings_html}

") if not response_parts: response_parts.append("

No issues found.

") return "".join(response_parts)