portal.py 4.9 KB

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