util.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # mautrix-signal - A Matrix-Signal puppeting bridge
  2. # Copyright (C) 2021 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from __future__ import annotations
  17. from mautrix.appservice import IntentAPI
  18. from mautrix.types import ContentURI, EventType, JoinRule, PowerLevelStateEventContent, RoomID
  19. async def get_initial_state(
  20. intent: IntentAPI, room_id: RoomID
  21. ) -> tuple[
  22. str | None,
  23. str | None,
  24. PowerLevelStateEventContent | None,
  25. bool,
  26. ContentURI | None,
  27. JoinRule | None,
  28. ]:
  29. state = await intent.get_state(room_id)
  30. title: str | None = None
  31. about: str | None = None
  32. levels: PowerLevelStateEventContent | None = None
  33. encrypted: bool = False
  34. avatar_url: ContentURI | None = None
  35. join_rule: JoinRule | None = None
  36. for event in state:
  37. try:
  38. if event.type == EventType.ROOM_NAME:
  39. title = event.content.name
  40. elif event.type == EventType.ROOM_TOPIC:
  41. about = event.content.topic
  42. elif event.type == EventType.ROOM_POWER_LEVELS:
  43. levels = event.content
  44. elif event.type == EventType.ROOM_CANONICAL_ALIAS:
  45. title = title or event.content.canonical_alias
  46. elif event.type == EventType.ROOM_ENCRYPTION:
  47. encrypted = True
  48. elif event.type == EventType.ROOM_AVATAR:
  49. avatar_url = event.content.url
  50. elif event.type == EventType.ROOM_JOIN_RULES:
  51. join_rule = event.content.join_rule
  52. except KeyError:
  53. # Some state event probably has empty content
  54. pass
  55. return title, about, levels, encrypted, avatar_url, join_rule