message.py 3.7 KB

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