feat(bot): add basic in-db message storage (no attachments yet)
This commit is contained in:
@@ -9,6 +9,7 @@ client = AsyncMongoClient(env.db.connection_url)
|
||||
async def init_db():
|
||||
from .models import (
|
||||
DynamicConfig,
|
||||
MessageLog,
|
||||
RespondSession,
|
||||
ReviewSession,
|
||||
)
|
||||
@@ -19,5 +20,6 @@ async def init_db():
|
||||
DynamicConfig,
|
||||
RespondSession,
|
||||
ReviewSession,
|
||||
MessageLog,
|
||||
],
|
||||
)
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
from .config import DynamicConfig
|
||||
from .message_log import MessageLog
|
||||
from .session import RespondSession, ReviewSession
|
||||
|
||||
44
src/utils/db/models/message_log.py
Normal file
44
src/utils/db/models/message_log.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import datetime
|
||||
from typing import Annotated, Optional
|
||||
|
||||
from aiogram.enums.message_origin_type import MessageOriginType
|
||||
from beanie import Document, Indexed
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class MessageAttachment(BaseModel):
|
||||
file_id: str
|
||||
file_size: int
|
||||
file_name: str
|
||||
mime_type: str
|
||||
|
||||
|
||||
class MessageLogBase(BaseModel):
|
||||
chat_id: Annotated[int, Indexed()]
|
||||
message_id: Annotated[int, Indexed()]
|
||||
|
||||
user_id: Annotated[int, Indexed()]
|
||||
username: Optional[str] = None
|
||||
first_name: str
|
||||
last_name: Optional[str] = None
|
||||
|
||||
text: Optional[str]
|
||||
timestamp: datetime.datetime
|
||||
reply_to_message_id: Optional[int] = None
|
||||
|
||||
forward_from_type: Optional[MessageOriginType] = None
|
||||
original_message_date: Optional[datetime.datetime] = None
|
||||
original_message_author: Optional[str] = None
|
||||
original_message_author_id: Optional[int] = None
|
||||
|
||||
attachment_file_ids: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class MessageLog(MessageLogBase, Document):
|
||||
class Settings:
|
||||
name = "message_logs"
|
||||
indexes = [[("chat_id", 1), ("timestamp", -1)]]
|
||||
|
||||
@classmethod
|
||||
def get_n_messages(cls, chat_id: int, n: int):
|
||||
return cls.find(cls.chat_id == chat_id).sort("timestamp", "-").limit(n)
|
||||
Reference in New Issue
Block a user