puppet.py 4.2 KB

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