storkit: merge 447_bug_element_tab_completion_display_name_breaks_bot_command_matching
This commit is contained in:
+49
-5
@@ -46,29 +46,44 @@ pub fn strip_prefix_ci<'a>(text: &'a str, prefix: &str) -> Option<&'a str> {
|
||||
/// - `@bot_localpart:server.com rest` → `rest`
|
||||
/// - `@bot_localpart rest` → `rest`
|
||||
/// - `DisplayName rest` → `rest`
|
||||
/// - `DisplayName: rest` → `rest` (Element tab-completion inserts a colon)
|
||||
/// - `DisplayName, rest` → `rest` (Element tab-completion may insert a comma)
|
||||
/// - `DisplayName ⚡️: rest` → `rest` (display name with emoji)
|
||||
pub fn strip_bot_mention<'a>(message: &'a str, bot_name: &str, bot_user_id: &str) -> &'a str {
|
||||
let trimmed = message.trim();
|
||||
|
||||
// Try full Matrix user ID (e.g. "@timmy:homeserver.local")
|
||||
if let Some(rest) = strip_prefix_ci(trimmed, bot_user_id) {
|
||||
return rest;
|
||||
return strip_mention_separator(rest);
|
||||
}
|
||||
|
||||
// Try @localpart (e.g. "@timmy")
|
||||
if let Some(localpart) = bot_user_id.split(':').next()
|
||||
&& let Some(rest) = strip_prefix_ci(trimmed, localpart)
|
||||
{
|
||||
return rest;
|
||||
return strip_mention_separator(rest);
|
||||
}
|
||||
|
||||
// Try display name (e.g. "Timmy")
|
||||
// Try display name (e.g. "Timmy" or "timmy ⚡️")
|
||||
if let Some(rest) = strip_prefix_ci(trimmed, bot_name) {
|
||||
return rest;
|
||||
return strip_mention_separator(rest);
|
||||
}
|
||||
|
||||
trimmed
|
||||
}
|
||||
|
||||
/// Strip an optional Element tab-completion separator (`:` or `,`) and
|
||||
/// surrounding whitespace from the start of text that follows a bot mention.
|
||||
///
|
||||
/// Element's tab-completion inserts `DisplayName: ` (colon + space) after the
|
||||
/// name. Without this strip the leading `:` would be treated as part of the
|
||||
/// command name and no command would match.
|
||||
fn strip_mention_separator(rest: &str) -> &str {
|
||||
let rest = rest.trim_start();
|
||||
let rest = rest.strip_prefix([',', ':']).unwrap_or(rest);
|
||||
rest.trim_start()
|
||||
}
|
||||
|
||||
/// Returns `true` when `text` ends while inside an open fenced code block.
|
||||
///
|
||||
/// A fenced code block opens and closes on lines that start with ` ``` `
|
||||
@@ -334,7 +349,36 @@ mod tests {
|
||||
#[test]
|
||||
fn strip_mention_comma_after_name() {
|
||||
let rest = strip_bot_mention("@timmy, help", "Timmy", "@timmy:homeserver.local");
|
||||
assert_eq!(rest.trim().trim_start_matches(',').trim(), "help");
|
||||
assert_eq!(rest.trim(), "help");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strip_mention_colon_separator_element_tab_completion() {
|
||||
// Element tab-completes display names with a trailing ": "
|
||||
let rest = strip_bot_mention(
|
||||
"timmy ⚡️: ambient on",
|
||||
"timmy ⚡️",
|
||||
"@timmy:homeserver.local",
|
||||
);
|
||||
assert_eq!(rest, "ambient on");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strip_mention_emoji_display_name_no_separator() {
|
||||
// Display name with emoji, no separator
|
||||
let rest = strip_bot_mention(
|
||||
"timmy ⚡️ ambient on",
|
||||
"timmy ⚡️",
|
||||
"@timmy:homeserver.local",
|
||||
);
|
||||
assert_eq!(rest, "ambient on");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strip_mention_colon_after_localpart() {
|
||||
// Element may also produce "@timmy: help"
|
||||
let rest = strip_bot_mention("@timmy: help", "Timmy", "@timmy:homeserver.local");
|
||||
assert_eq!(rest, "help");
|
||||
}
|
||||
|
||||
// -- drain_complete_paragraphs ------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user