mapping.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. """
  3. Satellite-Email Mapper.
  4. Ordnet Satellites den konfigurierten E-Mail-Konten zu.
  5. Unterstuetzt Matching per Alias und Room-ID — analog zum Calendar-Mapper.
  6. """
  7. from trixy_core.utils.debug import pdebug
  8. class SatelliteEmailMapper:
  9. """
  10. Verwaltet die Zuordnung zwischen Satellites und E-Mail-Konten.
  11. Matching-Logik:
  12. 1. Satellite-Alias oder Room-ID gegen konfigurierte Eintraege pruefen
  13. 2. Globale Accounts werden immer hinzugefuegt
  14. 3. Standalone-Modus: Fallback auf ersten Eintrag
  15. """
  16. def __init__(self, config: dict, application: object) -> None:
  17. self._config = config
  18. self._application = application
  19. self._accounts_config = config.get("accounts", {})
  20. self._mapping = config.get("satellite_mapping", {})
  21. self._global_accounts = self._mapping.get("global_accounts", [])
  22. self._entries = self._mapping.get("satellites", [])
  23. def get_accounts_for_satellite(self, satellite_id: str) -> list[str]:
  24. """
  25. Gibt alle Account-Keys fuer einen Satellite zurueck.
  26. Returns:
  27. Liste von Account-Schluesseln (z.B. ["outlook_patrick"])
  28. """
  29. entry = self._find_entry_for_satellite(satellite_id)
  30. account_keys: list[str] = []
  31. # Persoenliche Accounts
  32. if entry:
  33. account_keys.extend(entry.get("accounts", []))
  34. # Globale Accounts
  35. for key in self._global_accounts:
  36. if key not in account_keys:
  37. account_keys.append(key)
  38. # Nur existierende Accounts zurueckgeben
  39. return [k for k in account_keys if k in self._accounts_config]
  40. def get_account_config(self, account_key: str) -> dict:
  41. """Gibt die Konfiguration eines Accounts zurueck."""
  42. return self._accounts_config.get(account_key, {})
  43. def get_satellites_for_account(self, account_key: str) -> list[str]:
  44. """Gibt alle Satellite-IDs zurueck, die einen Account sehen."""
  45. satellite_ids: list[str] = []
  46. if account_key in self._global_accounts:
  47. return self._get_all_connected_satellite_ids()
  48. for entry in self._entries:
  49. if account_key in entry.get("accounts", []):
  50. satellite_ids.extend(self._get_satellite_ids_for_entry(entry))
  51. return satellite_ids
  52. def get_all_account_keys(self) -> list[str]:
  53. """Gibt alle konfigurierten Account-Keys zurueck."""
  54. return list(self._accounts_config.keys())
  55. def _find_entry_for_satellite(self, satellite_id: str) -> dict | None:
  56. """Findet den Mapping-Eintrag fuer einen Satellite."""
  57. satellites = getattr(self._application, "satellites", None)
  58. if not satellites:
  59. # Standalone-Modus: Ersten Eintrag als Fallback
  60. if self._entries:
  61. return self._entries[0]
  62. return None
  63. satellite = satellites.get(satellite_id)
  64. if not satellite:
  65. return None
  66. sat_alias = getattr(satellite, "alias", "").lower()
  67. sat_room = getattr(satellite, "room_id", "").lower()
  68. for entry in self._entries:
  69. alias_list = [a.strip().lower() for a in entry.get("alias", "").split(",")]
  70. if sat_alias and sat_alias in alias_list:
  71. return entry
  72. room_list = [r.strip().lower() for r in entry.get("room", "").split(",")]
  73. if sat_room and sat_room in room_list:
  74. return entry
  75. return None
  76. def _get_satellite_ids_for_entry(self, entry: dict) -> list[str]:
  77. """Gibt Satellite-IDs fuer einen Mapping-Eintrag zurueck."""
  78. satellites = getattr(self._application, "satellites", None)
  79. if not satellites:
  80. return []
  81. ids: list[str] = []
  82. alias_list = [a.strip().lower() for a in entry.get("alias", "").split(",")]
  83. room_list = [r.strip().lower() for r in entry.get("room", "").split(",")]
  84. for sat in satellites:
  85. sat_alias = getattr(sat, "alias", "").lower()
  86. sat_room = getattr(sat, "room_id", "").lower()
  87. if (sat_alias and sat_alias in alias_list) or (sat_room and sat_room in room_list):
  88. ids.append(sat.id)
  89. return ids
  90. def _get_all_connected_satellite_ids(self) -> list[str]:
  91. """Gibt alle verbundenen Satellite-IDs zurueck."""
  92. satellites = getattr(self._application, "satellites", None)
  93. if not satellites:
  94. return []
  95. return [sat.id for sat in satellites if sat.is_connected]