-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcache.py
215 lines (189 loc) · 6.51 KB
/
cache.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*- coding: utf-8 -*-
# cache.py
# Copyright (C) 2017-2022 KunoiSayami
#
# 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
# 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/>.
import struct
import time
from dataclasses import dataclass
import asyncpg
from libpy3.aiopgsqldb import PgSQLdb
DATABASE_CURRENT_VERSION = 1
PACK_FORMAT = "<h????????"
class GroupAdmins:
def __init__(self):
self._admins_list: list[int] | None = None
self._last_fetch: float = 0.0
@property
def admins_list(self) -> list[int] | None:
if time.time() - self._last_fetch > 120:
return None
return self._admins_list
@admins_list.setter
def admins_list(self, value: list[int]) -> None:
del self._admins_list
self._admins_list = value
self._last_fetch = time.time()
@dataclass(init=False)
class GroupProperty:
welcome_text: str | None
no_welcome: bool
no_service_msg: bool
no_new_member: bool
no_blue: bool
ignore_err: bool
poemable: bool
no_channel: bool
no_channel_msg: bool
def __init__(
self,
text: str | None,
no_welcome: bool,
no_service_msg: bool,
no_new_member: bool,
no_blue: bool,
ignore_err: bool,
poemable: bool,
no_channel: bool,
no_channel_msg: bool,
):
self.welcome_text: str | None = text
self.no_welcome: bool = no_welcome
self.no_service_msg: bool = no_service_msg
self.no_new_member: bool = no_new_member
self.no_blue: bool = no_blue
self.ignore_err: bool = ignore_err
self.poemable: bool = poemable
self.no_channel: bool = no_channel
self.no_channel_msg: bool = no_channel_msg
self._admins_list: GroupAdmins = GroupAdmins()
@property
def admins(self) -> list[int] | None:
return self._admins_list.admins_list
@admins.setter
def admins(self, value: list[int]) -> None:
self._admins_list.admins_list = value
@classmethod
def unpack(cls, welcome_text: str, data: bytes) -> "GroupProperty":
version, = struct.unpack("<h", data[:2])
if version != DATABASE_CURRENT_VERSION:
raise ValueError(
f"except version {DATABASE_CURRENT_VERSION} but {version} found"
)
(
_,
no_welcome,
no_service_msg,
no_new_member,
no_blue,
ignore_err,
poemable,
no_channel,
no_channel_msg,
) = struct.unpack(PACK_FORMAT, data)
return cls(
welcome_text,
no_welcome,
no_service_msg,
no_new_member,
no_blue,
ignore_err,
poemable,
no_channel,
no_channel_msg,
)
def pack(self) -> bytes:
return struct.pack(
PACK_FORMAT,
DATABASE_CURRENT_VERSION,
self.no_welcome,
self.no_service_msg,
self.no_new_member,
self.no_blue,
self.ignore_err,
self.poemable,
self.no_channel,
self.no_channel_msg,
)
class PostgreSQL(PgSQLdb):
async def insert_last_message_id(self, chat_id: int, message_id: int) -> None:
await self.execute(
"""UPDATE "welcome_msg" SET "previous_msg_id" = $1 WHERE "group_id" = $2""",
message_id,
chat_id,
)
async def query_last_message_id(self, chat_id: int) -> int:
return (
await self.query1(
"""SELECT "previous_msg_id" FROM "welcome_msg" WHERE "group_id" = $1""",
chat_id,
)
)["previous_msg_id"]
# FIXME: using redis instead built-in dict
class GroupCache:
def __init__(self, conn: PostgreSQL):
self.conn = conn
self.groups = {}
@classmethod
async def create(cls, conn: PostgreSQL) -> "GroupCache":
self = GroupCache(conn)
await self.read_database()
return self
async def read_database(self) -> None:
sql_obj = await self.conn.query(
"""SELECT * FROM "welcome_msg" WHERE "available" = true"""
)
for x in sql_obj:
self.groups.update({x["group_id"]: self.get_group_property_from_dict(x)})
def __getitem__(self, key: int) -> GroupProperty | None:
return self.groups.get(key)
@staticmethod
def get_group_property_from_dict(d: asyncpg.Record) -> GroupProperty:
return GroupProperty.unpack(d["msg"], d["flags"])
async def insert_group(self, chat_id: int) -> GroupProperty:
await self.update_group(
chat_id,
GroupProperty(None, False, False, False, False, True, False, False, False),
)
return self.groups[chat_id]
async def update_group(
self, chat_id: int, new_property: GroupProperty, no_update: bool = False
) -> None:
self.groups.update({chat_id: new_property})
if no_update:
return
if (
await self.conn.query1(
"""SELECT 1 FROM "welcome_msg" WHERE "group_id" = $1""", chat_id
)
is None
): # type: ignore
await self.conn.execute(
"""INSERT INTO "welcome_msg"
("group_id", "msg", "flags")
VALUES ($1, $2, $3)""",
chat_id,
new_property.welcome_text,
new_property.pack(),
) # type: ignore
else:
await self.conn.execute(
"""UPDATE "welcome_msg"
SET "msg" = $1, "flags" = $2 WHERE "group_id" = $3""",
new_property.welcome_text,
new_property.pack(),
chat_id,
) # type: ignore
async def delete_group(self, chat_id: int) -> None:
await self.conn.execute("""DELETE FROM "welcome_msg" WHERE "group_id" = $1""", chat_id) # type: ignore