disappearing_message.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, List, Optional
  18. from attr import dataclass
  19. from mautrix.types import EventID, RoomID
  20. from mautrix.util.async_db import Database
  21. import asyncpg
  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: Optional[int] = 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 update(self) -> None:
  39. q = """
  40. UPDATE disappearing_message
  41. SET expiration_seconds=$3, expiration_ts=$4
  42. WHERE room_id=$1 AND mxid=$2
  43. """
  44. try:
  45. await self.db.execute(
  46. q, self.room_id, self.mxid, self.expiration_seconds, self.expiration_ts
  47. )
  48. except Exception as e:
  49. print(e)
  50. @classmethod
  51. async def delete(cls, room_id: RoomID, event_id: EventID) -> None:
  52. q = "DELETE from disappearing_message WHERE room_id=$1 AND mxid=$2"
  53. await cls.db.execute(q, room_id, event_id)
  54. @classmethod
  55. def _from_row(cls, row: asyncpg.Record) -> DisappearingMessage:
  56. return cls(**row)
  57. @classmethod
  58. async def get(cls, room_id: RoomID, event_id: EventID) -> Optional[DisappearingMessage]:
  59. q = """
  60. SELECT room_id, mxid, expiration_seconds, expiration_ts
  61. FROM disappearing_message
  62. WHERE room_id = $1
  63. AND mxid = $2
  64. """
  65. try:
  66. return cls._from_row(await cls.db.fetchrow(q, room_id, event_id))
  67. except Exception:
  68. return None
  69. @classmethod
  70. async def get_all(cls) -> List[DisappearingMessage]:
  71. q = "SELECT room_id, mxid, expiration_seconds, expiration_ts FROM disappearing_message"
  72. return [cls._from_row(r) for r in await cls.db.fetch(q)]
  73. @classmethod
  74. async def get_all_for_room(cls, room_id: RoomID) -> List[DisappearingMessage]:
  75. q = """
  76. SELECT room_id, mxid, expiration_seconds, expiration_ts
  77. FROM disappearing_message
  78. WHERE room_id = $1
  79. """
  80. return [cls._from_row(r) for r in await cls.db.fetch(q, room_id)]