portal.py 3.5 KB

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