message.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 Message:
  24. db: ClassVar[Database] = fake_db
  25. mxid: EventID
  26. mx_room: RoomID
  27. item_id: str
  28. client_context: str | None
  29. receiver: int
  30. sender: int
  31. async def insert(self) -> None:
  32. q = (
  33. "INSERT INTO message (mxid, mx_room, item_id, client_context, receiver, sender) "
  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.item_id,
  41. self.client_context,
  42. self.receiver,
  43. self.sender,
  44. )
  45. async def delete(self) -> None:
  46. q = "DELETE FROM message WHERE item_id=$1 AND receiver=$2"
  47. await self.db.execute(q, self.item_id, self.receiver)
  48. @classmethod
  49. async def delete_all(cls, room_id: RoomID) -> None:
  50. await cls.db.execute("DELETE FROM message WHERE mx_room=$1", room_id)
  51. @classmethod
  52. async def get_by_mxid(cls, mxid: EventID, mx_room: RoomID) -> Message | None:
  53. q = (
  54. "SELECT mxid, mx_room, item_id, client_context, receiver, sender "
  55. "FROM message WHERE mxid=$1 AND mx_room=$2"
  56. )
  57. row = await cls.db.fetchrow(q, mxid, mx_room)
  58. if not row:
  59. return None
  60. return cls(**row)
  61. @classmethod
  62. async def get_by_item_id(cls, item_id: str, receiver: int) -> Message | None:
  63. q = (
  64. "SELECT mxid, mx_room, item_id, client_context, receiver, sender "
  65. "FROM message WHERE item_id=$1 AND receiver=$2"
  66. )
  67. row = await cls.db.fetchrow(q, item_id, receiver)
  68. if not row:
  69. return None
  70. return cls(**row)
  71. @property
  72. def is_internal(self) -> bool:
  73. return self.item_id.startswith("fi.mau.instagram.")