portal.py 4.1 KB

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