message.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2022 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 | None
  32. async def insert(self) -> None:
  33. q = """
  34. INSERT INTO message (mxid, mx_room, item_id, client_context, receiver, sender,
  35. ig_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.item_id,
  43. self.client_context,
  44. self.receiver,
  45. self.sender,
  46. self.ig_timestamp,
  47. )
  48. async def delete(self) -> None:
  49. q = "DELETE FROM message WHERE item_id=$1 AND receiver=$2"
  50. await self.db.execute(q, self.item_id, self.receiver)
  51. @classmethod
  52. async def delete_all(cls, room_id: RoomID) -> None:
  53. await cls.db.execute("DELETE FROM message WHERE mx_room=$1", room_id)
  54. _columns = "mxid, mx_room, item_id, client_context, receiver, sender, ig_timestamp"
  55. @classmethod
  56. async def get_by_mxid(cls, mxid: EventID, mx_room: RoomID) -> Message | None:
  57. q = f"SELECT {cls._columns} FROM message WHERE mxid=$1 AND mx_room=$2"
  58. row = await cls.db.fetchrow(q, mxid, mx_room)
  59. if not row:
  60. return None
  61. return cls(**row)
  62. @classmethod
  63. async def get_last(cls, mx_room: RoomID) -> Message | None:
  64. q = f"""
  65. SELECT {cls._columns} FROM message
  66. WHERE mx_room=$1 AND ig_timestamp IS NOT NULL AND item_id NOT LIKE 'fi.mau.instagram.%'
  67. ORDER BY ig_timestamp DESC LIMIT 1
  68. """
  69. row = await cls.db.fetchrow(q, mx_room)
  70. if not row:
  71. return None
  72. return cls(**row)
  73. @classmethod
  74. async def get_closest(cls, mx_room: RoomID, before_ts: int) -> Message | None:
  75. q = f"""
  76. SELECT {cls._columns} FROM message
  77. WHERE mx_room=$1 AND ig_timestamp<=$2 AND item_id NOT LIKE 'fi.mau.instagram.%'
  78. ORDER BY ig_timestamp DESC LIMIT 1
  79. """
  80. row = await cls.db.fetchrow(q, mx_room, before_ts)
  81. if not row:
  82. return None
  83. return cls(**row)
  84. @classmethod
  85. async def get_by_item_id(cls, item_id: str, receiver: int) -> Message | None:
  86. q = f"SELECT {cls._columns} FROM message WHERE item_id=$1 AND receiver=$2"
  87. row = await cls.db.fetchrow(q, item_id, receiver)
  88. if not row:
  89. return None
  90. return cls(**row)
  91. @property
  92. def is_internal(self) -> bool:
  93. return self.item_id.startswith("fi.mau.instagram.")