diff --git a/src/gnomik/gnomik/__main__.py b/src/gnomik/gnomik/__main__.py index 6220b4c..71c4ab9 100644 --- a/src/gnomik/gnomik/__main__.py +++ b/src/gnomik/gnomik/__main__.py @@ -11,6 +11,7 @@ async def main(): block_loader("standard", "config", safe=False), block_loader("standard", "database", safe=False), block_loader("standard", "statistics", safe=False), + block_loader("standard", "chats", safe=False), block_loader("standard", "users", safe=False), block_loader("standard", "command_helper"), block_loader("standard", "roles", safe=False), diff --git a/src/karkas_blocks/karkas_blocks/standard/chats/__init__.py b/src/karkas_blocks/karkas_blocks/standard/chats/__init__.py new file mode 100644 index 0000000..7a66bb9 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/chats/__init__.py @@ -0,0 +1,15 @@ +from karkas_core.modules_system.public_api import ( + get_module, + register_outer_message_middleware, +) + +from .main import UsersMiddleware + + +def module_init(): + register_app_config = get_module("standard.database", "register_app_config") + from .db import APP_CONFIG + + register_app_config(APP_CONFIG) + + register_outer_message_middleware(UsersMiddleware()) diff --git a/src/karkas_blocks/karkas_blocks/standard/chats/db/__init__.py b/src/karkas_blocks/karkas_blocks/standard/chats/db/__init__.py new file mode 100644 index 0000000..e3e4a0b --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/chats/db/__init__.py @@ -0,0 +1 @@ +from .piccolo_app import APP_CONFIG diff --git a/src/karkas_blocks/karkas_blocks/standard/chats/db/piccolo_app.py b/src/karkas_blocks/karkas_blocks/standard/chats/db/piccolo_app.py new file mode 100644 index 0000000..0fc642e --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/chats/db/piccolo_app.py @@ -0,0 +1,15 @@ +import os + +from karkas_piccolo.conf.apps import AppConfig + +from .tables import ChatInfo + +CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) + +APP_CONFIG = AppConfig( + app_name="standard.chats", + migrations_folder_path=os.path.join(CURRENT_DIRECTORY, "piccolo_migrations"), + table_classes=[ChatInfo], + migration_dependencies=[], + commands=[], +) diff --git a/src/karkas_blocks/karkas_blocks/standard/chats/db/piccolo_migrations/__init__.py b/src/karkas_blocks/karkas_blocks/standard/chats/db/piccolo_migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/karkas_blocks/karkas_blocks/standard/chats/db/piccolo_migrations/standardchats_2024_08_20t17_25_21_296396.py b/src/karkas_blocks/karkas_blocks/standard/chats/db/piccolo_migrations/standardchats_2024_08_20t17_25_21_296396.py new file mode 100644 index 0000000..355e536 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/chats/db/piccolo_migrations/standardchats_2024_08_20t17_25_21_296396.py @@ -0,0 +1,104 @@ +from piccolo.apps.migrations.auto.migration_manager import MigrationManager +from piccolo.columns.column_types import Date, Integer, Text +from piccolo.columns.defaults.date import DateNow +from piccolo.columns.indexes import IndexMethod + +ID = "2024-08-20T17:25:21:296396" +VERSION = "1.16.0" +DESCRIPTION = "" + + +async def forwards(): + manager = MigrationManager( + migration_id=ID, app_name="standard.chats", description=DESCRIPTION + ) + + manager.add_table( + class_name="ChatInfo", tablename="chat_info", schema=None, columns=None + ) + + manager.add_column( + table_class_name="ChatInfo", + tablename="chat_info", + column_name="chat_id", + db_column_name="chat_id", + column_class_name="Integer", + column_class=Integer, + params={ + "default": 0, + "null": False, + "primary_key": True, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + manager.add_column( + table_class_name="ChatInfo", + tablename="chat_info", + column_name="chat_name", + db_column_name="chat_name", + column_class_name="Text", + column_class=Text, + params={ + "default": "", + "null": False, + "primary_key": False, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + manager.add_column( + table_class_name="ChatInfo", + tablename="chat_info", + column_name="chat_type", + db_column_name="chat_type", + column_class_name="Integer", + column_class=Integer, + params={ + "default": 10, + "null": False, + "primary_key": False, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + manager.add_column( + table_class_name="ChatInfo", + tablename="chat_info", + column_name="created_at", + db_column_name="created_at", + column_class_name="Date", + column_class=Date, + params={ + "default": DateNow(), + "null": False, + "primary_key": False, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + return manager diff --git a/src/karkas_blocks/karkas_blocks/standard/chats/db/tables.py b/src/karkas_blocks/karkas_blocks/standard/chats/db/tables.py new file mode 100644 index 0000000..8d1fe90 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/chats/db/tables.py @@ -0,0 +1,9 @@ +from piccolo.columns import Date, Integer, Text +from piccolo.table import Table + + +class ChatInfo(Table): + chat_id = Integer(primary_key=True) + chat_name = Text() + chat_type = Integer(default=10) + created_at = Date() diff --git a/src/karkas_blocks/karkas_blocks/standard/chats/info.json b/src/karkas_blocks/karkas_blocks/standard/chats/info.json new file mode 100644 index 0000000..9c34d03 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/chats/info.json @@ -0,0 +1,13 @@ +{ + "id": "standard.chats", + "name": "Чаты", + "description": "Очень полезный модуль", + "author": "Karkas Team", + "version": "1.0.0", + "privileged": true, + "dependencies": { + "required": { + "standard.database": "^1.0.0" + } + } + } diff --git a/src/karkas_blocks/karkas_blocks/standard/chats/main.py b/src/karkas_blocks/karkas_blocks/standard/chats/main.py new file mode 100644 index 0000000..469314f --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/chats/main.py @@ -0,0 +1,32 @@ +from typing import Any, Awaitable, Callable, Dict + +from aiogram import BaseMiddleware +from aiogram.types import Chat, TelegramObject + +from .db.tables import ChatInfo + + +async def update_chat_info(chat: Chat): + await ChatInfo.insert( + ChatInfo( + chat_name=chat.title, + ) + ).on_conflict( + action="DO UPDATE", + values=[ + ChatInfo.chat_name, + ], + ).run() + + +class UsersMiddleware(BaseMiddleware): + async def __call__( + self, + handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]], + event: TelegramObject, + data: Dict[str, Any], + ) -> Any: + chat = event.chat + await update_chat_info(chat) + result = await handler(event, data) + return result