message.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # mautrix-signal - A Matrix-Signal puppeting bridge
  2. # Copyright (C) 2020 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, ClassVar
  18. from attr import dataclass
  19. from mautrix.types import EventID, RoomID
  20. from mautrix.util.async_db import Database
  21. import asyncpg
  22. from mausignald.types import Address, GroupID
  23. from ..util import id_to_str
  24. fake_db = Database.create("") if TYPE_CHECKING else None
  25. @dataclass
  26. class Message:
  27. db: ClassVar[Database] = fake_db
  28. mxid: EventID
  29. mx_room: RoomID
  30. sender: Address
  31. timestamp: int
  32. signal_chat_id: GroupID | Address
  33. signal_receiver: str
  34. async def insert(self) -> None:
  35. q = """
  36. INSERT INTO message (mxid, mx_room, sender, timestamp, signal_chat_id, signal_receiver)
  37. VALUES ($1, $2, $3, $4, $5, $6)
  38. """
  39. await self.db.execute(
  40. q,
  41. self.mxid,
  42. self.mx_room,
  43. self.sender.best_identifier,
  44. self.timestamp,
  45. id_to_str(self.signal_chat_id),
  46. self.signal_receiver,
  47. )
  48. async def delete(self) -> None:
  49. q = """
  50. DELETE FROM message
  51. WHERE sender=$1 AND timestamp=$2 AND signal_chat_id=$3 AND signal_receiver=$4
  52. """
  53. await self.db.execute(
  54. q,
  55. self.sender.best_identifier,
  56. self.timestamp,
  57. id_to_str(self.signal_chat_id),
  58. self.signal_receiver,
  59. )
  60. @classmethod
  61. async def delete_all(cls, room_id: RoomID) -> None:
  62. await cls.db.execute("DELETE FROM message WHERE mx_room=$1", room_id)
  63. @classmethod
  64. def _from_row(cls, row: asyncpg.Record) -> Message:
  65. data = {**row}
  66. chat_id = data.pop("signal_chat_id")
  67. if data["signal_receiver"]:
  68. chat_id = Address.parse(chat_id)
  69. sender = Address.parse(data.pop("sender"))
  70. return cls(signal_chat_id=chat_id, sender=sender, **data)
  71. @classmethod
  72. async def get_by_mxid(cls, mxid: EventID, mx_room: RoomID) -> Message | None:
  73. q = """
  74. SELECT mxid, mx_room, sender, timestamp, signal_chat_id, signal_receiver FROM message
  75. WHERE mxid=$1 AND mx_room=$2
  76. """
  77. row = await cls.db.fetchrow(q, mxid, mx_room)
  78. if not row:
  79. return None
  80. return cls._from_row(row)
  81. @classmethod
  82. async def get_by_signal_id(
  83. cls,
  84. sender: Address,
  85. timestamp: int,
  86. signal_chat_id: GroupID | Address,
  87. signal_receiver: str = "",
  88. ) -> Message | None:
  89. q = """
  90. SELECT mxid, mx_room, sender, timestamp, signal_chat_id, signal_receiver FROM message
  91. WHERE sender=$1 AND timestamp=$2 AND signal_chat_id=$3 AND signal_receiver=$4
  92. """
  93. row = await cls.db.fetchrow(
  94. q, sender.best_identifier, timestamp, id_to_str(signal_chat_id), signal_receiver
  95. )
  96. if not row:
  97. return None
  98. return cls._from_row(row)
  99. @classmethod
  100. async def find_by_timestamps(cls, timestamps: list[int]) -> list[Message]:
  101. if cls.db.scheme == "postgres":
  102. q = """
  103. SELECT mxid, mx_room, sender, timestamp, signal_chat_id, signal_receiver FROM message
  104. WHERE timestamp=ANY($1)
  105. """
  106. rows = await cls.db.fetch(q, timestamps)
  107. else:
  108. placeholders = ", ".join("?" for _ in range(len(timestamps)))
  109. q = f"""
  110. SELECT mxid, mx_room, sender, timestamp, signal_chat_id, signal_receiver FROM message
  111. WHERE timestamp IN ({placeholders})
  112. """
  113. rows = await cls.db.fetch(q, *timestamps)
  114. return [cls._from_row(row) for row in rows]
  115. @classmethod
  116. async def find_by_sender_timestamp(cls, sender: Address, timestamp: int) -> Message | None:
  117. q = """
  118. SELECT mxid, mx_room, sender, timestamp, signal_chat_id, signal_receiver FROM message
  119. WHERE sender=$1 AND timestamp=$2
  120. """
  121. row = await cls.db.fetchrow(q, sender.best_identifier, timestamp)
  122. if not row:
  123. return None
  124. return cls._from_row(row)