ForcePilot/backend/package/yuxi/channels/adapters/bluebubbles/accounts.py

57 lines
1.6 KiB
Python
Raw Normal View History

from __future__ import annotations
from typing import Any
from pydantic import BaseModel
class BlueBubblesAccountConfig(BaseModel):
account_id: str = "default"
name: str = ""
server_url: str = "http://localhost:1234"
password: str = ""
enabled: bool = True
dm_policy: str = "pairing"
group_policy: str = "allowlist"
dm_allow_from: list[str] = []
group_allow_chats: list[str] = []
require_mention: bool = False
tapback_enabled: bool = True
default_account: bool = False
@property
def display_name(self) -> str:
return self.name or self.account_id
def parse_accounts_config(raw: dict[str, Any]) -> dict[str, BlueBubblesAccountConfig]:
accounts: dict[str, BlueBubblesAccountConfig] = {}
for key, value in raw.items():
if not isinstance(value, dict):
continue
try:
accounts[key] = BlueBubblesAccountConfig(**value)
except Exception:
continue
return accounts
def get_default_account(accounts: dict[str, BlueBubblesAccountConfig]) -> BlueBubblesAccountConfig | None:
for account in accounts.values():
if account.default_account and account.enabled:
return account
for account in accounts.values():
if account.enabled:
return account
return None
def normalize_account_id(raw_id: str) -> str:
return raw_id.strip().lower().replace(" ", "_")
def get_account_display_name(account_id: str, accounts: dict[str, BlueBubblesAccountConfig]) -> str:
if account_id in accounts:
return accounts[account_id].display_name
return account_id