Spaces:
Running
Running
File size: 18,685 Bytes
3a67739 2fe1440 1fa270a 89e33ed 3a67739 1fa270a 3a67739 1fa270a 2fe1440 89e33ed 3a67739 658e097 89e33ed 3a67739 8359204 3a67739 658e097 3a67739 2fe1440 89e33ed 3a67739 89e33ed 3a67739 89e33ed 3a67739 e7261d3 3a67739 658e097 3a67739 658e097 3a67739 658e097 3a67739 658e097 3a67739 17cac08 3a67739 8359204 3a67739 17cac08 3a67739 8359204 3a67739 e7261d3 3a67739 8359204 e7261d3 8359204 3a67739 e7261d3 3a67739 17cac08 2fe1440 89e33ed e7261d3 89e33ed 2fe1440 89e33ed 2fe1440 89e33ed e7261d3 2fe1440 89e33ed 2fe1440 89e33ed 2fe1440 89e33ed 2fe1440 e7261d3 2fe1440 89e33ed 2fe1440 89e33ed e7261d3 89e33ed e7261d3 89e33ed e7261d3 89e33ed e7261d3 89e33ed 2fe1440 3a67739 1fa270a 3a67739 | 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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | # Thunder/utils/database.py
import datetime
from typing import Any, Dict, Optional
from pymongo import AsyncMongoClient
from pymongo.asynchronous.collection import AsyncCollection
from pymongo.errors import DuplicateKeyError
from Thunder.vars import Var
from Thunder.utils.logger import logger
class Database:
def __init__(self, uri: str, database_name: str, *args, **kwargs):
self._client = AsyncMongoClient(uri, *args, **kwargs)
self.db = self._client[database_name]
self.col: AsyncCollection = self.db.users
self.banned_users_col: AsyncCollection = self.db.banned_users
self.banned_channels_col: AsyncCollection = self.db.banned_channels
self.token_col: AsyncCollection = self.db.tokens
self.authorized_users_col: AsyncCollection = self.db.authorized_users
self.restart_message_col: AsyncCollection = self.db.restart_message
self.files_col: AsyncCollection = self.db.files
self.file_ingest_locks_col: AsyncCollection = self.db.file_ingest_locks
async def _deduplicate_users(self) -> None:
pipeline = [
{"$sort": {"join_date": 1}},
{"$group": {"_id": "$id", "doc_id": {"$first": "$_id"}}},
{"$project": {"_id": "$doc_id"}}
]
keep_ids = []
async for doc in self.col.aggregate(pipeline):
keep_ids.append(doc["_id"])
if keep_ids:
result = await self.col.delete_many({"_id": {"$nin": keep_ids}})
if result.deleted_count > 0:
logger.warning(f"Deduplicated {result.deleted_count} duplicate user documents.")
async def ensure_indexes(self, *, raise_on_error: bool = True) -> bool:
try:
await self.banned_users_col.create_index("user_id", unique=True)
await self.banned_channels_col.create_index("channel_id", unique=True)
await self.token_col.create_index("token", unique=True)
await self.authorized_users_col.create_index("user_id", unique=True)
try:
await self.col.create_index("id", unique=True)
except DuplicateKeyError:
logger.warning("Duplicate users found, deduplicating...")
await self._deduplicate_users()
await self.col.create_index("id", unique=True)
await self.token_col.create_index("expires_at", expireAfterSeconds=0)
await self.token_col.create_index("activated")
await self.restart_message_col.create_index("message_id", unique=True)
await self.restart_message_col.create_index("timestamp", expireAfterSeconds=3600)
await self.files_col.create_index("file_unique_id", unique=True)
await self.files_col.create_index("public_hash", unique=True)
await self.files_col.create_index("canonical_message_id", unique=True)
await self.files_col.create_index("created_at")
await self.files_col.create_index("last_seen_at")
await self.file_ingest_locks_col.create_index("expires_at", expireAfterSeconds=0)
logger.debug("Database indexes ensured.")
return True
except Exception as e:
logger.error(f"Error in ensure_indexes: {e}", exc_info=True)
if raise_on_error:
raise
return False
def new_user(self, user_id: int) -> dict:
try:
return {
'id': user_id,
'join_date': datetime.datetime.now(datetime.timezone.utc)
}
except Exception as e:
logger.error(f"Error in new_user for user {user_id}: {e}", exc_info=True)
raise
async def add_user(self, user_id: int) -> bool:
try:
result = await self.col.update_one(
{'id': user_id},
{'$setOnInsert': self.new_user(user_id)},
upsert=True
)
if result.upserted_id:
logger.debug(f"Added new user {user_id} to database.")
return True
return False
except Exception as e:
logger.error(f"Error in add_user for user {user_id}: {e}", exc_info=True)
raise
async def is_user_exist(self, user_id: int) -> bool:
"""Read-only existence check. For user registration, use add_user() instead."""
try:
user = await self.col.find_one({'id': user_id}, {'_id': 1})
return bool(user)
except Exception as e:
logger.error(f"Error in is_user_exist for user {user_id}: {e}", exc_info=True)
raise
async def total_users_count(self) -> int:
try:
return await self.col.count_documents({})
except Exception as e:
logger.error(f"Error in total_users_count: {e}", exc_info=True)
return 0
async def get_authorized_users_count(self) -> int:
try:
return await self.authorized_users_col.count_documents({})
except Exception as e:
logger.error(f"Error in get_authorized_users_count: {e}", exc_info=True)
return 0
async def get_regular_users_count(self) -> int:
try:
auth_ids = await self.authorized_users_col.distinct("user_id")
return await self.col.count_documents({"id": {"$nin": auth_ids}})
except Exception as e:
logger.error(f"Error in get_regular_users_count: {e}", exc_info=True)
return 0
async def get_all_users(self):
try:
return self.col.find({})
except Exception as e:
logger.error(f"Error in get_all_users: {e}", exc_info=True)
return self.col.find({"_id": {"$exists": False}})
async def get_authorized_users_cursor(self):
try:
return self.authorized_users_col.find({})
except Exception as e:
logger.error(f"Error in get_authorized_users_cursor: {e}", exc_info=True)
return self.authorized_users_col.find({"_id": {"$exists": False}})
async def get_regular_users_cursor(self):
try:
auth_ids = await self.authorized_users_col.distinct("user_id")
return self.col.find({"id": {"$nin": auth_ids}})
except Exception as e:
logger.error(f"Error in get_regular_users_cursor: {e}", exc_info=True)
return self.col.find({"_id": {"$exists": False}})
async def delete_user(self, user_id: int):
try:
await self.col.delete_one({'id': user_id})
logger.debug(f"Deleted user {user_id}.")
except Exception as e:
logger.error(f"Error in delete_user for user {user_id}: {e}", exc_info=True)
raise
async def add_banned_user(
self, user_id: int, banned_by: Optional[int] = None,
reason: Optional[str] = None
):
try:
ban_data = {
"user_id": user_id,
"banned_at": datetime.datetime.now(datetime.timezone.utc),
"banned_by": banned_by,
"reason": reason
}
await self.banned_users_col.update_one(
{"user_id": user_id},
{"$set": ban_data},
upsert=True
)
logger.debug(f"Added/Updated banned user {user_id}. Reason: {reason}")
except Exception as e:
logger.error(f"Error in add_banned_user for user {user_id}: {e}", exc_info=True)
raise
async def remove_banned_user(self, user_id: int) -> bool:
try:
result = await self.banned_users_col.delete_one({"user_id": user_id})
if result.deleted_count > 0:
logger.debug(f"Removed banned user {user_id}.")
return True
return False
except Exception as e:
logger.error(f"Error in remove_banned_user for user {user_id}: {e}", exc_info=True)
return False
async def is_user_banned(self, user_id: int) -> Optional[Dict[str, Any]]:
try:
return await self.banned_users_col.find_one({"user_id": user_id})
except Exception as e:
logger.error(f"Error in is_user_banned for user {user_id}: {e}", exc_info=True)
return None
async def add_banned_channel(
self, channel_id: int, banned_by: Optional[int] = None,
reason: Optional[str] = None
):
try:
ban_data = {
"channel_id": channel_id,
"banned_at": datetime.datetime.now(datetime.timezone.utc),
"banned_by": banned_by,
"reason": reason
}
await self.banned_channels_col.update_one(
{"channel_id": channel_id},
{"$set": ban_data},
upsert=True
)
logger.debug(f"Added/Updated banned channel {channel_id}. Reason: {reason}")
except Exception as e:
logger.error(f"Error in add_banned_channel for channel {channel_id}: {e}", exc_info=True)
raise
async def remove_banned_channel(self, channel_id: int) -> bool:
try:
result = await self.banned_channels_col.delete_one({"channel_id": channel_id})
if result.deleted_count > 0:
logger.debug(f"Removed banned channel {channel_id}.")
return True
return False
except Exception as e:
logger.error(f"Error in remove_banned_channel for channel {channel_id}: {e}", exc_info=True)
return False
async def is_channel_banned(self, channel_id: int) -> Optional[Dict[str, Any]]:
try:
return await self.banned_channels_col.find_one({"channel_id": channel_id})
except Exception as e:
logger.error(f"Error in is_channel_banned for channel {channel_id}: {e}", exc_info=True)
return None
async def save_main_token(self, user_id: int, token_value: str, expires_at: datetime.datetime, created_at: datetime.datetime, activated: bool) -> None:
try:
await self.token_col.update_one(
{"user_id": user_id, "token": token_value},
{"$set": {
"expires_at": expires_at,
"created_at": created_at,
"activated": activated
}
},
upsert=True
)
logger.debug(f"Saved main token {token_value} for user {user_id} with activated status {activated}.")
except Exception as e:
logger.error(f"Error saving main token for user {user_id}: {e}", exc_info=True)
raise
async def add_restart_message(self, message_id: int, chat_id: int) -> None:
try:
await self.restart_message_col.insert_one({
"message_id": message_id,
"chat_id": chat_id,
"timestamp": datetime.datetime.now(datetime.timezone.utc)
})
logger.debug(f"Added restart message {message_id} for chat {chat_id}.")
except Exception as e:
logger.error(f"Error adding restart message {message_id}: {e}", exc_info=True)
async def get_restart_message(self) -> Optional[Dict[str, Any]]:
try:
return await self.restart_message_col.find_one(sort=[("timestamp", -1)])
except Exception as e:
logger.error(f"Error getting restart message: {e}", exc_info=True)
return None
async def delete_restart_message(self, message_id: int) -> None:
try:
await self.restart_message_col.delete_one({"message_id": message_id})
logger.debug(f"Deleted restart message {message_id}.")
except Exception as e:
logger.error(f"Error deleting restart message {message_id}: {e}", exc_info=True)
async def is_user_authorized(self, user_id: int) -> bool:
try:
user = await self.authorized_users_col.find_one({'user_id': user_id}, {'_id': 1})
return bool(user)
except Exception as e:
logger.error(f"Error in is_user_authorized for user {user_id}: {e}", exc_info=True)
return False
async def get_file_by_unique_id(self, file_unique_id: str) -> Optional[Dict[str, Any]]:
try:
return await self.files_col.find_one({"file_unique_id": file_unique_id})
except Exception as e:
logger.error(f"Error getting file by unique_id {file_unique_id}: {e}", exc_info=True)
return None
async def get_file_by_hash(
self,
public_hash: str,
*,
raise_on_error: bool = True
) -> Optional[Dict[str, Any]]:
try:
return await self.files_col.find_one({"public_hash": public_hash})
except Exception as e:
logger.error(f"Error getting file by hash {public_hash}: {e}", exc_info=True)
if raise_on_error:
raise
return None
async def get_file_by_message_id(self, canonical_message_id: int) -> Optional[Dict[str, Any]]:
try:
return await self.files_col.find_one({"canonical_message_id": canonical_message_id})
except Exception as e:
logger.error(
f"Error getting file by message_id {canonical_message_id}: {e}",
exc_info=True
)
return None
async def create_file_record(self, file_record: Dict[str, Any]) -> None:
try:
await self.files_col.insert_one(file_record)
except Exception as e:
logger.error(
f"Error creating canonical file record for {file_record.get('file_unique_id')}: {e}",
exc_info=True
)
raise
async def replace_file_record(self, file_record: Dict[str, Any]) -> None:
try:
await self.files_col.replace_one(
{"file_unique_id": file_record["file_unique_id"]},
file_record,
upsert=True
)
except Exception as e:
logger.error(
f"Error replacing canonical file record for {file_record.get('file_unique_id')}: {e}",
exc_info=True
)
raise
async def touch_file_record(
self,
public_hash: str,
*,
reused: bool = False,
raise_on_error: bool = False
) -> bool:
try:
update_doc: Dict[str, Any] = {
"$set": {"last_seen_at": datetime.datetime.now(datetime.timezone.utc)},
"$inc": {"seen_count": 1}
}
if reused:
update_doc["$inc"]["reuse_count"] = 1
await self.files_col.update_one({"public_hash": public_hash}, update_doc)
return True
except Exception as e:
logger.error(f"Error touching canonical file {public_hash}: {e}", exc_info=True)
if raise_on_error:
raise
return False
async def update_file_id(
self,
public_hash: str,
file_id: str,
*,
raise_on_error: bool = False
) -> bool:
try:
await self.files_col.update_one(
{"public_hash": public_hash},
{
"$set": {
"file_id": file_id,
"last_seen_at": datetime.datetime.now(datetime.timezone.utc)
}
}
)
return True
except Exception as e:
logger.error(f"Error updating file_id for {public_hash}: {e}", exc_info=True)
if raise_on_error:
raise
return False
async def acquire_file_ingest_claim(
self,
file_unique_id: str,
*,
ttl_seconds: int = 60
) -> bool:
now = datetime.datetime.now(datetime.timezone.utc)
claim_fields = {
"created_at": now,
"expires_at": now + datetime.timedelta(seconds=ttl_seconds)
}
try:
await self.file_ingest_locks_col.insert_one({
"_id": file_unique_id,
**claim_fields
})
return True
except DuplicateKeyError:
try:
result = await self.file_ingest_locks_col.find_one_and_update(
{
"_id": file_unique_id,
"$or": [
{"expires_at": {"$lte": now}},
{"expires_at": {"$exists": False}}
]
},
{
"$set": claim_fields
},
return_document=False
)
return bool(result)
except Exception as e:
logger.error(f"Error updating ingest claim for {file_unique_id}: {e}", exc_info=True)
raise
except Exception as e:
logger.error(f"Error acquiring ingest claim for {file_unique_id}: {e}", exc_info=True)
raise
async def release_file_ingest_claim(self, file_unique_id: str) -> bool:
try:
await self.file_ingest_locks_col.delete_one({"_id": file_unique_id})
return True
except Exception as e:
logger.error(f"Error releasing ingest claim for {file_unique_id}: {e}", exc_info=True)
return False
async def is_file_ingest_claim_active(self, file_unique_id: str) -> bool:
try:
claim = await self.file_ingest_locks_col.find_one(
{
"_id": file_unique_id,
"expires_at": {"$gt": datetime.datetime.now(datetime.timezone.utc)}
},
{"_id": 1}
)
return bool(claim)
except Exception as e:
logger.error(f"Error checking ingest claim for {file_unique_id}: {e}", exc_info=True)
raise
async def close(self):
if self._client:
await self._client.close()
db = Database(Var.DATABASE_URL, Var.NAME)
|