добавлен блок standard.chat

This commit is contained in:
Maxim Slipenko 2024-08-20 17:26:09 +03:00
parent a3f0298288
commit 8512b3300b
No known key found for this signature in database
GPG Key ID: 6DEA9FA7DE98C7D6
9 changed files with 190 additions and 0 deletions

View File

@ -11,6 +11,7 @@ async def main():
block_loader("standard", "config", safe=False), block_loader("standard", "config", safe=False),
block_loader("standard", "database", safe=False), block_loader("standard", "database", safe=False),
block_loader("standard", "statistics", safe=False), block_loader("standard", "statistics", safe=False),
block_loader("standard", "chats", safe=False),
block_loader("standard", "users", safe=False), block_loader("standard", "users", safe=False),
block_loader("standard", "command_helper"), block_loader("standard", "command_helper"),
block_loader("standard", "roles", safe=False), block_loader("standard", "roles", safe=False),

View File

@ -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())

View File

@ -0,0 +1 @@
from .piccolo_app import APP_CONFIG

View File

@ -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=[],
)

View File

@ -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

View File

@ -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()

View File

@ -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"
}
}
}

View File

@ -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