puppet.py 4.1 KB

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