baibot/controller/cfg/dispatching/
mod.rs

1use crate::strings;
2use crate::{Bot, entity::MessageContext};
3use mxlink::MessageResponseType;
4
5use super::controller_type::{
6    ConfigControllerType, ConfigSettingRelatedControllerType, SettingsStorageSource,
7};
8
9mod speech_to_text;
10mod text_generation;
11mod text_to_speech;
12
13pub async fn dispatch_controller(
14    handler: &ConfigControllerType,
15    message_context: &MessageContext,
16    bot: &Bot,
17) -> anyhow::Result<()> {
18    // Anyone can access Help and Status.
19    // Settings-related access checks are done in dispatch_config_related_handler().
20
21    match handler {
22        ConfigControllerType::Help => super::help::handle(bot, message_context).await,
23        ConfigControllerType::Status => super::status::handle(bot, message_context).await,
24        ConfigControllerType::SettingsRelated(config_type, config_related_handler) => {
25            dispatch_config_related_handler(
26                config_type,
27                config_related_handler,
28                message_context,
29                bot,
30            )
31            .await
32        }
33    }
34}
35
36async fn dispatch_config_related_handler(
37    config_type: &SettingsStorageSource,
38    handler: &ConfigSettingRelatedControllerType,
39    message_context: &MessageContext,
40    bot: &Bot,
41) -> anyhow::Result<()> {
42    if let SettingsStorageSource::Global = config_type {
43        if !message_context.sender_can_manage_global_config() {
44            bot.messaging()
45                .send_error_markdown_no_fail(
46                    message_context.room(),
47                    strings::global_config::no_permissions_to_administrate(),
48                    MessageResponseType::Reply(message_context.thread_info().root_event_id.clone()),
49                )
50                .await;
51            return Ok(());
52        }
53    };
54
55    let room_settings = match config_type {
56        SettingsStorageSource::Room => &message_context.room_config().settings,
57        SettingsStorageSource::Global => &message_context.global_config().fallback_room_settings,
58    };
59
60    match handler {
61        ConfigSettingRelatedControllerType::GetHandler(purpose) => match config_type {
62            SettingsStorageSource::Room => {
63                super::room_config::handler::handle_get(bot, message_context, *purpose).await
64            }
65            SettingsStorageSource::Global => {
66                super::global_config::handler::handle_get(bot, message_context, *purpose).await
67            }
68        },
69        ConfigSettingRelatedControllerType::SetHandler(purpose, agent_identifier) => {
70            match config_type {
71                SettingsStorageSource::Room => {
72                    super::room_config::handler::handle_set(
73                        bot,
74                        bot.room_config_manager(),
75                        message_context,
76                        *purpose,
77                        agent_identifier,
78                    )
79                    .await
80                }
81                SettingsStorageSource::Global => {
82                    super::global_config::handler::handle_set(
83                        bot,
84                        bot.global_config_manager(),
85                        message_context,
86                        *purpose,
87                        agent_identifier,
88                    )
89                    .await
90                }
91            }
92        }
93        ConfigSettingRelatedControllerType::TextGeneration(controller_type) => {
94            text_generation::dispatch(
95                controller_type,
96                message_context,
97                bot,
98                room_settings,
99                config_type,
100            )
101            .await
102        }
103        ConfigSettingRelatedControllerType::SpeechToText(controller_type) => {
104            speech_to_text::dispatch(
105                controller_type,
106                message_context,
107                bot,
108                room_settings,
109                config_type,
110            )
111            .await
112        }
113        ConfigSettingRelatedControllerType::TextToSpeech(controller_type) => {
114            text_to_speech::dispatch(
115                controller_type,
116                message_context,
117                bot,
118                room_settings,
119                config_type,
120            )
121            .await
122        }
123    }
124}