message.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. ig_timestamp: int
  32. async def insert(self) -> None:
  33. q = (
  34. "INSERT INTO message (mxid, mx_room, item_id, client_context, receiver, sender, ig_timestamp) "
  35. "VALUES ($1, $2, $3, $4, $5, $6, $7)"
  36. )
  37. await self.db.execute(
  38. q,
  39. self.mxid,
  40. self.mx_room,
  41. self.item_id,
  42. self.client_context,
  43. self.receiver,
  44. self.sender,
  45. self.ig_timestamp,
  46. )
  47. async def delete(self) -> None:
  48. q = "DELETE FROM message WHERE item_id=$1 AND receiver=$2"
  49. await self.db.execute(q, self.item_id, self.receiver)
  50. @classmethod
  51. async def delete_all(cls, room_id: RoomID) -> None:
  52. await cls.db.execute("DELETE FROM message WHERE mx_room=$1", room_id)
  53. @classmethod
  54. async def get_by_mxid(cls, mxid: EventID, mx_room: RoomID) -> Message | None:
  55. q = (
  56. "SELECT mxid, mx_room, item_id, client_context, receiver, sender, ig_timestamp "
  57. "FROM message WHERE mxid=$1 AND mx_room=$2"
  58. )
  59. row = await cls.db.fetchrow(q, mxid, mx_room)
  60. if not row:
  61. return None
  62. return cls(**row)
  63. @classmethod
  64. async def get_last(cls, mx_room: RoomID) -> Message | None:
  65. q = (
  66. "SELECT mxid, mx_room, item_id, client_context, receiver, sender, ig_timestamp "
  67. "FROM message WHERE mx_room=$1 AND item_id NOT LIKE 'fi.mau.instagram.%' "
  68. "AND ig_timestamp IS NOT NULL ORDER BY ig_timestamp DESC LIMIT 1"
  69. )
  70. row = await cls.db.fetchrow(q, mx_room)
  71. if not row:
  72. return None
  73. return cls(**row)
  74. @classmethod
  75. async def get_by_item_id(cls, item_id: str, receiver: int) -> Message | None:
  76. q = (
  77. "SELECT mxid, mx_room, item_id, client_context, receiver, sender, ig_timestamp "
  78. "FROM message WHERE item_id=$1 AND receiver=$2"
  79. )
  80. row = await cls.db.fetchrow(q, item_id, receiver)
  81. if not row:
  82. return None
  83. return cls(**row)
  84. @property
  85. def is_internal(self) -> bool:
  86. return self.item_id.startswith("fi.mau.instagram.")