puppet.py 3.9 KB

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