disappearing_message.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. import asyncpg
  19. from mautrix.bridge import AbstractDisappearingMessage
  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. class DisappearingMessage(AbstractDisappearingMessage):
  24. db: ClassVar[Database] = fake_db
  25. async def insert(self) -> None:
  26. q = """
  27. INSERT INTO disappearing_message (room_id, mxid, expiration_seconds, expiration_ts)
  28. VALUES ($1, $2, $3, $4)
  29. """
  30. await self.db.execute(
  31. q, self.room_id, self.event_id, self.expiration_seconds, self.expiration_ts
  32. )
  33. async def update(self) -> None:
  34. q = "UPDATE disappearing_message SET expiration_ts=$3 WHERE room_id=$1 AND mxid=$2"
  35. await self.db.execute(q, self.room_id, self.event_id, self.expiration_ts)
  36. async def delete(self) -> None:
  37. q = "DELETE from disappearing_message WHERE room_id=$1 AND mxid=$2"
  38. await self.db.execute(q, self.room_id, self.event_id)
  39. @classmethod
  40. def _from_row(cls, row: asyncpg.Record) -> DisappearingMessage:
  41. return cls(row["room_id"], row["mxid"], row["expiration_seconds"], row["expiration_ts"])
  42. @classmethod
  43. async def get(cls, room_id: RoomID, event_id: EventID) -> DisappearingMessage | None:
  44. q = """
  45. SELECT room_id, mxid, expiration_seconds, expiration_ts FROM disappearing_message
  46. WHERE room_id=$1 AND mxid=$2
  47. """
  48. try:
  49. return cls._from_row(await cls.db.fetchrow(q, room_id, event_id))
  50. except Exception:
  51. return None
  52. @classmethod
  53. async def get_all_scheduled(cls) -> list[DisappearingMessage]:
  54. q = """
  55. SELECT room_id, mxid, expiration_seconds, expiration_ts FROM disappearing_message
  56. WHERE expiration_ts IS NOT NULL
  57. """
  58. return [cls._from_row(r) for r in await cls.db.fetch(q)]
  59. @classmethod
  60. async def get_unscheduled_for_room(cls, room_id: RoomID) -> list[DisappearingMessage]:
  61. q = """
  62. SELECT room_id, mxid, expiration_seconds, expiration_ts FROM disappearing_message
  63. WHERE room_id = $1 AND expiration_ts IS NULL
  64. """
  65. return [cls._from_row(r) for r in await cls.db.fetch(q, room_id)]