reaction.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # mautrix-instagram - A Matrix-Instagram 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. fake_db = Database.create("") if TYPE_CHECKING else None
  22. @dataclass
  23. class Reaction:
  24. db: ClassVar[Database] = fake_db
  25. mxid: EventID
  26. mx_room: RoomID
  27. ig_item_id: str
  28. ig_receiver: int
  29. ig_sender: int
  30. reaction: str
  31. mx_timestamp: int | None
  32. async def insert(self) -> None:
  33. q = """
  34. INSERT INTO reaction (mxid, mx_room, ig_item_id, ig_receiver, ig_sender, reaction,
  35. mx_timestamp)
  36. VALUES ($1, $2, $3, $4, $5, $6, $7)
  37. """
  38. await self.db.execute(
  39. q,
  40. self.mxid,
  41. self.mx_room,
  42. self.ig_item_id,
  43. self.ig_receiver,
  44. self.ig_sender,
  45. self.reaction,
  46. self.mx_timestamp,
  47. )
  48. async def edit(self, mx_room: RoomID, mxid: EventID, reaction: str, mx_timestamp: int) -> None:
  49. q = """
  50. UPDATE reaction SET mxid=$1, mx_room=$2, reaction=$3, mx_timestamp=$4
  51. WHERE ig_item_id=$5 AND ig_receiver=$6 AND ig_sender=$7
  52. """
  53. await self.db.execute(
  54. q,
  55. mxid,
  56. mx_room,
  57. reaction,
  58. mx_timestamp,
  59. self.ig_item_id,
  60. self.ig_receiver,
  61. self.ig_sender,
  62. )
  63. async def delete(self) -> None:
  64. q = "DELETE FROM reaction WHERE ig_item_id=$1 AND ig_receiver=$2 AND ig_sender=$3"
  65. await self.db.execute(q, self.ig_item_id, self.ig_receiver, self.ig_sender)
  66. _columns = "mxid, mx_room, ig_item_id, ig_receiver, ig_sender, reaction, mx_timestamp"
  67. @classmethod
  68. async def get_by_mxid(cls, mxid: EventID, mx_room: RoomID) -> Reaction | None:
  69. q = f"SELECT {cls._columns} FROM reaction WHERE mxid=$1 AND mx_room=$2"
  70. row = await cls.db.fetchrow(q, mxid, mx_room)
  71. if not row:
  72. return None
  73. return cls(**row)
  74. @classmethod
  75. async def get_by_item_id(
  76. cls, ig_item_id: str, ig_receiver: int, ig_sender: int
  77. ) -> Reaction | None:
  78. q = (
  79. f"SELECT {cls._columns} FROM reaction"
  80. " WHERE ig_item_id=$1 AND ig_sender=$2 AND ig_receiver=$3"
  81. )
  82. row = await cls.db.fetchrow(q, ig_item_id, ig_sender, ig_receiver)
  83. if not row:
  84. return None
  85. return cls(**row)
  86. @classmethod
  87. async def count(cls, ig_item_id: str, ig_receiver: int) -> int:
  88. q = "SELECT COUNT(*) FROM reaction WHERE ig_item_id=$1 AND ig_receiver=$2"
  89. return await cls.db.fetchval(q, ig_item_id, ig_receiver)
  90. @classmethod
  91. async def get_all_by_item_id(cls, ig_item_id: str, ig_receiver: int) -> list[Reaction]:
  92. q = f"SELECT {cls._columns} FROM reaction WHERE ig_item_id=$1 AND ig_receiver=$2"
  93. rows = await cls.db.fetch(q, ig_item_id, ig_receiver)
  94. return [cls(**row) for row in rows]
  95. @classmethod
  96. async def get_closest(cls, mx_room: RoomID, before_ts: int) -> Reaction | None:
  97. q = f"""
  98. SELECT {cls._columns} FROM reaction WHERE mx_room=$1 AND mx_timestamp<=$2
  99. ORDER BY mx_timestamp DESC LIMIT 1
  100. """
  101. row = await cls.db.fetchrow(q, mx_room, before_ts)
  102. if not row:
  103. return None
  104. return cls(**row)