puppet.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 Connection, 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. contact_info_set: bool
  37. is_registered: bool
  38. custom_mxid: UserID | None
  39. access_token: str | None
  40. next_batch: SyncToken | None
  41. base_url: URL | None
  42. @property
  43. def _base_url_str(self) -> str | None:
  44. return str(self.base_url) if self.base_url else None
  45. @property
  46. def _values(self):
  47. return (
  48. self.uuid,
  49. self.number,
  50. self.name,
  51. self.name_quality,
  52. self.avatar_hash,
  53. self.avatar_url,
  54. self.name_set,
  55. self.avatar_set,
  56. self.contact_info_set,
  57. self.is_registered,
  58. self.custom_mxid,
  59. self.access_token,
  60. self.next_batch,
  61. self._base_url_str,
  62. )
  63. async def _delete_existing_number(self, conn: Connection) -> None:
  64. if not self.number:
  65. return
  66. await conn.execute(
  67. "UPDATE puppet SET number=null WHERE number=$1 AND uuid<>$2", self.number, self.uuid
  68. )
  69. async def insert(self) -> None:
  70. q = """
  71. INSERT INTO puppet (uuid, number, name, name_quality, avatar_hash, avatar_url,
  72. name_set, avatar_set, contact_info_set, is_registered,
  73. custom_mxid, access_token, next_batch, base_url)
  74. VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
  75. """
  76. async with self.db.acquire() as conn, conn.transaction():
  77. await self._delete_existing_number(conn)
  78. await conn.execute(q, *self._values)
  79. async def _update_number(self) -> None:
  80. async with self.db.acquire() as conn, conn.transaction():
  81. await self._delete_existing_number(conn)
  82. await conn.execute("UPDATE puppet SET number=$1 WHERE uuid=$2", self.number, self.uuid)
  83. async def update(self) -> None:
  84. q = """
  85. UPDATE puppet
  86. SET number=$2, name=$3, name_quality=$4, avatar_hash=$5, avatar_url=$6,
  87. name_set=$7, avatar_set=$8, contact_info_set=$9, is_registered=$10,
  88. custom_mxid=$11, access_token=$12, next_batch=$13, base_url=$14
  89. WHERE uuid=$1
  90. """
  91. await self.db.execute(q, *self._values)
  92. @classmethod
  93. def _from_row(cls, row: asyncpg.Record | None) -> Puppet | None:
  94. if not row:
  95. return None
  96. data = {**row}
  97. base_url_str = data.pop("base_url")
  98. base_url = URL(base_url_str) if base_url_str is not None else None
  99. return cls(base_url=base_url, **data)
  100. _select_base = """
  101. SELECT uuid, number, name, name_quality, avatar_hash, avatar_url, name_set, avatar_set,
  102. contact_info_set, is_registered, custom_mxid, access_token, next_batch, base_url
  103. FROM puppet
  104. """
  105. @classmethod
  106. async def get_by_uuid(cls, uuid: UUID) -> Puppet | None:
  107. return cls._from_row(await cls.db.fetchrow(f"{cls._select_base} WHERE uuid=$1", uuid))
  108. @classmethod
  109. async def get_by_number(cls, number: str) -> Puppet | None:
  110. return cls._from_row(await cls.db.fetchrow(f"{cls._select_base} WHERE number=$1", number))
  111. @classmethod
  112. async def get_by_custom_mxid(cls, mxid: UserID) -> Puppet | None:
  113. return cls._from_row(
  114. await cls.db.fetchrow(f"{cls._select_base} WHERE custom_mxid=$1", mxid)
  115. )
  116. @classmethod
  117. async def all_with_custom_mxid(cls) -> list[Puppet]:
  118. return [
  119. cls._from_row(row)
  120. for row in await cls.db.fetch(f"{cls._select_base} WHERE custom_mxid IS NOT NULL")
  121. ]