portal.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. expiration_time: Optional[int]
  39. @property
  40. def chat_id_str(self) -> str:
  41. return id_to_str(self.chat_id)
  42. async def insert(self) -> None:
  43. q = ("INSERT INTO portal (chat_id, receiver, mxid, name, avatar_hash, avatar_url, "
  44. " name_set, avatar_set, revision, encrypted, relay_user_id, "
  45. " expiration_time) "
  46. "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)")
  47. await self.db.execute(q, self.chat_id_str, self.receiver, self.mxid, self.name,
  48. self.avatar_hash, self.avatar_url, self.name_set, self.avatar_set,
  49. self.revision, self.encrypted, self.relay_user_id,
  50. self.expiration_time)
  51. async def update(self) -> None:
  52. q = ("UPDATE portal SET mxid=$1, name=$2, avatar_hash=$3, avatar_url=$4, name_set=$5, "
  53. " avatar_set=$6, revision=$7, encrypted=$8, relay_user_id=$9, "
  54. " expiration_time=$10"
  55. "WHERE chat_id=$11 AND receiver=$12")
  56. await self.db.execute(q, self.mxid, self.name, self.avatar_hash, self.avatar_url,
  57. self.name_set, self.avatar_set, self.revision, self.encrypted,
  58. self.relay_user_id, self.expiration_time, self.chat_id_str,
  59. self.receiver)
  60. @classmethod
  61. def _from_row(cls, row: asyncpg.Record) -> 'Portal':
  62. data = {**row}
  63. chat_id = data.pop("chat_id")
  64. if data["receiver"]:
  65. chat_id = Address.parse(chat_id)
  66. return cls(chat_id=chat_id, **data)
  67. @classmethod
  68. async def get_by_mxid(cls, mxid: RoomID) -> Optional['Portal']:
  69. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, name_set, avatar_set,"
  70. " revision, encrypted, relay_user_id, expiration_time "
  71. "FROM portal WHERE mxid=$1")
  72. row = await cls.db.fetchrow(q, mxid)
  73. if not row:
  74. return None
  75. return cls._from_row(row)
  76. @classmethod
  77. async def get_by_chat_id(cls, chat_id: Union[GroupID, Address], receiver: str = ""
  78. ) -> Optional['Portal']:
  79. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, name_set, avatar_set,"
  80. " revision, encrypted, relay_user_id, expiration_time "
  81. "FROM portal WHERE chat_id=$1 AND receiver=$2")
  82. row = await cls.db.fetchrow(q, id_to_str(chat_id), receiver)
  83. if not row:
  84. return None
  85. return cls._from_row(row)
  86. @classmethod
  87. async def find_private_chats_of(cls, receiver: str) -> List['Portal']:
  88. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, name_set, avatar_set,"
  89. " revision, encrypted, relay_user_id, expiration_time "
  90. "FROM portal WHERE receiver=$1")
  91. rows = await cls.db.fetch(q, receiver)
  92. return [cls._from_row(row) for row in rows]
  93. @classmethod
  94. async def find_private_chats_with(cls, other_user: Address) -> List['Portal']:
  95. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, name_set, avatar_set,"
  96. " revision, encrypted, relay_user_id, expiration_time "
  97. "FROM portal WHERE chat_id=$1 AND receiver<>''")
  98. rows = await cls.db.fetch(q, other_user.best_identifier)
  99. return [cls._from_row(row) for row in rows]
  100. @classmethod
  101. async def all_with_room(cls) -> List['Portal']:
  102. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, name_set, avatar_set,"
  103. " revision, encrypted, relay_user_id, expiration_time "
  104. "FROM portal WHERE mxid IS NOT NULL")
  105. rows = await cls.db.fetch(q)
  106. return [cls._from_row(row) for row in rows]