disappearing_message.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 typing import ClassVar, List, Optional, TYPE_CHECKING
  17. from attr import dataclass
  18. import asyncpg
  19. from mautrix.types import RoomID, EventID
  20. from mautrix.util.async_db import Database
  21. fake_db = Database.create("") if TYPE_CHECKING else None
  22. @dataclass
  23. class DisappearingMessage:
  24. db: ClassVar[Database] = fake_db
  25. room_id: RoomID
  26. mxid: EventID
  27. expiration_seconds: int
  28. expiration_ts: Optional[int] = None
  29. async def insert(self) -> None:
  30. q = """
  31. INSERT INTO disappearing_message (room_id, mxid, expiration_seconds, expiration_ts)
  32. VALUES ($1, $2, $3, $4)
  33. """
  34. await self.db.execute(q, self.room_id, self.mxid, self.expiration_seconds,
  35. self.expiration_ts)
  36. async def update(self) -> None:
  37. q = """
  38. UPDATE disappearing_message
  39. SET expiration_seconds=$3, expiration_ts=$4
  40. WHERE room_id=$1 AND mxid=$2
  41. """
  42. try:
  43. await self.db.execute(q, self.room_id, self.mxid, self.expiration_seconds,
  44. self.expiration_ts)
  45. except Exception as e:
  46. print(e)
  47. @classmethod
  48. async def delete(cls, room_id: RoomID, event_id: EventID) -> None:
  49. q = "DELETE from disappearing_message WHERE room_id=$1 AND mxid=$2"
  50. await cls.db.execute(q, room_id, event_id)
  51. @classmethod
  52. def _from_row(cls, row: asyncpg.Record) -> "DisappearingMessage":
  53. return cls(**row)
  54. @classmethod
  55. async def get(cls, room_id: RoomID, event_id: EventID) -> Optional["DisappearingMessage"]:
  56. q = """
  57. SELECT room_id, mxid, expiration_seconds, expiration_ts
  58. FROM disappearing_message
  59. WHERE room_id = $1
  60. AND mxid = $2
  61. """
  62. try:
  63. return cls._from_row(await cls.db.fetchrow(q, room_id, event_id))
  64. except Exception:
  65. return None
  66. @classmethod
  67. async def get_all(cls) -> List["DisappearingMessage"]:
  68. q = "SELECT room_id, mxid, expiration_seconds, expiration_ts FROM disappearing_message"
  69. return [cls._from_row(r) for r in await cls.db.fetch(q)]
  70. @classmethod
  71. async def get_all_for_room(cls, room_id: RoomID) -> List["DisappearingMessage"]:
  72. q = """
  73. SELECT room_id, mxid, expiration_seconds, expiration_ts
  74. FROM disappearing_message
  75. WHERE room_id = $1
  76. """
  77. return [cls._from_row(r) for r in await cls.db.fetch(q, room_id)]