2022-02-25 02:22:50 -05:00
|
|
|
# matrix-appservice-kakaotalk - A Matrix-KakaoTalk puppeting bridge.
|
|
|
|
# Copyright (C) 2022 Tulir Asokan, Andrew Ferrazzutti
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING, ClassVar
|
|
|
|
|
|
|
|
from asyncpg import Record
|
2022-03-11 03:43:00 -05:00
|
|
|
from attr import dataclass, field
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
from mautrix.types import ContentURI, RoomID, UserID
|
|
|
|
from mautrix.util.async_db import Database
|
|
|
|
|
2022-05-05 03:00:41 -04:00
|
|
|
from ..kt.types.bson import Long, to_optional_long
|
2022-02-25 02:22:50 -05:00
|
|
|
from ..kt.types.channel.channel_type import ChannelType
|
|
|
|
|
|
|
|
fake_db = Database.create("") if TYPE_CHECKING else None
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Portal:
|
|
|
|
db: ClassVar[Database] = fake_db
|
|
|
|
|
2022-03-11 03:43:00 -05:00
|
|
|
ktid: Long = field(converter=Long)
|
|
|
|
kt_receiver: Long = field(converter=Long)
|
2022-02-25 02:22:50 -05:00
|
|
|
kt_type: ChannelType
|
|
|
|
mxid: RoomID | None
|
|
|
|
name: str | None
|
2022-04-13 13:02:55 -04:00
|
|
|
description: str | None
|
2022-02-25 02:22:50 -05:00
|
|
|
photo_id: str | None
|
|
|
|
avatar_url: ContentURI | None
|
|
|
|
encrypted: bool
|
|
|
|
name_set: bool
|
2022-04-13 13:02:55 -04:00
|
|
|
topic_set: bool
|
2022-02-25 02:22:50 -05:00
|
|
|
avatar_set: bool
|
2022-05-16 02:23:52 -04:00
|
|
|
link_id: Long | None = field(converter=to_optional_long)
|
|
|
|
link_url: str | None
|
2022-05-05 03:00:41 -04:00
|
|
|
fully_read_kt_chat: Long | None = field(converter=to_optional_long)
|
2022-02-25 02:22:50 -05:00
|
|
|
relay_user_id: UserID | None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _from_row(cls, row: Record) -> Portal:
|
2022-03-11 03:43:00 -05:00
|
|
|
return cls(**row)
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _from_optional_row(cls, row: Record | None) -> Portal | None:
|
|
|
|
return cls._from_row(row) if row is not None else None
|
|
|
|
|
2022-05-05 03:00:41 -04:00
|
|
|
_columns = (
|
|
|
|
"ktid, kt_receiver, kt_type, mxid, name, description, photo_id, avatar_url, encrypted, "
|
2022-05-16 02:23:52 -04:00
|
|
|
"name_set, avatar_set, link_id, link_url, fully_read_kt_chat, relay_user_id"
|
2022-05-05 03:00:41 -04:00
|
|
|
)
|
|
|
|
|
2022-02-25 02:22:50 -05:00
|
|
|
@classmethod
|
2022-03-11 03:43:00 -05:00
|
|
|
async def get_by_ktid(cls, ktid: int, kt_receiver: int) -> Portal | None:
|
2022-05-05 03:00:41 -04:00
|
|
|
q = f"SELECT {cls._columns} FROM portal WHERE ktid=$1 AND kt_receiver=$2"
|
2022-03-11 03:43:00 -05:00
|
|
|
row = await cls.db.fetchrow(q, ktid, kt_receiver)
|
2022-02-25 02:22:50 -05:00
|
|
|
return cls._from_optional_row(row)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def get_by_mxid(cls, mxid: RoomID) -> Portal | None:
|
2022-05-05 03:00:41 -04:00
|
|
|
q = f"SELECT {cls._columns} FROM portal WHERE mxid=$1"
|
2022-02-25 02:22:50 -05:00
|
|
|
row = await cls.db.fetchrow(q, mxid)
|
|
|
|
return cls._from_optional_row(row)
|
|
|
|
|
|
|
|
@classmethod
|
2022-03-11 03:43:00 -05:00
|
|
|
async def get_all_by_receiver(cls, kt_receiver: int) -> list[Portal]:
|
2022-05-05 03:00:41 -04:00
|
|
|
q = f"SELECT {cls._columns} FROM portal WHERE kt_receiver=$1"
|
2022-03-11 03:43:00 -05:00
|
|
|
rows = await cls.db.fetch(q, kt_receiver)
|
2022-02-25 02:22:50 -05:00
|
|
|
return [cls._from_row(row) for row in rows if row]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def all(cls) -> list[Portal]:
|
2022-05-05 03:00:41 -04:00
|
|
|
q = f"SELECT {cls._columns} FROM portal"
|
2022-02-25 02:22:50 -05:00
|
|
|
rows = await cls.db.fetch(q)
|
|
|
|
return [cls._from_row(row) for row in rows if row]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _values(self):
|
|
|
|
return (
|
2022-03-11 03:43:00 -05:00
|
|
|
self.ktid,
|
|
|
|
self.kt_receiver,
|
2022-02-25 02:22:50 -05:00
|
|
|
self.kt_type,
|
|
|
|
self.mxid,
|
|
|
|
self.name,
|
2022-04-13 13:02:55 -04:00
|
|
|
self.description,
|
2022-02-25 02:22:50 -05:00
|
|
|
self.photo_id,
|
|
|
|
self.avatar_url,
|
|
|
|
self.encrypted,
|
|
|
|
self.name_set,
|
|
|
|
self.avatar_set,
|
2022-05-16 02:23:52 -04:00
|
|
|
self.link_id,
|
|
|
|
self.link_url,
|
2022-05-05 03:00:41 -04:00
|
|
|
self.fully_read_kt_chat,
|
2022-02-25 02:22:50 -05:00
|
|
|
self.relay_user_id,
|
|
|
|
)
|
|
|
|
|
2022-05-05 03:00:41 -04:00
|
|
|
_args = "$" + ", $".join(str(i + 1) for i in range(_columns.count(',') + 1))
|
2022-02-25 02:22:50 -05:00
|
|
|
async def insert(self) -> None:
|
2022-05-05 03:00:41 -04:00
|
|
|
q = f"INSERT INTO portal ({self._columns}) VALUES ({self._args})"
|
2022-02-25 02:22:50 -05:00
|
|
|
await self.db.execute(q, *self._values)
|
|
|
|
|
|
|
|
async def delete(self) -> None:
|
|
|
|
q = "DELETE FROM portal WHERE ktid=$1 AND kt_receiver=$2"
|
2022-03-11 03:43:00 -05:00
|
|
|
await self.db.execute(q, self.ktid, self.kt_receiver)
|
2022-02-25 02:22:50 -05:00
|
|
|
|
2022-05-05 03:00:41 -04:00
|
|
|
_nonkey_column_asgns = ", ".join(
|
|
|
|
map(lambda t: (i:=t[0], word:=t[1], f"{word}=${i + 3}")[-1],
|
|
|
|
enumerate(_columns.split(", ")[2:])
|
|
|
|
)
|
|
|
|
)
|
2022-02-25 02:22:50 -05:00
|
|
|
async def save(self) -> None:
|
2022-05-05 03:00:41 -04:00
|
|
|
q = f"UPDATE portal SET {self._nonkey_column_asgns} WHERE ktid=$1 AND kt_receiver=$2"
|
2022-02-25 02:22:50 -05:00
|
|
|
await self.db.execute(q, *self._values)
|