portal.py 3.5 KB

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