| 1 | //! Queue commands: queue list/edit/drop/clear |
| 2 | |
| 3 | use codewhale_command_contract::facets::{CommandComposerContext, CommandPresentationContext}; |
| 4 | use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler}; |
| 5 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 6 | use codewhale_command_contract::outcome::CommandResult; |
| 7 | |
| 8 | const COMMAND_INFO: CommandInfo = CommandInfo { |
| 9 | name: "queue", |
| 10 | aliases: &["queued"], |
| 11 | usage: "/queue [list|send <n>|edit <n>|drop <n>|clear]", |
| 12 | description_key: "cmd_queue_description", |
| 13 | }; |
| 14 | |
| 15 | pub(super) struct QueueCmd; |
| 16 | |
| 17 | impl<A> RegisterCommand<CommandResult<A>> for QueueCmd { |
| 18 | fn info() -> &'static CommandInfo { |
| 19 | &COMMAND_INFO |
| 20 | } |
| 21 | fn handler() -> CommandHandler<CommandResult<A>> { |
| 22 | CommandHandler::Contextual { |
| 23 | capabilities: CommandCapabilities::COMPOSER.union(CommandCapabilities::PRESENTATION), |
| 24 | handler: queue, |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn queue<A>(mut contexts: CommandContexts<'_>, args: Option<&str>) -> CommandResult<A> { |
| 30 | let Some(composer) = contexts.take_composer() else { |
| 31 | return CommandResult::error("Command capability unavailable: composer"); |
| 32 | }; |
| 33 | let Some(presentation) = contexts.into_parts().presentation else { |
| 34 | return CommandResult::error("Command capability unavailable: presentation"); |
| 35 | }; |
| 36 | match execute_queue(composer, presentation, args.unwrap_or("").trim()) { |
| 37 | Ok(message) => CommandResult::message(message), |
| 38 | Err(error) => CommandResult::error(error), |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn execute_queue( |
| 43 | composer: &mut dyn CommandComposerContext, |
| 44 | presentation: &dyn CommandPresentationContext, |
| 45 | arg: &str, |
| 46 | ) -> Result<String, String> { |
| 47 | let translate = |key| presentation.translate(key, &[]); |
| 48 | if arg.is_empty() || arg.eq_ignore_ascii_case("list") { |
| 49 | let queue = composer.queue_previews(); |
| 50 | let mut lines = Vec::new(); |
| 51 | if let Some(draft) = queue.editing { |
| 52 | lines.push("Editing queued message:".to_string()); |
| 53 | lines.push(format!("- {draft}")); |
| 54 | } |
| 55 | if queue.waiting.is_empty() { |
| 56 | return if lines.is_empty() { |
| 57 | translate("cmd_queue_no_messages") |
| 58 | } else { |
| 59 | Ok(lines.join("\n")) |
| 60 | }; |
| 61 | } |
| 62 | lines.push(presentation.translate( |
| 63 | "cmd_queue_list_header", |
| 64 | &[("count", &queue.waiting.len().to_string())], |
| 65 | )?); |
| 66 | for (index, preview) in queue.waiting.iter().enumerate() { |
| 67 | lines.push(format!("{}. {preview}", index + 1)); |
| 68 | } |
| 69 | lines.push(translate("cmd_queue_tip")?); |
| 70 | return Ok(lines.join("\n")); |
| 71 | } |
| 72 | let mut words = arg.split_whitespace(); |
| 73 | let action = words.next().unwrap_or_default().to_lowercase(); |
| 74 | match action.as_str() { |
| 75 | "clear" => { |
| 76 | let cleared = translate("cmd_queue_cleared")?; |
| 77 | let empty = translate("cmd_queue_already_empty")?; |
| 78 | Ok(if composer.clear_queued() { |
| 79 | cleared |
| 80 | } else { |
| 81 | empty |
| 82 | }) |
| 83 | } |
| 84 | "edit" | "drop" | "remove" | "rm" => { |
| 85 | let editing = action == "edit"; |
| 86 | if editing && composer.editing_queue() { |
| 87 | return Err(translate("cmd_queue_already_editing")?); |
| 88 | } |
| 89 | let Some(raw) = words.next() else { |
| 90 | return Err(translate("cmd_queue_missing_index")?); |
| 91 | }; |
| 92 | let Ok(number) = raw.parse::<usize>() else { |
| 93 | return Err(translate("cmd_queue_index_positive")?); |
| 94 | }; |
| 95 | let Some(index) = number.checked_sub(1) else { |
| 96 | return Err(translate("cmd_queue_index_min")?); |
| 97 | }; |
| 98 | let number = number.to_string(); |
| 99 | let replacements = &[("index", number.as_str())]; |
| 100 | let message_key = if editing { |
| 101 | "cmd_queue_editing_message" |
| 102 | } else { |
| 103 | "cmd_queue_dropped" |
| 104 | }; |
| 105 | let message = presentation.translate(message_key, replacements)?; |
| 106 | let found = if editing { |
| 107 | let status = presentation.translate("cmd_queue_editing_status", replacements)?; |
| 108 | composer.edit_queued(index, status) |
| 109 | } else { |
| 110 | composer.drop_queued(index) |
| 111 | }; |
| 112 | if found { |
| 113 | Ok(message) |
| 114 | } else { |
| 115 | Err(translate("cmd_queue_not_found")?) |
| 116 | } |
| 117 | } |
| 118 | _ => Err(translate("cmd_queue_usage")?), |
| 119 | } |
| 120 | } |
| 121 |