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
|
|
|
|
|
2022-07-14 01:41:24 -04:00
|
|
|
from typing import TYPE_CHECKING, ClassVar, List, Optional
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
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 EventID, RoomID
|
2022-03-24 19:32:46 -04:00
|
|
|
from mautrix.util.async_db import Database, Scheme
|
2022-02-25 02:22:50 -05:00
|
|
|
|
2022-04-28 01:50:47 -04:00
|
|
|
from ..kt.types.bson import Long, to_optional_long
|
2022-03-09 02:25:28 -05:00
|
|
|
|
2022-02-25 02:22:50 -05:00
|
|
|
fake_db = Database.create("") if TYPE_CHECKING else None
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Message:
|
|
|
|
db: ClassVar[Database] = fake_db
|
|
|
|
|
|
|
|
mxid: EventID
|
|
|
|
mx_room: RoomID
|
2022-07-14 01:41:24 -04:00
|
|
|
ktid: Optional[Long] = field(converter=to_optional_long)
|
2022-02-25 02:22:50 -05:00
|
|
|
index: int
|
2022-05-05 03:00:41 -04:00
|
|
|
kt_channel: Long = field(converter=Long)
|
2022-03-11 03:43:00 -05:00
|
|
|
kt_receiver: Long = field(converter=Long)
|
2022-02-25 02:22:50 -05:00
|
|
|
timestamp: int
|
|
|
|
|
|
|
|
@classmethod
|
2022-03-11 03:43:00 -05:00
|
|
|
def _from_row(cls, row: Record) -> Message:
|
|
|
|
return cls(**row)
|
2022-03-09 02:25:28 -05:00
|
|
|
|
|
|
|
@classmethod
|
2022-07-14 01:41:24 -04:00
|
|
|
def _from_optional_row(cls, row: Optional[Record]) -> Optional[Message]:
|
2022-03-09 02:25:28 -05:00
|
|
|
return cls._from_row(row) if row is not None else None
|
2022-02-25 02:22:50 -05:00
|
|
|
|
2022-05-05 03:00:41 -04:00
|
|
|
columns = 'mxid, mx_room, ktid, "index", kt_channel, kt_receiver, timestamp'
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
@classmethod
|
2022-07-14 01:41:24 -04:00
|
|
|
async def get_all_by_ktid(cls, ktid: int, kt_channel: int, kt_receiver: int) -> List[Message]:
|
2022-05-05 03:00:41 -04:00
|
|
|
q = f"SELECT {cls.columns} FROM message WHERE ktid=$1 AND kt_channel=$2 AND kt_receiver=$3"
|
|
|
|
rows = await cls.db.fetch(q, ktid, kt_channel, kt_receiver)
|
2022-03-10 02:46:24 -05:00
|
|
|
return [cls._from_row(row) for row in rows if row]
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
@classmethod
|
2022-07-14 01:41:24 -04:00
|
|
|
async def get_by_ktid(cls, ktid: int, kt_channel: int, kt_receiver: int, index: int = 0) -> Optional[Message]:
|
2022-05-05 03:00:41 -04:00
|
|
|
q = f'SELECT {cls.columns} FROM message WHERE ktid=$1 AND kt_channel=$2 AND kt_receiver=$3 AND "index"=$4'
|
|
|
|
row = await cls.db.fetchrow(q, ktid, kt_channel, kt_receiver, index)
|
2022-03-10 02:46:24 -05:00
|
|
|
return cls._from_optional_row(row)
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def delete_all_by_room(cls, room_id: RoomID) -> None:
|
|
|
|
await cls.db.execute("DELETE FROM message WHERE mx_room=$1", room_id)
|
|
|
|
|
|
|
|
@classmethod
|
2022-07-14 01:41:24 -04:00
|
|
|
async def get_by_mxid(cls, mxid: EventID, mx_room: RoomID) -> Optional[Message]:
|
2022-02-25 02:22:50 -05:00
|
|
|
q = f"SELECT {cls.columns} FROM message WHERE mxid=$1 AND mx_room=$2"
|
|
|
|
row = await cls.db.fetchrow(q, mxid, mx_room)
|
2022-03-10 02:46:24 -05:00
|
|
|
return cls._from_optional_row(row)
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
@classmethod
|
2022-07-14 01:41:24 -04:00
|
|
|
async def get_most_recent(cls, kt_channel: int, kt_receiver: int) -> Optional[Message]:
|
2022-02-25 02:22:50 -05:00
|
|
|
q = (
|
|
|
|
f"SELECT {cls.columns} "
|
2022-05-05 03:00:41 -04:00
|
|
|
"FROM message WHERE kt_channel=$1 AND kt_receiver=$2 AND ktid IS NOT NULL "
|
|
|
|
"ORDER BY ktid DESC LIMIT 1"
|
2022-02-25 02:22:50 -05:00
|
|
|
)
|
2022-05-05 03:00:41 -04:00
|
|
|
row = await cls.db.fetchrow(q, kt_channel, kt_receiver)
|
2022-03-10 02:46:24 -05:00
|
|
|
return cls._from_optional_row(row)
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def get_closest_before(
|
2022-05-05 03:00:41 -04:00
|
|
|
cls, kt_channel: int, kt_receiver: int, ktid: int
|
2022-07-14 01:41:24 -04:00
|
|
|
) -> Optional[Message]:
|
2022-02-25 02:22:50 -05:00
|
|
|
q = (
|
|
|
|
f"SELECT {cls.columns} "
|
2022-05-05 03:00:41 -04:00
|
|
|
"FROM message WHERE kt_channel=$1 AND kt_receiver=$2 AND ktid<=$3 "
|
2022-04-11 04:26:21 -04:00
|
|
|
"ORDER BY ktid DESC LIMIT 1"
|
2022-02-25 02:22:50 -05:00
|
|
|
)
|
2022-05-05 03:00:41 -04:00
|
|
|
row = await cls.db.fetchrow(q, kt_channel, kt_receiver, ktid)
|
2022-03-10 02:46:24 -05:00
|
|
|
return cls._from_optional_row(row)
|
2022-02-25 02:22:50 -05:00
|
|
|
|
2022-05-05 03:00:41 -04:00
|
|
|
@classmethod
|
2022-07-14 01:41:24 -04:00
|
|
|
async def get_all_since(cls, kt_channel: int, kt_receiver: int, since_ktid: Optional[Long]) -> List[Message]:
|
2022-05-05 03:00:41 -04:00
|
|
|
q = (
|
|
|
|
f"SELECT {cls.columns} "
|
|
|
|
"FROM message WHERE kt_channel=$1 AND kt_receiver=$2 AND ktid>=$3 "
|
|
|
|
"ORDER BY ktid"
|
|
|
|
)
|
|
|
|
rows = await cls.db.fetch(q, kt_channel, kt_receiver, since_ktid or 0)
|
|
|
|
return [cls._from_row(row) for row in rows if row]
|
|
|
|
|
2022-02-25 02:22:50 -05:00
|
|
|
_insert_query = (
|
2022-05-05 03:00:41 -04:00
|
|
|
'INSERT INTO message (mxid, mx_room, ktid, "index", kt_channel, kt_receiver, '
|
2022-03-09 02:25:28 -05:00
|
|
|
" timestamp) "
|
|
|
|
"VALUES ($1, $2, $3, $4, $5, $6, $7)"
|
2022-02-25 02:22:50 -05:00
|
|
|
)
|
|
|
|
|
2022-03-24 19:32:46 -04:00
|
|
|
@classmethod
|
|
|
|
async def bulk_create(
|
|
|
|
cls,
|
2022-03-26 03:37:53 -04:00
|
|
|
ktid: Long,
|
2022-05-05 03:00:41 -04:00
|
|
|
kt_channel: Long,
|
2022-03-26 03:37:53 -04:00
|
|
|
kt_receiver: Long,
|
2022-07-14 01:41:24 -04:00
|
|
|
event_ids: List[EventID],
|
2022-03-24 19:32:46 -04:00
|
|
|
timestamp: int,
|
|
|
|
mx_room: RoomID,
|
2022-07-14 01:41:24 -04:00
|
|
|
) -> List[Message]:
|
2022-03-24 19:32:46 -04:00
|
|
|
if not event_ids:
|
2022-04-02 23:16:53 -04:00
|
|
|
return []
|
2022-03-24 19:32:46 -04:00
|
|
|
columns = [col.strip('"') for col in cls.columns.split(", ")]
|
|
|
|
records = [
|
2022-05-05 03:00:41 -04:00
|
|
|
(mxid, mx_room, ktid, index, kt_channel, kt_receiver, timestamp)
|
2022-03-24 19:32:46 -04:00
|
|
|
for index, mxid in enumerate(event_ids)
|
|
|
|
]
|
|
|
|
async with cls.db.acquire() as conn, conn.transaction():
|
|
|
|
if cls.db.scheme == Scheme.POSTGRES:
|
|
|
|
await conn.copy_records_to_table("message", records=records, columns=columns)
|
|
|
|
else:
|
|
|
|
await conn.executemany(cls._insert_query, records)
|
2022-04-02 23:16:53 -04:00
|
|
|
return [Message(*record) for record in records]
|
2022-03-24 19:32:46 -04:00
|
|
|
|
|
|
|
|
2022-02-25 02:22:50 -05:00
|
|
|
async def insert(self) -> None:
|
|
|
|
q = self._insert_query
|
|
|
|
await self.db.execute(
|
|
|
|
q,
|
|
|
|
self.mxid,
|
|
|
|
self.mx_room,
|
2022-03-11 03:43:00 -05:00
|
|
|
self.ktid,
|
2022-02-25 02:22:50 -05:00
|
|
|
self.index,
|
2022-05-05 03:00:41 -04:00
|
|
|
self.kt_channel,
|
2022-03-11 03:43:00 -05:00
|
|
|
self.kt_receiver,
|
2022-02-25 02:22:50 -05:00
|
|
|
self.timestamp,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def delete(self) -> None:
|
|
|
|
q = 'DELETE FROM message WHERE ktid=$1 AND kt_receiver=$2 AND "index"=$3'
|
2022-03-11 03:43:00 -05:00
|
|
|
await self.db.execute(q, self.ktid, self.kt_receiver, self.index)
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
async def update(self) -> None:
|
|
|
|
q = "UPDATE message SET ktid=$1, timestamp=$2 WHERE mxid=$3 AND mx_room=$4"
|
2022-03-11 03:43:00 -05:00
|
|
|
await self.db.execute(q, self.ktid, self.timestamp, self.mxid, self.mx_room)
|