user.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. async def insert(self) -> None:
  32. q = 'INSERT INTO "user" (mxid, igpk, state, notice_room) VALUES ($1, $2, $3, $4)'
  33. await self.db.execute(
  34. q, self.mxid, self.igpk, self.state.json() if self.state else None, self.notice_room
  35. )
  36. async def update(self) -> None:
  37. q = 'UPDATE "user" SET igpk=$2, state=$3, notice_room=$4 WHERE mxid=$1'
  38. await self.db.execute(
  39. q, self.mxid, self.igpk, self.state.json() if self.state else None, self.notice_room
  40. )
  41. @classmethod
  42. def _from_row(cls, row: asyncpg.Record) -> User:
  43. data = {**row}
  44. state_str = data.pop("state")
  45. return cls(state=AndroidState.parse_json(state_str) if state_str else None, **data)
  46. @classmethod
  47. async def get_by_mxid(cls, mxid: UserID) -> User | None:
  48. q = 'SELECT mxid, igpk, state, notice_room FROM "user" WHERE mxid=$1'
  49. row = await cls.db.fetchrow(q, mxid)
  50. if not row:
  51. return None
  52. return cls._from_row(row)
  53. @classmethod
  54. async def get_by_igpk(cls, igpk: int) -> User | None:
  55. q = 'SELECT mxid, igpk, state, notice_room FROM "user" WHERE igpk=$1'
  56. row = await cls.db.fetchrow(q, igpk)
  57. if not row:
  58. return None
  59. return cls._from_row(row)
  60. @classmethod
  61. async def all_logged_in(cls) -> list[User]:
  62. q = (
  63. "SELECT mxid, igpk, state, notice_room "
  64. 'FROM "user" WHERE igpk IS NOT NULL AND state IS NOT NULL'
  65. )
  66. rows = await cls.db.fetch(q)
  67. return [cls._from_row(row) for row in rows]