matrix.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2022 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 typing import TYPE_CHECKING
  18. from mautrix.bridge import BaseMatrixHandler
  19. from mautrix.types import (
  20. Event,
  21. EventID,
  22. EventType,
  23. PresenceEvent,
  24. ReactionEvent,
  25. ReactionEventContent,
  26. ReceiptEvent,
  27. RedactionEvent,
  28. RelationType,
  29. RoomID,
  30. SingleReceiptEventContent,
  31. TypingEvent,
  32. UserID,
  33. )
  34. from mautrix.util.message_send_checkpoint import MessageSendCheckpointStatus
  35. from . import portal as po, user as u
  36. from .db import Message as DBMessage
  37. if TYPE_CHECKING:
  38. from .__main__ import InstagramBridge
  39. class MatrixHandler(BaseMatrixHandler):
  40. def __init__(self, bridge: "InstagramBridge") -> None:
  41. prefix, suffix = bridge.config["bridge.username_template"].format(userid=":").split(":")
  42. homeserver = bridge.config["homeserver.domain"]
  43. self.user_id_prefix = f"@{prefix}"
  44. self.user_id_suffix = f"{suffix}:{homeserver}"
  45. super().__init__(bridge=bridge)
  46. async def send_welcome_message(self, room_id: RoomID, inviter: u.User) -> None:
  47. await super().send_welcome_message(room_id, inviter)
  48. if not inviter.notice_room:
  49. inviter.notice_room = room_id
  50. await inviter.update()
  51. await self.az.intent.send_notice(
  52. room_id, "This room has been marked as your Instagram bridge notice room."
  53. )
  54. async def handle_leave(self, room_id: RoomID, user_id: UserID, event_id: EventID) -> None:
  55. portal = await po.Portal.get_by_mxid(room_id)
  56. if not portal:
  57. return
  58. user = await u.User.get_by_mxid(user_id, create=False)
  59. if not user:
  60. return
  61. await portal.handle_matrix_leave(user)
  62. @staticmethod
  63. async def handle_redaction(
  64. room_id: RoomID, user_id: UserID, event_id: EventID, redaction_event_id: EventID
  65. ) -> None:
  66. user = await u.User.get_by_mxid(user_id)
  67. if not user:
  68. return
  69. portal = await po.Portal.get_by_mxid(room_id)
  70. if not portal:
  71. user.send_remote_checkpoint(
  72. MessageSendCheckpointStatus.PERM_FAILURE,
  73. event_id,
  74. room_id,
  75. EventType.ROOM_REDACTION,
  76. error=Exception("Ignoring redaction event in non-portal room"),
  77. )
  78. return
  79. await portal.handle_matrix_redaction(user, event_id, redaction_event_id)
  80. @classmethod
  81. async def handle_reaction(
  82. cls, room_id: RoomID, user_id: UserID, event_id: EventID, content: ReactionEventContent
  83. ) -> None:
  84. if content.relates_to.rel_type != RelationType.ANNOTATION:
  85. cls.log.debug(
  86. f"Ignoring m.reaction event in {room_id} from {user_id} with unexpected "
  87. f"relation type {content.relates_to.rel_type}"
  88. )
  89. return
  90. user = await u.User.get_by_mxid(user_id)
  91. if not user:
  92. return
  93. portal = await po.Portal.get_by_mxid(room_id)
  94. if not portal:
  95. return
  96. await portal.handle_matrix_reaction(
  97. user, event_id, content.relates_to.event_id, content.relates_to.key
  98. )
  99. async def handle_read_receipt(
  100. self,
  101. user: u.User,
  102. portal: po.Portal,
  103. event_id: EventID,
  104. data: SingleReceiptEventContent,
  105. ) -> None:
  106. message = await DBMessage.get_by_mxid(event_id, portal.mxid)
  107. if not message or message.is_internal:
  108. return
  109. user.log.debug(f"Marking {message.item_id} in {portal.thread_id} as read")
  110. await user.mqtt.mark_seen(portal.thread_id, message.item_id)
  111. @staticmethod
  112. async def handle_typing(room_id: RoomID, typing: list[UserID]) -> None:
  113. portal = await po.Portal.get_by_mxid(room_id)
  114. if not portal:
  115. return
  116. await portal.handle_matrix_typing(set(typing))
  117. async def handle_event(self, evt: Event) -> None:
  118. if evt.type == EventType.ROOM_REDACTION:
  119. evt: RedactionEvent
  120. await self.handle_redaction(evt.room_id, evt.sender, evt.redacts, evt.event_id)
  121. elif evt.type == EventType.REACTION:
  122. evt: ReactionEvent
  123. await self.handle_reaction(evt.room_id, evt.sender, evt.event_id, evt.content)
  124. async def handle_ephemeral_event(
  125. self, evt: ReceiptEvent | PresenceEvent | TypingEvent
  126. ) -> None:
  127. if evt.type == EventType.TYPING:
  128. await self.handle_typing(evt.room_id, evt.content.user_ids)
  129. else:
  130. await super().handle_ephemeral_event(evt)