feat: make save_messages optional in statistics

This commit is contained in:
Maxim Slipenko 2024-10-08 16:18:08 +03:00
parent 750b6be3a5
commit b1d95a1496
No known key found for this signature in database
GPG Key ID: 6DEA9FA7DE98C7D6
3 changed files with 25 additions and 1 deletions

View File

@ -1,3 +1,5 @@
from typing import TYPE_CHECKING
from karkas_core.modules_system.public_api import (
get_module,
register_outer_message_middleware,
@ -5,8 +7,15 @@ from karkas_core.modules_system.public_api import (
from .main import StatisticsMiddleware
if TYPE_CHECKING:
from karkas_blocks.standard.config import IConfig
config: "IConfig" = get_module("standard.config", "config")
def module_init():
config.register("statistics::save_messages", "boolean", default_value=False)
register_app_config = get_module("standard.database", "register_app_config")
from .db import APP_CONFIG

View File

@ -7,6 +7,7 @@
"privileged": true,
"dependencies": {
"required": {
"standard.config": "^1.0.0",
"standard.database": "^1.0.0"
}
},

View File

@ -7,6 +7,12 @@ from .db.tables import ChatStats, Messages, UserStats
if TYPE_CHECKING:
from aiogram.types import Message, TelegramObject
from karkas_blocks.standard.config import IConfig
from karkas_core.modules_system.public_api import get_module
config: "IConfig" = get_module("standard.config", "config")
async def update_chat_stats(event: "Message"):
await ChatStats.insert(
@ -53,6 +59,12 @@ async def save_messages(event: "Message"):
)
async def get_user_stats(chat_id: int, user_id: int):
return (
await UserStats.select().where(UserStats.key == f"{chat_id}-{user_id}").first()
)
class StatisticsMiddleware(BaseMiddleware):
async def __call__(
self,
@ -63,7 +75,9 @@ class StatisticsMiddleware(BaseMiddleware):
await update_chat_stats(event)
await update_user_stats(event)
await save_messages(event)
if config.get("statistics::save_messages"):
await save_messages(event)
result = await handler(event, data)