puppet.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. is_registered: bool
  32. custom_mxid: Optional[UserID]
  33. access_token: Optional[str]
  34. next_batch: Optional[SyncToken]
  35. base_url: Optional[URL]
  36. async def insert(self) -> None:
  37. q = ("INSERT INTO puppet (pk, name, username, photo_id, photo_mxc, is_registered,"
  38. " custom_mxid, access_token, next_batch, base_url) "
  39. "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)")
  40. await self.db.execute(q, self.pk, self.name, self.username, self.photo_id, self.photo_mxc,
  41. self.is_registered, self.custom_mxid, self.access_token,
  42. self.next_batch, str(self.base_url) if self.base_url else None)
  43. async def update(self) -> None:
  44. q = ("UPDATE puppet SET name=$2, username=$3, photo_id=$4, photo_mxc=$5, is_registered=$6,"
  45. " custom_mxid=$7, access_token=$8, next_batch=$9, base_url=$10 "
  46. "WHERE pk=$1")
  47. await self.db.execute(q, self.pk, self.name, self.username, self.photo_id, self.photo_mxc,
  48. self.is_registered, self.custom_mxid, self.access_token,
  49. self.next_batch, str(self.base_url) if self.base_url else None)
  50. @classmethod
  51. def _from_row(cls, row: asyncpg.Record) -> 'Puppet':
  52. data = {**row}
  53. base_url_str = data.pop("base_url")
  54. base_url = URL(base_url_str) if base_url_str is not None else None
  55. return cls(base_url=base_url, **data)
  56. @classmethod
  57. async def get_by_pk(cls, pk: int) -> Optional['Puppet']:
  58. q = ("SELECT pk, name, username, photo_id, photo_mxc, is_registered,"
  59. " custom_mxid, access_token, next_batch, base_url "
  60. "FROM puppet WHERE igpk=$1")
  61. row = await cls.db.fetchrow(q, pk)
  62. if not row:
  63. return None
  64. return cls._from_row(row)
  65. @classmethod
  66. async def get_by_custom_mxid(cls, mxid: UserID) -> Optional['Puppet']:
  67. q = ("SELECT pk, name, username, photo_id, photo_mxc, is_registered,"
  68. " custom_mxid, access_token, next_batch, base_url "
  69. "FROM puppet WHERE custom_mxid=$1")
  70. row = await cls.db.fetchrow(q, mxid)
  71. if not row:
  72. return None
  73. return cls._from_row(row)
  74. @classmethod
  75. async def all_with_custom_mxid(cls) -> List['Puppet']:
  76. q = ("SELECT pk, name, username, photo_id, photo_mxc, is_registered,"
  77. " custom_mxid, access_token, next_batch, base_url "
  78. "FROM puppet WHERE custom_mxid IS NOT NULL")
  79. rows = await cls.db.fetch(q)
  80. return [cls._from_row(row) for row in rows]