reaction.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. async def insert(self) -> None:
  32. q = (
  33. "INSERT INTO reaction (mxid, mx_room, ig_item_id, ig_receiver, ig_sender, reaction) "
  34. "VALUES ($1, $2, $3, $4, $5, $6)"
  35. )
  36. await self.db.execute(
  37. q,
  38. self.mxid,
  39. self.mx_room,
  40. self.ig_item_id,
  41. self.ig_receiver,
  42. self.ig_sender,
  43. self.reaction,
  44. )
  45. async def edit(self, mx_room: RoomID, mxid: EventID, reaction: str) -> None:
  46. q = (
  47. "UPDATE reaction SET mxid=$1, mx_room=$2, reaction=$3 "
  48. "WHERE ig_item_id=$4 AND ig_receiver=$5 AND ig_sender=$6"
  49. )
  50. await self.db.execute(
  51. q, mxid, mx_room, reaction, self.ig_item_id, self.ig_receiver, self.ig_sender
  52. )
  53. async def delete(self) -> None:
  54. q = "DELETE FROM reaction WHERE ig_item_id=$1 AND ig_receiver=$2 AND ig_sender=$3"
  55. await self.db.execute(q, self.ig_item_id, self.ig_receiver, self.ig_sender)
  56. @classmethod
  57. async def get_by_mxid(cls, mxid: EventID, mx_room: RoomID) -> Reaction | None:
  58. q = (
  59. "SELECT mxid, mx_room, ig_item_id, ig_receiver, ig_sender, reaction "
  60. "FROM reaction WHERE mxid=$1 AND mx_room=$2"
  61. )
  62. row = await cls.db.fetchrow(q, mxid, mx_room)
  63. if not row:
  64. return None
  65. return cls(**row)
  66. @classmethod
  67. async def get_by_item_id(
  68. cls, ig_item_id: str, ig_receiver: int, ig_sender: int
  69. ) -> Reaction | None:
  70. q = (
  71. "SELECT mxid, mx_room, ig_item_id, ig_receiver, ig_sender, reaction "
  72. "FROM reaction WHERE ig_item_id=$1 AND ig_sender=$2 AND ig_receiver=$3"
  73. )
  74. row = await cls.db.fetchrow(q, ig_item_id, ig_sender, ig_receiver)
  75. if not row:
  76. return None
  77. return cls(**row)
  78. @classmethod
  79. async def count(cls, ig_item_id: str, ig_receiver: int) -> int:
  80. q = "SELECT COUNT(*) FROM reaction WHERE ig_item_id=$1 AND ig_receiver=$2"
  81. return await cls.db.fetchval(q, ig_item_id, ig_receiver)
  82. @classmethod
  83. async def get_all_by_item_id(cls, ig_item_id: str, ig_receiver: int) -> list[Reaction]:
  84. q = (
  85. "SELECT mxid, mx_room, ig_item_id, ig_receiver, ig_sender, reaction "
  86. "FROM reaction WHERE ig_item_id=$1 AND ig_receiver=$2"
  87. )
  88. rows = await cls.db.fetch(q, ig_item_id, ig_receiver)
  89. return [cls(**row) for row in rows]