user.py 3.2 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 __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. @property
  34. def _values(self):
  35. return (
  36. self.mxid,
  37. self.igpk,
  38. self.state.json() if self.state else None,
  39. self.notice_room,
  40. self.seq_id,
  41. self.snapshot_at_ms,
  42. )
  43. async def insert(self) -> None:
  44. q = """
  45. INSERT INTO "user" (mxid, igpk, state, notice_room, seq_id, snapshot_at_ms)
  46. VALUES ($1, $2, $3, $4, $5, $6)
  47. """
  48. await self.db.execute(q, *self._values)
  49. async def update(self) -> None:
  50. q = """
  51. UPDATE "user" SET igpk=$2, state=$3, notice_room=$4, seq_id=$5, snapshot_at_ms=$6
  52. WHERE mxid=$1
  53. """
  54. await self.db.execute(q, *self._values)
  55. async def save_seq_id(self) -> None:
  56. q = 'UPDATE "user" SET seq_id=$2, snapshot_at_ms=$3 WHERE mxid=$1'
  57. await self.db.execute(q, self.mxid, self.seq_id, self.snapshot_at_ms)
  58. @classmethod
  59. def _from_row(cls, row: asyncpg.Record) -> User:
  60. data = {**row}
  61. state_str = data.pop("state")
  62. return cls(state=AndroidState.parse_json(state_str) if state_str else None, **data)
  63. _columns = "mxid, igpk, state, notice_room, seq_id, snapshot_at_ms"
  64. @classmethod
  65. async def get_by_mxid(cls, mxid: UserID) -> User | None:
  66. q = f'SELECT {cls._columns} FROM "user" WHERE mxid=$1'
  67. row = await cls.db.fetchrow(q, mxid)
  68. if not row:
  69. return None
  70. return cls._from_row(row)
  71. @classmethod
  72. async def get_by_igpk(cls, igpk: int) -> User | None:
  73. q = f'SELECT {cls._columns} FROM "user" WHERE igpk=$1'
  74. row = await cls.db.fetchrow(q, igpk)
  75. if not row:
  76. return None
  77. return cls._from_row(row)
  78. @classmethod
  79. async def all_logged_in(cls) -> list[User]:
  80. q = f'SELECT {cls._columns} FROM "user" WHERE igpk IS NOT NULL'
  81. rows = await cls.db.fetch(q)
  82. return [cls._from_row(row) for row in rows]