matrix-appservice-kakaotalk/matrix_appservice_kakaotalk/db/login_credential.py

69 lines
2.2 KiB
Python

# 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
from attr import dataclass
from mautrix.types import UserID
from mautrix.util.async_db import Database
fake_db = Database.create("") if TYPE_CHECKING else None
@dataclass
class LoginCredential:
db: ClassVar[Database] = fake_db
mxid: UserID
email: str
password: str
@classmethod
async def get_by_mxid(cls, mxid: UserID) -> LoginCredential | None:
q = "SELECT mxid, email, password FROM login_credential WHERE mxid=$1"
row = await cls.db.fetchrow(q, mxid)
return cls(**row) if row else None
@classmethod
async def delete_by_mxid(cls, mxid: UserID) -> None:
await cls.db.execute("DELETE FROM login_credential WHERE mxid=$1", mxid)
async def insert(self) -> None:
q = """
INSERT INTO login_credential (mxid, email, password)
VALUES ($1, $2, $3)
"""
await self.db.execute(q, self.mxid, self.email, self.password)
def get_form(self) -> dict[str, str]:
return {
"email": self.email,
"password": self.password,
}
async def delete(self) -> None:
await self.delete_by_mxid(self.mxid)
async def save(self) -> None:
q = """
UPDATE login_credential SET email=$2, password=$3
WHERE mxid=$1
"""
await self.db.execute(q, self.mxid, self.email, self.password)