reaction.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, TYPE_CHECKING
  17. from uuid import UUID
  18. from attr import dataclass
  19. import asyncpg
  20. from mautrix.types import RoomID, EventID
  21. from mautrix.util.async_db import Database
  22. fake_db = Database("") if TYPE_CHECKING else None
  23. @dataclass
  24. class Reaction:
  25. db: ClassVar[Database] = fake_db
  26. mxid: EventID
  27. mx_room: RoomID
  28. signal_chat_id: str
  29. signal_receiver: str
  30. msg_author: UUID
  31. msg_timestamp: int
  32. author: UUID
  33. emoji: str
  34. async def insert(self) -> None:
  35. q = ("INSERT INTO reaction (mxid, mx_room, signal_chat_id, signal_receiver, msg_author,"
  36. " msg_timestamp, author, emoji) "
  37. "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)")
  38. await self.db.execute(q, self.mxid, self.mx_room, self.signal_chat_id,
  39. self.signal_receiver, self.msg_author, self.msg_timestamp,
  40. self.author, self.emoji)
  41. async def edit(self, mx_room: RoomID, mxid: EventID, emoji: str) -> None:
  42. await self.db.execute("UPDATE reaction SET mxid=$1, mx_room=$2, emoji=$3 "
  43. "WHERE signal_chat_id=$4 AND signal_receiver=$5"
  44. " AND msg_author=$6 AND msg_timestamp=$7 AND author=$8",
  45. mxid, mx_room, emoji, self.signal_chat_id, self.signal_receiver,
  46. self.msg_author, self.msg_timestamp, self.author)
  47. async def delete(self) -> None:
  48. q = ("DELETE FROM reaction WHERE signal_chat_id=$1 AND signal_receiver=$2"
  49. " AND msg_author=$3 AND msg_timestamp=$4 AND author=$5")
  50. await self.db.execute(q, self.signal_chat_id, self.signal_receiver, self.msg_author,
  51. self.msg_timestamp, self.author)
  52. @classmethod
  53. def _from_row(cls, row: asyncpg.Record) -> 'Reaction':
  54. data = {**row}
  55. if data["signal_receiver"]:
  56. chat_id = UUID(data.pop("signal_chat_id"))
  57. else:
  58. chat_id = data.pop("signal_chat_id")
  59. return cls(signal_chat_id=chat_id, **data)
  60. @classmethod
  61. async def get_by_mxid(cls, mxid: EventID, mx_room: RoomID) -> Optional['Reaction']:
  62. q = ("SELECT mxid, mx_room, signal_chat_id, signal_receiver,"
  63. " msg_author, msg_timestamp, author, emoji "
  64. "FROM reaction WHERE mxid=$1 AND mx_room=$2")
  65. row = await cls.db.fetchrow(q, mxid, mx_room)
  66. if not row:
  67. return None
  68. return cls(**row)
  69. @classmethod
  70. async def get_by_signal_id(cls, chat_id: str, receiver: str, msg_author: UUID,
  71. msg_timestamp: int, author: UUID) -> Optional['Reaction']:
  72. q = ("SELECT mxid, mx_room, signal_chat_id, signal_receiver,"
  73. " msg_author, msg_timestamp, author, emoji "
  74. "FROM reaction WHERE signal_chat_id=$1 AND signal_receiver=$2"
  75. " AND msg_author=$3 AND msg_timestamp=$4 AND author=$5")
  76. row = await cls.db.fetchrow(q, chat_id, receiver, msg_author, msg_timestamp, author)
  77. if not row:
  78. return None
  79. return cls(**row)