Handle Telegram keyword searches and stateless result pages
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
pub(crate) enum Input<'a> {
|
||||
Ignore,
|
||||
Help,
|
||||
Search(telefind_core::Query<'a>),
|
||||
}
|
||||
|
||||
pub(crate) fn parse<'a>(bot: &str, text: &'a str) -> Input<'a> {
|
||||
let text = text.trim();
|
||||
if text.is_empty() {
|
||||
return Input::Ignore;
|
||||
}
|
||||
if !text.starts_with('/') {
|
||||
return Input::Search(telefind_core::Query::new(text));
|
||||
}
|
||||
let (command, args) = text.split_once(char::is_whitespace).unwrap_or((text, ""));
|
||||
let (command, target) = command
|
||||
.split_once('@')
|
||||
.map_or((command, None), |(cmd, target)| (cmd, Some(target)));
|
||||
if target.is_some_and(|name| !name.eq_ignore_ascii_case(bot.trim_start_matches('@'))) {
|
||||
return Input::Ignore;
|
||||
}
|
||||
match (command, args.trim()) {
|
||||
("/search", "") | ("/help" | "/start", _) => Input::Help,
|
||||
("/search", query) => Input::Search(telefind_core::Query::new(query)),
|
||||
("/page", args) => {
|
||||
let Some((number, text)) = args.split_once(char::is_whitespace) else {
|
||||
return Input::Help;
|
||||
};
|
||||
let mut query = telefind_core::Query::new(text.trim());
|
||||
let Some(offset) = number
|
||||
.parse::<usize>()
|
||||
.ok()
|
||||
.and_then(|n| n.checked_sub(1))
|
||||
.and_then(|n| n.checked_mul(query.limit))
|
||||
else {
|
||||
return Input::Help;
|
||||
};
|
||||
query.offset = offset;
|
||||
Input::Search(query)
|
||||
}
|
||||
_ => Input::Ignore,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user