portal.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # mautrix-signal - A Matrix-Signal 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, Union, TYPE_CHECKING
  17. from uuid import UUID
  18. from attr import dataclass
  19. import asyncpg
  20. from mausignald.types import Address, GroupID
  21. from mautrix.types import RoomID, ContentURI
  22. from mautrix.util.async_db import Database
  23. from ..util import id_to_str
  24. fake_db = Database("") if TYPE_CHECKING else None
  25. @dataclass
  26. class Portal:
  27. db: ClassVar[Database] = fake_db
  28. chat_id: Union[GroupID, Address]
  29. receiver: str
  30. mxid: Optional[RoomID]
  31. name: Optional[str]
  32. avatar_hash: Optional[str]
  33. avatar_url: Optional[ContentURI]
  34. name_set: bool
  35. avatar_set: bool
  36. revision: int
  37. encrypted: bool
  38. @property
  39. def chat_id_str(self) -> str:
  40. return id_to_str(self.chat_id)
  41. async def insert(self) -> None:
  42. q = ("INSERT INTO portal (chat_id, receiver, mxid, name, avatar_hash, avatar_url, "
  43. " name_set, avatar_set, revision, encrypted) "
  44. "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)")
  45. await self.db.execute(q, self.chat_id_str, self.receiver, self.mxid, self.name,
  46. self.avatar_hash, self.avatar_url, self.encrypted)
  47. async def update(self) -> None:
  48. q = ("UPDATE portal SET mxid=$3, name=$4, avatar_hash=$5, avatar_url=$6, "
  49. " name_set=$7, avatar_set=$8, revision=$9, encrypted=$10 "
  50. "WHERE chat_id=$1 AND receiver=$2")
  51. await self.db.execute(q, self.chat_id_str, self.receiver, self.mxid, self.name,
  52. self.avatar_hash, self.avatar_url, self.name_set, self.avatar_set,
  53. self.revision, self.encrypted)
  54. @classmethod
  55. def _from_row(cls, row: asyncpg.Record) -> 'Portal':
  56. data = {**row}
  57. chat_id = data.pop("chat_id")
  58. if data["receiver"]:
  59. chat_id = Address.parse(chat_id)
  60. return cls(chat_id=chat_id, **data)
  61. @classmethod
  62. async def get_by_mxid(cls, mxid: RoomID) -> Optional['Portal']:
  63. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, name_set, avatar_set,"
  64. " revision, encrypted "
  65. "FROM portal WHERE mxid=$1")
  66. row = await cls.db.fetchrow(q, mxid)
  67. if not row:
  68. return None
  69. return cls._from_row(row)
  70. @classmethod
  71. async def get_by_chat_id(cls, chat_id: Union[GroupID, Address], receiver: str = ""
  72. ) -> Optional['Portal']:
  73. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, name_set, avatar_set,"
  74. " revision, encrypted "
  75. "FROM portal WHERE chat_id=$1 AND receiver=$2")
  76. row = await cls.db.fetchrow(q, id_to_str(chat_id), receiver)
  77. if not row:
  78. return None
  79. return cls._from_row(row)
  80. @classmethod
  81. async def find_private_chats_of(cls, receiver: str) -> List['Portal']:
  82. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, name_set, avatar_set,"
  83. " revision, encrypted "
  84. "FROM portal WHERE receiver=$1")
  85. rows = await cls.db.fetch(q, receiver)
  86. return [cls._from_row(row) for row in rows]
  87. @classmethod
  88. async def find_private_chats_with(cls, other_user: Address) -> List['Portal']:
  89. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, name_set, avatar_set,"
  90. " revision, encrypted "
  91. "FROM portal WHERE chat_id=$1 AND receiver<>''")
  92. rows = await cls.db.fetch(q, other_user.best_identifier)
  93. return [cls._from_row(row) for row in rows]
  94. @classmethod
  95. async def all_with_room(cls) -> List['Portal']:
  96. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, name_set, avatar_set,"
  97. " revision, encrypted "
  98. "FROM portal WHERE mxid IS NOT NULL")
  99. rows = await cls.db.fetch(q)
  100. return [cls._from_row(row) for row in rows]