puppet.py 4.1 KB

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