disappearing_message.py 3.1 KB

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