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