refactor: many changes

- Add missing actions and make a clear boundary between actions,
  services and nfra (i.e: actions shouldn't use stuff from infra/)
- Delete stuff not in use: tts, gtts, etc
- Add a ton of tests
This commit is contained in:
cătălin 2025-03-05 11:34:44 +01:00
commit 152546982c
No known key found for this signature in database
46 changed files with 2328 additions and 700 deletions

View file

@ -5,10 +5,12 @@ from litestar.datastructures import UploadFile
from litestar.response import Template
from pydantic import BaseModel, ConfigDict
from huesoporro.actions.chatbot.create_or_update_chatbot import (
CreateOrUpdateChatbotAction,
)
from huesoporro.actions.chatbot.get_chatbot_by_user_id import GetChatbotByUserIdAction
from huesoporro.bot import BotsManager
from huesoporro.models import ChatbotSettings, User
from huesoporro.svc.get_chatbot_settings import ChatbotSettingsGetterSvc
from huesoporro.svc.store_settings import ChatbotSettingsStorerSvc
from huesoporro.models import Chatbot, User
class ManageBotDTO(BaseModel):
@ -23,6 +25,12 @@ class ImportTextFileDTO(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
class UpdateChatbotDTO(BaseModel):
automatic_generation_timer: int = 300
automatic_quote_timer: int = 500
mods: list[str]
@get(
"/tts",
media_type=MediaType.HTML,
@ -48,8 +56,10 @@ async def get_tts_permalink(access_token: str) -> Template:
"/",
media_type=MediaType.HTML,
)
async def get_index(user: User, gbs: ChatbotSettingsGetterSvc) -> Template:
chatbot_settings = await gbs.run(user=user)
async def get_index(
user: User, get_chatbot_by_user_id_action: GetChatbotByUserIdAction
) -> Template:
chatbot_settings = await get_chatbot_by_user_id_action.run(user_id=user.id)
return Template(
template_name="index.html",
context=chatbot_settings.model_dump() if chatbot_settings else {},
@ -60,22 +70,24 @@ async def get_index(user: User, gbs: ChatbotSettingsGetterSvc) -> Template:
async def manage_bot(
user: User,
data: ManageBotDTO,
gbs: ChatbotSettingsGetterSvc,
sbs: ChatbotSettingsStorerSvc,
create_or_update_chatbot_action: CreateOrUpdateChatbotAction,
get_chatbot_by_user_id_action: GetChatbotByUserIdAction,
bm: BotsManager,
) -> Response:
chatbot_settings = await gbs.run(user=user)
if not chatbot_settings:
await sbs.run(user=user, bot_settings=ChatbotSettings())
chatbot_settings = await gbs.run(user=user)
chatbot = await get_chatbot_by_user_id_action.run(
user_id=user.id
) or await create_or_update_chatbot_action.run(
user_id=user.id,
)
if data.command == "start":
if not data.channel_name:
return Response({"message": "Channel name is required"}, status_code=400)
bm.add_bot(user, data.channel_name, chatbot_settings=chatbot_settings) # type: ignore[arg-type]
if user.user in bm.bots:
bm.add_bot(user, data.channel_name, chatbot=chatbot) # type: ignore[arg-type]
if user.username in bm.bots:
await bm.run_user_bot(user)
return Response({"message": "Bot started"})
if data.command == "stop" and user.user in bm.bots:
if data.command == "stop" and user.username in bm.bots:
await bm.stop_user_bot(user)
return Response({"message": "Bot stopped"})
return Response({"message": "Invalid command"}, status_code=400)
@ -83,24 +95,26 @@ async def manage_bot(
@get("/api/v1/bot")
async def get_bot_status(user: User, bm: BotsManager) -> dict:
if user.user not in bm.bots:
if user.username not in bm.bots:
return {"status": "ko"}
return {"status": "ok"}
@get("/api/v1/bot/settings")
async def get_bot_settings(
user: User, gbs: ChatbotSettingsGetterSvc
) -> ChatbotSettings | dict:
cbs = await gbs.run(user=user)
if not cbs:
return {"status": "Not found"}
return cbs
async def get_bot_settings(chatbot: Chatbot) -> Chatbot:
return chatbot
@put("/api/v1/bot/settings")
async def save_bot_settings(
user: User, data: ChatbotSettings, sbs: ChatbotSettingsStorerSvc
user: User,
data: UpdateChatbotDTO,
create_or_update_chatbot_action: CreateOrUpdateChatbotAction,
) -> dict:
await sbs.run(user=user, bot_settings=data)
await create_or_update_chatbot_action.run(
user_id=user.id,
automatic_generation_timer=data.automatic_generation_timer,
automatic_quote_timer=data.automatic_quote_timer,
mods=data.mods,
)
return {"status": "ok"}