puppet.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 _values(self):
  41. return (
  42. self.pk,
  43. self.name,
  44. self.username,
  45. self.photo_id,
  46. self.photo_mxc,
  47. self.name_set,
  48. self.avatar_set,
  49. self.is_registered,
  50. self.custom_mxid,
  51. self.access_token,
  52. self.next_batch,
  53. str(self.base_url) if self.base_url else None,
  54. )
  55. async def insert(self) -> None:
  56. q = (
  57. "INSERT INTO puppet (pk, name, username, photo_id, photo_mxc, name_set, avatar_set,"
  58. " is_registered, custom_mxid, access_token, next_batch, base_url) "
  59. "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)"
  60. )
  61. await self.db.execute(q, *self._values)
  62. async def update(self) -> None:
  63. q = (
  64. "UPDATE puppet SET name=$2, username=$3, photo_id=$4, photo_mxc=$5, name_set=$6,"
  65. " avatar_set=$7, is_registered=$8, custom_mxid=$9, access_token=$10,"
  66. " next_batch=$11, base_url=$12 "
  67. "WHERE pk=$1"
  68. )
  69. await self.db.execute(q, *self._values)
  70. @classmethod
  71. def _from_row(cls, row: asyncpg.Record) -> Puppet:
  72. data = {**row}
  73. base_url_str = data.pop("base_url")
  74. base_url = URL(base_url_str) if base_url_str is not None else None
  75. return cls(base_url=base_url, **data)
  76. @classmethod
  77. async def get_by_pk(cls, pk: int) -> Puppet | None:
  78. q = (
  79. "SELECT pk, name, username, photo_id, photo_mxc, name_set, avatar_set, is_registered,"
  80. " custom_mxid, access_token, next_batch, base_url "
  81. "FROM puppet WHERE pk=$1"
  82. )
  83. row = await cls.db.fetchrow(q, pk)
  84. if not row:
  85. return None
  86. return cls._from_row(row)
  87. @classmethod
  88. async def get_by_custom_mxid(cls, mxid: UserID) -> Puppet | None:
  89. q = (
  90. "SELECT pk, name, username, photo_id, photo_mxc, name_set, avatar_set, is_registered,"
  91. " custom_mxid, access_token, next_batch, base_url "
  92. "FROM puppet WHERE custom_mxid=$1"
  93. )
  94. row = await cls.db.fetchrow(q, mxid)
  95. if not row:
  96. return None
  97. return cls._from_row(row)
  98. @classmethod
  99. async def all_with_custom_mxid(cls) -> list[Puppet]:
  100. q = (
  101. "SELECT pk, name, username, photo_id, photo_mxc, name_set, avatar_set, is_registered,"
  102. " custom_mxid, access_token, next_batch, base_url "
  103. "FROM puppet WHERE custom_mxid IS NOT NULL"
  104. )
  105. rows = await cls.db.fetch(q)
  106. return [cls._from_row(row) for row in rows]