reaction.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 typing import Optional, ClassVar, Union, TYPE_CHECKING
  17. from uuid import UUID
  18. from attr import dataclass
  19. import asyncpg
  20. from mausignald.types import Address, GroupID
  21. from mautrix.types import RoomID, EventID
  22. from mautrix.util.async_db import Database
  23. from ..util import id_to_str
  24. fake_db = Database.create("") if TYPE_CHECKING else None
  25. @dataclass
  26. class Reaction:
  27. db: ClassVar[Database] = fake_db
  28. mxid: EventID
  29. mx_room: RoomID
  30. signal_chat_id: Union[GroupID, Address]
  31. signal_receiver: str
  32. msg_author: Address
  33. msg_timestamp: int
  34. author: Address
  35. emoji: str
  36. async def insert(self) -> None:
  37. q = (
  38. "INSERT INTO reaction (mxid, mx_room, signal_chat_id, signal_receiver, msg_author,"
  39. " msg_timestamp, author, emoji) "
  40. "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"
  41. )
  42. await self.db.execute(
  43. q,
  44. self.mxid,
  45. self.mx_room,
  46. id_to_str(self.signal_chat_id),
  47. self.signal_receiver,
  48. self.msg_author.best_identifier,
  49. self.msg_timestamp,
  50. self.author.best_identifier,
  51. self.emoji,
  52. )
  53. async def edit(self, mx_room: RoomID, mxid: EventID, emoji: str) -> None:
  54. await self.db.execute(
  55. "UPDATE reaction SET mxid=$1, mx_room=$2, emoji=$3 "
  56. "WHERE signal_chat_id=$4 AND signal_receiver=$5"
  57. " AND msg_author=$6 AND msg_timestamp=$7 AND author=$8",
  58. mxid,
  59. mx_room,
  60. emoji,
  61. id_to_str(self.signal_chat_id),
  62. self.signal_receiver,
  63. self.msg_author.best_identifier,
  64. self.msg_timestamp,
  65. self.author.best_identifier,
  66. )
  67. async def delete(self) -> None:
  68. q = (
  69. "DELETE FROM reaction WHERE signal_chat_id=$1 AND signal_receiver=$2"
  70. " AND msg_author=$3 AND msg_timestamp=$4 AND author=$5"
  71. )
  72. await self.db.execute(
  73. q,
  74. id_to_str(self.signal_chat_id),
  75. self.signal_receiver,
  76. self.msg_author.best_identifier,
  77. self.msg_timestamp,
  78. self.author.best_identifier,
  79. )
  80. @classmethod
  81. def _from_row(cls, row: asyncpg.Record) -> "Reaction":
  82. data = {**row}
  83. chat_id = data.pop("signal_chat_id")
  84. if data["signal_receiver"]:
  85. chat_id = Address.parse(chat_id)
  86. msg_author = Address.parse(data.pop("msg_author"))
  87. author = Address.parse(data.pop("author"))
  88. return cls(signal_chat_id=chat_id, msg_author=msg_author, author=author, **data)
  89. @classmethod
  90. async def get_by_mxid(cls, mxid: EventID, mx_room: RoomID) -> Optional["Reaction"]:
  91. q = (
  92. "SELECT mxid, mx_room, signal_chat_id, signal_receiver,"
  93. " msg_author, msg_timestamp, author, emoji "
  94. "FROM reaction WHERE mxid=$1 AND mx_room=$2"
  95. )
  96. row = await cls.db.fetchrow(q, mxid, mx_room)
  97. if not row:
  98. return None
  99. return cls._from_row(row)
  100. @classmethod
  101. async def get_by_signal_id(
  102. cls,
  103. chat_id: Union[GroupID, Address],
  104. receiver: str,
  105. msg_author: Address,
  106. msg_timestamp: int,
  107. author: Address,
  108. ) -> Optional["Reaction"]:
  109. q = (
  110. "SELECT mxid, mx_room, signal_chat_id, signal_receiver,"
  111. " msg_author, msg_timestamp, author, emoji "
  112. "FROM reaction WHERE signal_chat_id=$1 AND signal_receiver=$2"
  113. " AND msg_author=$3 AND msg_timestamp=$4 AND author=$5"
  114. )
  115. row = await cls.db.fetchrow(
  116. q,
  117. id_to_str(chat_id),
  118. receiver,
  119. msg_author.best_identifier,
  120. msg_timestamp,
  121. author.best_identifier,
  122. )
  123. if not row:
  124. return None
  125. return cls._from_row(row)