disappearing_message.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_messages (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_messages
  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_messages WHERE room_id=$1 AND mxid=$2"
  50. await cls.db.execute(q, room_id, event_id)
  51. @classmethod
  52. async def delete_all(cls, room_id: RoomID) -> None:
  53. await cls.db.execute("DELETE FROM message WHERE room_id=$1", room_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_messages
  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_messages"
  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_messages
  78. WHERE room_id = $1
  79. """
  80. return [cls._from_row(r) for r in await cls.db.fetch(q, room_id)]