disappearing_message.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # mautrix-signal - A Matrix-Signal puppeting bridge
  2. # Copyright (C) 2021 Sumner Evans
  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. import asyncpg
  20. from mautrix.types import EventID, RoomID
  21. from mautrix.util.async_db import Database
  22. fake_db = Database.create("") if TYPE_CHECKING else None
  23. @dataclass
  24. class DisappearingMessage:
  25. db: ClassVar[Database] = fake_db
  26. room_id: RoomID
  27. mxid: EventID
  28. expiration_seconds: int
  29. expiration_ts: int | None = None
  30. async def insert(self) -> None:
  31. q = """
  32. INSERT INTO disappearing_message (room_id, mxid, expiration_seconds, expiration_ts)
  33. VALUES ($1, $2, $3, $4)
  34. """
  35. await self.db.execute(
  36. q, self.room_id, self.mxid, self.expiration_seconds, self.expiration_ts
  37. )
  38. async def set_expiration_ts(self, ts: int) -> None:
  39. q = "UPDATE disappearing_message SET expiration_ts=$3 WHERE room_id=$1 AND mxid=$2"
  40. self.expiration_ts = ts
  41. await self.db.execute(q, self.room_id, self.mxid, self.expiration_ts)
  42. @classmethod
  43. async def delete(cls, room_id: RoomID, event_id: EventID) -> None:
  44. q = "DELETE from disappearing_message WHERE room_id=$1 AND mxid=$2"
  45. await cls.db.execute(q, room_id, event_id)
  46. @classmethod
  47. def _from_row(cls, row: asyncpg.Record) -> DisappearingMessage:
  48. return cls(**row)
  49. @classmethod
  50. async def get(cls, room_id: RoomID, event_id: EventID) -> DisappearingMessage | None:
  51. q = """
  52. SELECT room_id, mxid, expiration_seconds, expiration_ts FROM disappearing_message
  53. WHERE room_id=$1 AND mxid=$2
  54. """
  55. try:
  56. return cls._from_row(await cls.db.fetchrow(q, room_id, event_id))
  57. except Exception:
  58. return None
  59. @classmethod
  60. async def get_all_scheduled(cls) -> list[DisappearingMessage]:
  61. q = """
  62. SELECT room_id, mxid, expiration_seconds, expiration_ts FROM disappearing_message
  63. WHERE expiration_ts IS NOT NULL
  64. """
  65. return [cls._from_row(r) for r in await cls.db.fetch(q)]
  66. @classmethod
  67. async def get_unscheduled_for_room(cls, room_id: RoomID) -> list[DisappearingMessage]:
  68. q = """
  69. SELECT room_id, mxid, expiration_seconds, expiration_ts FROM disappearing_message
  70. WHERE room_id = $1 AND expiration_ts IS NULL
  71. """
  72. return [cls._from_row(r) for r in await cls.db.fetch(q, room_id)]