user.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 __future__ import annotations
  17. from typing import TYPE_CHECKING, ClassVar
  18. from attr import dataclass
  19. import asyncpg
  20. from mauigpapi.state import AndroidState
  21. from mautrix.types import RoomID, UserID
  22. from mautrix.util.async_db import Database
  23. fake_db = Database.create("") if TYPE_CHECKING else None
  24. @dataclass
  25. class User:
  26. db: ClassVar[Database] = fake_db
  27. mxid: UserID
  28. igpk: int | None
  29. state: AndroidState | None
  30. notice_room: RoomID | None
  31. seq_id: int | None
  32. snapshot_at_ms: int | None
  33. oldest_cursor: str | None
  34. total_backfilled_portals: int | None
  35. thread_sync_completed: bool
  36. @property
  37. def _values(self):
  38. return (
  39. self.mxid,
  40. self.igpk,
  41. self.state.json() if self.state else None,
  42. self.notice_room,
  43. self.seq_id,
  44. self.snapshot_at_ms,
  45. self.oldest_cursor,
  46. self.total_backfilled_portals,
  47. self.thread_sync_completed,
  48. )
  49. _columns = ",".join(
  50. (
  51. "mxid",
  52. "igpk",
  53. "state",
  54. "notice_room",
  55. "seq_id",
  56. "snapshot_at_ms",
  57. "oldest_cursor",
  58. "total_backfilled_portals",
  59. "thread_sync_completed",
  60. )
  61. )
  62. async def insert(self) -> None:
  63. q = f"""
  64. INSERT INTO "user" ({self._columns})
  65. VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
  66. """
  67. await self.db.execute(q, *self._values)
  68. async def update(self) -> None:
  69. q = """
  70. UPDATE "user"
  71. SET igpk=$2, state=$3, notice_room=$4, seq_id=$5, snapshot_at_ms=$6,
  72. oldest_cursor=$7, total_backfilled_portals=$8, thread_sync_completed=$9
  73. WHERE mxid=$1
  74. """
  75. await self.db.execute(q, *self._values)
  76. async def save_seq_id(self) -> None:
  77. q = 'UPDATE "user" SET seq_id=$2, snapshot_at_ms=$3 WHERE mxid=$1'
  78. await self.db.execute(q, self.mxid, self.seq_id, self.snapshot_at_ms)
  79. @classmethod
  80. def _from_row(cls, row: asyncpg.Record) -> User:
  81. data = {**row}
  82. state_str = data.pop("state")
  83. return cls(state=AndroidState.parse_json(state_str) if state_str else None, **data)
  84. @classmethod
  85. async def get_by_mxid(cls, mxid: UserID) -> User | None:
  86. q = f'SELECT {cls._columns} FROM "user" WHERE mxid=$1'
  87. row = await cls.db.fetchrow(q, mxid)
  88. if not row:
  89. return None
  90. return cls._from_row(row)
  91. @classmethod
  92. async def get_by_igpk(cls, igpk: int) -> User | None:
  93. q = f'SELECT {cls._columns} FROM "user" WHERE igpk=$1'
  94. row = await cls.db.fetchrow(q, igpk)
  95. if not row:
  96. return None
  97. return cls._from_row(row)
  98. @classmethod
  99. async def all_logged_in(cls) -> list[User]:
  100. q = f'SELECT {cls._columns} FROM "user" WHERE igpk IS NOT NULL'
  101. rows = await cls.db.fetch(q)
  102. return [cls._from_row(row) for row in rows]