reaction.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 typing import Optional, ClassVar, List, TYPE_CHECKING
  17. from attr import dataclass
  18. from mautrix.types import RoomID, EventID
  19. from mautrix.util.async_db import Database
  20. fake_db = Database("") if TYPE_CHECKING else None
  21. @dataclass
  22. class Reaction:
  23. db: ClassVar[Database] = fake_db
  24. mxid: EventID
  25. mx_room: RoomID
  26. ig_item_id: str
  27. ig_receiver: int
  28. ig_sender: int
  29. reaction: str
  30. async def insert(self) -> None:
  31. q = ("INSERT INTO reaction (mxid, mx_room, ig_item_id, ig_receiver, ig_sender, reaction) "
  32. "VALUES ($1, $2, $3, $4, $5, $6)")
  33. await self.db.execute(q, self.mxid, self.mx_room, self.ig_item_id, self.ig_receiver,
  34. self.ig_sender, self.reaction)
  35. async def edit(self, mx_room: RoomID, mxid: EventID, reaction: str) -> None:
  36. await self.db.execute("UPDATE reaction SET mxid=$1, mx_room=$2, reaction=$3 "
  37. "WHERE ig_item_id=$4 AND ig_receiver=$5 AND ig_sender=$6",
  38. mxid, mx_room, reaction, self.ig_item_id, self.ig_receiver,
  39. self.ig_sender)
  40. async def delete(self) -> None:
  41. q = "DELETE FROM reaction WHERE ig_item_id=$1 AND ig_receiver=$2 AND ig_sender=$3"
  42. await self.db.execute(q, self.ig_item_id, self.ig_receiver, self.ig_sender)
  43. @classmethod
  44. async def get_by_mxid(cls, mxid: EventID, mx_room: RoomID) -> Optional['Reaction']:
  45. q = ("SELECT mxid, mx_room, ig_item_id, ig_receiver, ig_sender, reaction "
  46. "FROM reaction WHERE mxid=$1 AND mx_room=$2")
  47. row = await cls.db.fetchrow(q, mxid, mx_room)
  48. if not row:
  49. return None
  50. return cls(**row)
  51. @classmethod
  52. async def get_by_item_id(cls, ig_item_id: str, ig_receiver: int, ig_sender: int,
  53. ) -> Optional['Reaction']:
  54. q = ("SELECT mxid, mx_room, ig_item_id, ig_receiver, ig_sender, reaction "
  55. "FROM reaction WHERE ig_item_id=$1 AND ig_sender=$2 AND ig_receiver=$3")
  56. row = await cls.db.fetchrow(q, ig_item_id, ig_sender, ig_receiver)
  57. if not row:
  58. return None
  59. return cls(**row)
  60. @classmethod
  61. async def count(cls, ig_item_id: str, ig_receiver: int) -> int:
  62. q = "SELECT COUNT(*) FROM reaction WHERE ig_item_id=$1 AND ig_receiver=$2"
  63. return await cls.db.fetchval(q, ig_item_id, ig_receiver)
  64. @classmethod
  65. async def get_all_by_item_id(cls, ig_item_id: str, ig_receiver: int) -> List['Reaction']:
  66. q = ("SELECT mxid, mx_room, ig_item_id, ig_receiver, ig_sender, reaction "
  67. "FROM reaction WHERE ig_item_id=$1 AND ig_receiver=$2")
  68. rows = await cls.db.fetch(q, ig_item_id, ig_receiver)
  69. return [cls(**row) for row in rows]