-
Notifications
You must be signed in to change notification settings - Fork 104
/
users_handler.py
412 lines (338 loc) · 14.4 KB
/
users_handler.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""
Copyright (C) 2023-2024 Fern Lane
This file is part of the GPT-Telegramus distribution
(see <https://github.com/F33RNI/GPT-Telegramus>)
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 <http://www.gnu.org/licenses/>.
"""
import logging
import os
import json
import multiprocessing
from typing import Any, Dict, List, Tuple
from _version import version_major
# Default name for new users
DEFAULT_USER_NAME = "-"
class UsersHandler:
def __init__(self, config: Dict) -> None:
self.config = config
self._lock = multiprocessing.Lock()
def read_database(self) -> List[Dict] or None:
"""Tries to read and parse database
Returns:
List[Dict] or None: list of users or None in case of error
"""
try:
database_file = self.config.get("files").get("users_database")
# Create empty file
if not os.path.exists(database_file):
logging.info(f"Creating database file {database_file}")
with self._lock:
with open(database_file, "w+", encoding="utf-8") as file_:
json.dump([], file_, ensure_ascii=False, indent=4)
# Read and parse
logging.info(f"Reading users database from {database_file}")
with self._lock:
with open(database_file, "r", encoding="utf-8") as file_:
database = json.loads(file_.read())
return database
except Exception as e:
logging.error("Error reading users database", exc_info=e)
return None
def get_user(self, id_: int) -> Dict or None:
"""Tries to find user in database
Args:
id_ (int): ID of user to find
Returns:
Dict or None: user's data as dictionary as is (without replacing any keys) or None if not found
"""
try:
# Read database
database = self.read_database()
if database is None:
return None
# Find user
user = None
for user_ in database:
if user_["user_id"] == id_:
user = user_
break
# Check if we found them
if user:
return user
# No user
else:
logging.warning(f"No user {id_}")
return None
except Exception as e:
logging.error(f"Error finding user {id_} in database", exc_info=e)
return None
def get_key(self, id_: int, key: str, default_value: Any = None, user: Dict or None = None) -> Any:
"""Tries to read key value of user and handles previous formats (legacy)
It's possible to use pre-loaded dictionary as user argument (ex. from get_user() function)
If user argument is specified, id_ argument doesn't matter
Args:
id_ (int): ID of user
key (str): target key
default_value (Any, optional): fallback value. Defaults to None
user (Dict or None, optional): None to load from file, or Dict to use pre-loaded one. Defaults to None
Returns:
Any: key's value or default_value
"""
# Find user
if user is None:
user = self.get_user(id_)
# Check
if user is None:
return default_value
# Get user's format version
format_version = user.get("format_version")
############
# Language #
############
if key == "lang_id":
# Try current format
lang_id = user.get("lang_id")
# Old format
if lang_id is None and format_version is None:
lang_index = user.get("lang")
if lang_index is None:
return default_value
if lang_index == 0:
return "eng"
if lang_index == 1:
return "rus"
if lang_index == 2:
return "tof"
if lang_index == 3:
return "ind"
if lang_index == 4:
return "zho"
if lang_index == 5:
return "bel"
if lang_index == 6:
return "ukr"
if lang_index == 7:
return "fas"
if lang_index == 8:
return "spa"
return default_value
return default_value if lang_id is None else lang_id
##########
# Module #
##########
elif key == "module":
# Try current format
module = user.get("module")
# Old format
if module is not None and isinstance(module, int):
if module == 0:
return "lmao_chatgpt"
if module == 1:
return "dalle"
if module == 2:
return "lmao_ms_copilot" if "lmao_ms_copilot" in self.config else "ms_copilot"
if module == 3:
return "gemini"
if module == 4:
return "lmao_ms_copilot" if "lmao_ms_copilot" in self.config else "ms_copilot_image_creator"
if module == 5:
return "gemini"
return self.config.get("modules").get("default", default_value)
# Fix MS Copilot
if module == "ms_copilot" and "lmao_ms_copilot" in self.config:
module = "lmao_ms_copilot"
# Fallback to a legacy API
elif module == "lmao_ms_copilot" and "lmao_ms_copilot" not in self.config and "ms_copilot" in self.config:
module = "ms_copilot"
# Fix MS Copilot designer
if module == "ms_copilot_image_creator" and "lmao_ms_copilot" in self.config:
module = "lmao_ms_copilot"
return default_value if module is None else module
####################
# MS Copilot style #
####################
elif key == "ms_copilot_style":
# Try current format
ms_copilot_style = user.get("ms_copilot_style")
# Old format
if ms_copilot_style is None and format_version is None:
edgegpt_style = user.get("edgegpt_style")
if edgegpt_style is None:
return default_value
if edgegpt_style == 0:
return "precise"
if edgegpt_style == 1:
return "balanced"
if edgegpt_style == 2:
return "creative"
return default_value
return default_value if ms_copilot_style is None else ms_copilot_style
# Return key value or default value
return user.get(key, default_value)
def set_key(self, id_: int, key: str, value: Any) -> None:
"""Sets key's value of user and saves it to database or creates a new user
Args:
id_ (int): ID of user
key (str): key to set
value (Any): value of the key
"""
try:
# Read database
database = self.read_database()
if database is None:
return
# Find user
user_index = -1
for i, user_ in enumerate(database):
if user_["user_id"] == id_:
user_index = i
break
# User exists
if user_index != -1:
# Set the key
database[user_index][key] = value
# Save database
database_file = self.config.get("files").get("users_database")
logging.info(f"Saving users database to {database_file}")
with self._lock:
with open(database_file, "w+", encoding="utf-8") as file_:
json.dump(database, file_, ensure_ascii=False, indent=4)
# No user -> create a new one
else:
self.create_user(id_, key_values=[(key, value)])
except Exception as e:
logging.error(f"Error setting value of key {key} for user {id_}", exc_info=e)
def read_request_image(self, id_: int, user: Dict or None = None) -> bytes or None:
"""Tries to load user's last request image
Args:
id_ (int): ID of user
user (Dict or None, optional): None to load from file, or Dict to use pre-loaded one. Defaults to None
Returns:
bytes or None: image as bytes or None if not exists / error
"""
# Find user
if user is None:
user = self.get_user(id_)
if user is None:
return None
# Try to get image path
request_last_image = user.get("request_last_image")
# No image
if request_last_image is None or not os.path.exists(request_last_image):
return None
# Read
try:
logging.info(f"Reading user's last request image from {request_last_image}")
image_bytes = None
with open(request_last_image, "rb") as file:
image_bytes = file.read()
return image_bytes
except Exception as e:
logging.error("Error retrieving user's last request image", exc_info=e)
return None
def save_request_image(self, id_: int, image_bytes: bytes or None) -> None:
"""Saves user's last request image into file and it's path into users database
Args:
id_ (int): ID of user
image_bytes (bytes or None): image to save as bytes or None to delete existing one
"""
try:
# Read database
database = self.read_database()
if database is None:
return
# Find user
user_index = -1
for i, user_ in enumerate(database):
if user_["user_id"] == id_:
user_index = i
break
# Create directories if not exists
user_images_dir = self.config.get("files").get("user_images_dir")
if not os.path.exists(user_images_dir):
logging.info(f"Creating {user_images_dir} directory")
os.makedirs(user_images_dir)
request_last_image = os.path.join(user_images_dir, str(id_))
# Save image
if image_bytes is not None:
logging.info(f"Saving user's last request image to {request_last_image}")
with open(request_last_image, "wb+") as file:
file.write(image_bytes)
# Delete if exists
else:
if os.path.exists(request_last_image):
logging.info(f"Deleting user's last request image {request_last_image}")
os.remove(request_last_image)
request_last_image = None
# User exists
if user_index != -1:
# Set the key
database[user_index]["request_last_image"] = request_last_image
# Save database
database_file = self.config.get("files").get("users_database")
logging.info(f"Saving users database to {database_file}")
with self._lock:
with open(database_file, "w+", encoding="utf-8") as file_:
json.dump(database, file_, ensure_ascii=False, indent=4)
# No user -> create a new one
else:
self.create_user(id_, key_values=[("request_last_image", request_last_image)])
except Exception as e:
logging.error("Error saving user's last request image", exc_info=e)
def create_user(self, id_: int, key_values: List[Tuple[str, Any]] or None = None) -> Dict or None:
"""Creates a new user with default data and saves it to the database
Args:
id_ (int): ID of new user
key_values (List[Tuple[str, Any]]orNone, optional): list of (key, value) to set to a new user
Returns:
Dict or None: user's dictionary or None in case of error
"""
try:
# Create a new user with default params
logging.info(f"Creating a new user {id_}")
telegram_config = self.config.get("telegram")
user = {
"format_version": version_major(),
"user_id": id_,
"user_name": DEFAULT_USER_NAME,
"admin": True if id_ in telegram_config.get("admin_ids") else False,
"banned": (
False if id_ in telegram_config.get("admin_ids") else telegram_config.get("ban_by_default")
),
"module": self.config.get("modules").get("default"),
"requests_total": 0,
}
# Set additional keys
if key_values is not None:
for key, value in key_values:
user[key] = value
# Read database
database = self.read_database()
if database is None:
return None
# Check if user exists
for user_ in database:
if user_["user_id"] == id_:
raise Exception("User already exists")
# Append
database.append(user)
# Save database
database_file = self.config.get("files").get("users_database")
logging.info(f"Saving users database to {database_file}")
with self._lock:
with open(database_file, "w+", encoding="utf-8") as file_:
json.dump(database, file_, ensure_ascii=False, indent=4)
# Done -> return created user
return user
except Exception as e:
logging.error(f"Error creating user {id_}", exc_info=e)
return None