This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
user_contact.py
75 lines (60 loc) · 2.79 KB
/
user_contact.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# mautrix-facebook - A Matrix-Facebook Messenger puppeting bridge.
# Copyright (C) 2022 Tulir Asokan
#
# 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.util.async_db import Database
fake_db = Database.create("") if TYPE_CHECKING else None
@dataclass
class UserContact:
db: ClassVar[Database] = fake_db
user: int
contact: int
in_community: bool
@classmethod
def _from_row(cls, row: Record | None) -> UserContact | None:
if row is None:
return None
return cls(**row)
@classmethod
async def all(cls, user: int) -> dict[int, UserContact]:
q = 'SELECT "user", contact, in_community FROM user_contact WHERE "user"=$1'
rows = await cls.db.fetch(q, user)
return {up.contact: up for up in (cls._from_row(row) for row in rows)}
@classmethod
async def get(cls, user: int, contact: int) -> UserContact | None:
q = 'SELECT "user", contact, in_community FROM user_contact WHERE "user"=$1 AND contact=$2'
row = await cls.db.fetchrow(q, user, contact)
return cls._from_row(row)
async def insert(self) -> None:
q = 'INSERT INTO user_contact ("user", contact, in_community) VALUES ($1, $2, $3)'
await self.db.execute(q, self.user, self.contact, self.in_community)
async def upsert(self) -> None:
q = (
'INSERT INTO user_contact ("user", contact, in_community) VALUES ($1, $2, $3) '
'ON CONFLICT ("user", contact) DO UPDATE SET in_community=$3'
)
await self.db.execute(q, self.user, self.contact, self.in_community)
async def delete(self) -> None:
q = 'DELETE FROM user_contact WHERE "user"=$1 AND contact=$2'
await self.db.execute(q, self.user, self.contact)
async def save(self) -> None:
q = 'UPDATE user_contact SET in_community=$1 WHERE "user"=$2 AND contact=$3'
await self.db.execute(q, self.in_community, self.user, self.contact)
@classmethod
async def delete_all(cls, user: int) -> None:
await cls.db.execute('DELETE FROM user_contact WHERE "user"=$1', user)