puppet.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # mautrix-signal - A Matrix-Signal puppeting bridge
  2. # Copyright (C) 2020 Tulir Asokan
  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. from uuid import UUID
  19. from attr import dataclass
  20. from yarl import URL
  21. import asyncpg
  22. from mautrix.types import ContentURI, SyncToken, UserID
  23. from mautrix.util.async_db import Database
  24. fake_db = Database.create("") if TYPE_CHECKING else None
  25. @dataclass
  26. class Puppet:
  27. db: ClassVar[Database] = fake_db
  28. uuid: UUID
  29. number: str | None
  30. name: str | None
  31. name_quality: int
  32. avatar_hash: str | None
  33. avatar_url: ContentURI | None
  34. name_set: bool
  35. avatar_set: bool
  36. is_registered: bool
  37. custom_mxid: UserID | None
  38. access_token: str | None
  39. next_batch: SyncToken | None
  40. base_url: URL | None
  41. @property
  42. def _base_url_str(self) -> str | None:
  43. return str(self.base_url) if self.base_url else None
  44. @property
  45. def _values(self):
  46. return (
  47. self.uuid,
  48. self.number,
  49. self.name,
  50. self.name_quality,
  51. self.avatar_hash,
  52. self.avatar_url,
  53. self.name_set,
  54. self.avatar_set,
  55. self.is_registered,
  56. self.custom_mxid,
  57. self.access_token,
  58. self.next_batch,
  59. self._base_url_str,
  60. )
  61. async def insert(self) -> None:
  62. q = """
  63. INSERT INTO puppet (uuid, number, name, name_quality, avatar_hash, avatar_url,
  64. name_set, avatar_set, is_registered,
  65. custom_mxid, access_token, next_batch, base_url)
  66. VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
  67. """
  68. await self.db.execute(q, *self._values)
  69. async def _set_number(self, number: str) -> None:
  70. async with self.db.acquire() as conn, conn.transaction():
  71. await conn.execute(
  72. "UPDATE puppet SET number=null WHERE number=$1 AND uuid<>$2", number, self.uuid
  73. )
  74. await conn.execute("UPDATE puppet SET number=$1 WHERE uuid=$2", number, self.uuid)
  75. async def update(self) -> None:
  76. q = """
  77. UPDATE puppet
  78. SET number=$2, name=$3, name_quality=$4, avatar_hash=$5, avatar_url=$6,
  79. name_set=$7, avatar_set=$8, is_registered=$9,
  80. custom_mxid=$10, access_token=$11, next_batch=$12, base_url=$13
  81. WHERE uuid=$1
  82. """
  83. await self.db.execute(q, *self._values)
  84. @classmethod
  85. def _from_row(cls, row: asyncpg.Record | None) -> Puppet | None:
  86. if not row:
  87. return None
  88. data = {**row}
  89. base_url_str = data.pop("base_url")
  90. base_url = URL(base_url_str) if base_url_str is not None else None
  91. return cls(base_url=base_url, **data)
  92. _select_base = (
  93. "SELECT uuid, number, name, name_quality, avatar_hash, avatar_url, name_set, avatar_set, "
  94. " is_registered, custom_mxid, access_token, next_batch, base_url "
  95. "FROM puppet"
  96. )
  97. @classmethod
  98. async def get_by_uuid(cls, uuid: UUID) -> Puppet | None:
  99. return cls._from_row(await cls.db.fetchrow(f"{cls._select_base} WHERE uuid=$1", uuid))
  100. @classmethod
  101. async def get_by_number(cls, number: str) -> Puppet | None:
  102. return cls._from_row(await cls.db.fetchrow(f"{cls._select_base} WHERE number=$1", number))
  103. @classmethod
  104. async def get_by_custom_mxid(cls, mxid: UserID) -> Puppet | None:
  105. return cls._from_row(
  106. await cls.db.fetchrow(f"{cls._select_base} WHERE custom_mxid=$1", mxid)
  107. )
  108. @classmethod
  109. async def all_with_custom_mxid(cls) -> list[Puppet]:
  110. return [
  111. cls._from_row(row)
  112. for row in await cls.db.fetch(f"{cls._select_base} WHERE custom_mxid IS NOT NULL")
  113. ]