Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 29 additions & 33 deletions crates/signal-bot/src/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::commands::menu_locale::{
help_menu, info_menu, is_exact_command, thread_help_menu, thread_info_menu,
};
use crate::commands::CommandHandler;
use crate::config::BotRole;
use crate::error::AppResult;
use crate::group_preferences_store::GroupPreferencesStore;
use async_trait::async_trait;
Expand All @@ -13,13 +12,17 @@ use std::sync::Arc;

const NOT_THREAD_MSG: &str = "!commands is only available in a Language Thread.";

pub struct HelpHandler {
role: BotRole,
}
pub struct HelpHandler;

impl HelpHandler {
pub fn new(role: BotRole) -> Self {
Self { role }
pub fn new() -> Self {
Self
}
}

impl Default for HelpHandler {
fn default() -> Self {
Self::new()
}
}

Expand All @@ -36,7 +39,7 @@ impl CommandHandler for HelpHandler {

async fn execute(&self, message: &BotMessage) -> AppResult<String> {
let _ = message;
Ok(help_menu(self.role).into())
Ok(help_menu().into())
}
}

Expand Down Expand Up @@ -75,12 +78,11 @@ impl CommandHandler for CommandsHandler {
/// Same menus as [`HelpHandler`], with per-command explanations and blank-line breaks.
pub struct InfoHandler {
group_prefs: Arc<GroupPreferencesStore>,
role: BotRole,
}

impl InfoHandler {
pub fn new(group_prefs: Arc<GroupPreferencesStore>, role: BotRole) -> Self {
Self { group_prefs, role }
pub fn new(group_prefs: Arc<GroupPreferencesStore>) -> Self {
Self { group_prefs }
}
}

Expand All @@ -95,14 +97,12 @@ impl CommandHandler for InfoHandler {
}

async fn execute(&self, message: &BotMessage) -> AppResult<String> {
if self.role == BotRole::Translation {
if let Some(group_id) = message.group_id.as_deref() {
if self.group_prefs.lookup_sidecar(group_id).is_some() {
return Ok(thread_info_menu().into());
}
if let Some(group_id) = message.group_id.as_deref() {
if self.group_prefs.lookup_sidecar(group_id).is_some() {
return Ok(thread_info_menu().into());
}
}
Ok(info_menu(self.role).into())
Ok(info_menu().into())
}
}

Expand Down Expand Up @@ -135,33 +135,29 @@ mod tests {
}

#[tokio::test]
async fn help_returns_role_specific_menu() {
let transcription = HelpHandler::new(BotRole::Transcription);
let translation = HelpHandler::new(BotRole::Translation);

assert!(transcription.matches(&dm("!help")));
assert!(!transcription.matches(&dm("!help-threads")));
assert!(!transcription.matches(&dm("!help-in-chat")));
let t = transcription.execute(&dm("!help")).await.unwrap();
assert!(t.contains("!transcribe"));
assert!(t.contains("!help-transcription"));
assert!(!t.contains("!privacy"));
assert!(!t.contains("!translate-me-on"));
async fn help_returns_hub_menu() {
let handler = HelpHandler::new();

let t = translation.execute(&dm("!help")).await.unwrap();
assert!(handler.matches(&dm("!help")));
assert!(!handler.matches(&dm("!help-threads")));
assert!(!handler.matches(&dm("!help-in-chat")));
let t = handler.execute(&dm("!help")).await.unwrap();
assert!(t.contains("!translation-threads"));
assert!(t.contains("!translation-in-chat"));
assert!(t.contains("!transcription"));
assert!(t.contains("!privacy"));
assert!(t.contains("!info"));
assert!(t.contains("!help-transcription"));
assert!(!t.contains("!transcribe-on"));
assert!(!t.contains("!translate-me-on"));
assert!(!t.contains("Voice notes in this chat"));
assert!(!t.contains("!set-en"));
}

#[tokio::test]
async fn info_returns_described_hub() {
let store = GroupPreferencesStore::new_in_memory(0);
let handler = InfoHandler::new(store, BotRole::Translation);
let handler = InfoHandler::new(store);
assert!(handler.matches(&dm("!info")));
let out = handler.execute(&dm("!info")).await.unwrap();
assert!(out.contains("!translation-threads\n "));
Expand All @@ -176,7 +172,7 @@ mod tests {
async fn help_in_sidecar_returns_hub_menu() {
let store = GroupPreferencesStore::new_in_memory(0);
store.set_sidecar("main-1", "it", "group.it".into(), "it-internal".into());
let handler = HelpHandler::new(BotRole::Translation);
let handler = HelpHandler::new();
let out = handler
.execute(&group("!help", "it-internal"))
.await
Expand Down Expand Up @@ -220,7 +216,7 @@ mod tests {
async fn info_in_sidecar_returns_thread_info() {
let store = GroupPreferencesStore::new_in_memory(0);
store.set_sidecar("main-1", "it", "group.it".into(), "it-internal".into());
let handler = InfoHandler::new(store, BotRole::Translation);
let handler = InfoHandler::new(store);
let out = handler
.execute(&group("!info", "it-internal"))
.await
Expand All @@ -232,7 +228,7 @@ mod tests {

#[tokio::test]
async fn help_in_main_stays_hub() {
let handler = HelpHandler::new(BotRole::Translation);
let handler = HelpHandler::new();
let out = handler.execute(&group("!help", "main-1")).await.unwrap();
assert!(out.contains("!translation-threads"));
assert!(!out.contains("!rename"));
Expand Down
29 changes: 13 additions & 16 deletions crates/signal-bot/src/commands/menu_locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
//! Command-list layout follows the Signal mobile menu standard:
//! [`docs/solutions/signal-mobile-menus.md`](../../../../docs/solutions/signal-mobile-menus.md).

use crate::config::BotRole;
pub fn help_menu() -> &'static str {
HELP_HUB
}

pub fn help_menu(role: BotRole) -> &'static str {
match role {
BotRole::Transcription => HELP_TRANSCRIPTION,
BotRole::Translation => HELP_HUB,
}
/// Voice product menu (`!transcription`).
pub fn transcription_menu() -> &'static str {
HELP_TRANSCRIPTION
}

/// Hub descriptive menu (translation bot only; transcription uses `!transcription`).
pub fn info_menu(role: BotRole) -> &'static str {
match role {
BotRole::Translation => INFO_HUB,
BotRole::Transcription => unreachable!("transcription bot does not register !info"),
}
/// Hub descriptive menu (`!info`).
pub fn info_menu() -> &'static str {
INFO_HUB
}

pub fn thread_help_menu() -> &'static str {
Expand Down Expand Up @@ -302,7 +299,7 @@ mod tests {

#[test]
fn help_translation_is_hub() {
let h = help_menu(BotRole::Translation);
let h = help_menu();
assert!(h.contains("!translation-threads"));
assert!(h.contains("!translation-in-chat"));
assert!(h.contains("!transcription"));
Expand All @@ -317,7 +314,7 @@ mod tests {

#[test]
fn info_hub_has_breaks_and_descriptions() {
let h = info_menu(BotRole::Translation);
let h = info_menu();
assert!(h.contains("!translation-threads\n "));
assert!(h.contains("!translation-in-chat\n "));
assert!(h.contains("!transcription\n "));
Expand Down Expand Up @@ -439,7 +436,7 @@ mod tests {

#[test]
fn help_transcription_covers_voice() {
let h = help_menu(BotRole::Transcription);
let h = transcription_menu();
assert!(h.contains("!transcribe"));
assert!(h.contains("!transcribe-on"));
assert!(h.contains("!transcribe-off"));
Expand Down Expand Up @@ -472,7 +469,7 @@ mod tests {
assert!(!translation_threads_menu().contains("!verify"));
assert!(!translation_in_chat_menu(true).contains("!verify"));
assert!(!translation_in_chat_menu(false).contains("!verify"));
assert!(!help_menu(BotRole::Translation).contains("!verify"));
assert!(!help_menu().contains("!verify"));
assert!(!thread_help_menu().contains("!verify"));
}

Expand Down
21 changes: 17 additions & 4 deletions crates/signal-bot/src/commands/product_menus.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//! Product menus: `!translation-threads`, `!translation-in-chat`, `!transcription`, redirects.

use crate::commands::menu_locale::{
help_in_chat_guide, help_menu, help_threads_guide, help_transcription_guide, is_exact_command,
is_translation_in_chat_menu_command, is_translation_threads_menu_command,
help_in_chat_guide, help_threads_guide, help_transcription_guide, is_exact_command,
is_translation_in_chat_menu_command, is_translation_threads_menu_command, transcription_menu,
translation_in_chat_menu, translation_split_redirect, translation_threads_menu,
};
use crate::commands::CommandHandler;
use crate::config::BotRole;
use crate::error::AppResult;
use async_trait::async_trait;
use signal_client::BotMessage;
Expand Down Expand Up @@ -117,7 +116,7 @@ impl CommandHandler for TranscriptionMenuHandler {
}

async fn execute(&self, _message: &BotMessage) -> AppResult<String> {
Ok(help_menu(BotRole::Transcription).into())
Ok(transcription_menu().into())
}
}

Expand Down Expand Up @@ -367,4 +366,18 @@ mod tests {
assert!(transcription.contains("Whisper"));
assert!(transcription.contains("!transcribe"));
}

#[tokio::test]
async fn transcription_menu_returns_voice_commands() {
let out = TranscriptionMenuHandler::new()
.execute(&msg("!transcription"))
.await
.unwrap();
assert!(out.contains("!transcribe-on"));
assert!(out.contains("!transcribe-off"));
assert!(out.contains("!transcribe"));
assert!(out.contains("!help-transcription"));
assert!(!out.contains("!privacy"));
assert!(!out.contains("!translation-threads"));
}
}
Loading
Loading