user.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. import asyncpg
  19. from mauigpapi.state import AndroidState
  20. from mautrix.types import UserID, RoomID
  21. from mautrix.util.async_db import Database
  22. fake_db = Database("") if TYPE_CHECKING else None
  23. @dataclass
  24. class User:
  25. db: ClassVar[Database] = fake_db
  26. mxid: UserID
  27. igpk: Optional[int]
  28. state: Optional[AndroidState]
  29. notice_room: Optional[RoomID]
  30. async def insert(self) -> None:
  31. q = ('INSERT INTO "user" (mxid, igpk, state, notice_room) '
  32. 'VALUES ($1, $2, $3, $4)')
  33. await self.db.execute(q, self.mxid, self.igpk,
  34. self.state.serialize() if self.state else None, self.notice_room)
  35. async def update(self) -> None:
  36. await self.db.execute('UPDATE "user" SET igpk=$2, state=$3, notice_room=$4 '
  37. 'WHERE mxid=$1', self.mxid, self.igpk,
  38. self.state.serialize() if self.state else None, self.notice_room)
  39. @classmethod
  40. def _from_row(cls, row: asyncpg.Record) -> 'User':
  41. data = {**row}
  42. state_str = data.pop("state")
  43. return cls(state=AndroidState.parse_json(state_str) if state_str else None, **data)
  44. @classmethod
  45. async def get_by_mxid(cls, mxid: UserID) -> Optional['User']:
  46. q = 'SELECT mxid, igpk, state, notice_room FROM "user" WHERE mxid=$1'
  47. row = await cls.db.fetchrow(q, mxid)
  48. if not row:
  49. return None
  50. return cls._from_row(row)
  51. @classmethod
  52. async def get_by_igpk(cls, igpk: int) -> Optional['User']:
  53. q = 'SELECT mxid, igpk, state, notice_room FROM "user" WHERE igpk=$1'
  54. row = await cls.db.fetchrow(q, igpk)
  55. if not row:
  56. return None
  57. return cls._from_row(row)
  58. @classmethod
  59. async def all_logged_in(cls) -> List['User']:
  60. q = ("SELECT mxid, igp, state, notice_room "
  61. 'FROM "user" WHERE igpk IS NOT NULL AND state IS NOT NULL')
  62. rows = await cls.db.fetch(q)
  63. return [cls._from_row(row) for row in rows]