portal.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 mautrix.types import RoomID, ContentURI
  21. from mautrix.util.async_db import Database
  22. fake_db = Database("") if TYPE_CHECKING else None
  23. @dataclass
  24. class Portal:
  25. db: ClassVar[Database] = fake_db
  26. chat_id: Union[UUID, str]
  27. receiver: str
  28. mxid: Optional[RoomID]
  29. name: Optional[str]
  30. avatar_hash: Optional[str]
  31. avatar_url: Optional[ContentURI]
  32. encrypted: bool
  33. async def insert(self) -> None:
  34. q = ("INSERT INTO portal (chat_id, receiver, mxid, name, avatar_hash, avatar_url, "
  35. " encrypted) "
  36. "VALUES ($1, $2, $3, $4, $5, $6, $7)")
  37. await self.db.execute(q, str(self.chat_id), self.receiver, self.mxid, self.name,
  38. self.encrypted)
  39. async def update(self) -> None:
  40. q = ("UPDATE portal SET mxid=$3, name=$4, avatar_hash=$5, avatar_url=$6, encrypted=$7 "
  41. "WHERE chat_id=$1 AND receiver=$2")
  42. await self.db.execute(q, str(self.chat_id), self.receiver, self.mxid, self.name,
  43. self.avatar_hash, self.avatar_url, self.encrypted)
  44. @classmethod
  45. def _from_row(cls, row: asyncpg.Record) -> 'Portal':
  46. data = {**row}
  47. if data["receiver"]:
  48. chat_id = UUID(data.pop("chat_id"))
  49. else:
  50. chat_id = data.pop("chat_id")
  51. return cls(chat_id=chat_id, **data)
  52. @classmethod
  53. async def get_by_mxid(cls, mxid: RoomID) -> Optional['Portal']:
  54. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, encrypted "
  55. "FROM portal WHERE mxid=$1")
  56. row = await cls.db.fetchrow(q, mxid)
  57. if not row:
  58. return None
  59. return cls._from_row(row)
  60. @classmethod
  61. async def get_by_chat_id(cls, chat_id: Union[UUID, str], receiver: str = ""
  62. ) -> Optional['Portal']:
  63. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, encrypted "
  64. "FROM portal WHERE chat_id=$1 AND receiver=$2")
  65. row = await cls.db.fetchrow(q, str(chat_id), receiver)
  66. if not row:
  67. return None
  68. return cls._from_row(row)
  69. @classmethod
  70. async def find_private_chats_of(cls, receiver: str) -> List['Portal']:
  71. q =( "SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, encrypted "
  72. "FROM portal WHERE receiver=$1")
  73. rows = await cls.db.fetch(q, receiver)
  74. return [cls._from_row(row) for row in rows]
  75. @classmethod
  76. async def find_private_chats_with(cls, other_user: UUID) -> List['Portal']:
  77. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, encrypted "
  78. "FROM portal WHERE chat_id=$1::text AND receiver<>''")
  79. rows = await cls.db.fetch(q, other_user)
  80. return [cls._from_row(row) for row in rows]
  81. @classmethod
  82. async def all_with_room(cls) -> List['Portal']:
  83. q = ("SELECT chat_id, receiver, mxid, name, avatar_hash, avatar_url, encrypted "
  84. "FROM portal WHERE mxid IS NOT NULL")
  85. rows = await cls.db.fetch(q)
  86. return [cls._from_row(row) for row in rows]