Skip to content

Commit

Permalink
fix tag locking, fix set name parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
avoonix committed Jul 8, 2024
1 parent dc22fb9 commit 01af50d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
11 changes: 1 addition & 10 deletions fuzzle/src/callback/callback_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use crate::callback::CallbackData;

use tracing::Instrument;

#[tracing::instrument(skip(request_context, q))]
async fn change_sticker_locked_status(
lock: bool,
unique_id: &str,
Expand All @@ -45,16 +46,6 @@ async fn change_sticker_locked_status(
return Err(anyhow::anyhow!(
"user is not permitted to change locked status"
))?;
} else {
// TODO: inform admin; this should not happen
return answer_callback_query(
request_context.clone(),
q,
Some(Text::sticker_not_found()),
None,
None,
)
.await;
};
let sticker = request_context
.database
Expand Down
15 changes: 14 additions & 1 deletion fuzzle/src/inline/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ fn parse_inline_query_data(input: &str) -> IResult<&str, InlineQueryData> {
tag(")"),
take_while(|c| true),
)),
|(_, sticker_id, _, set_title)| InlineQueryData::AddToUserSet {
|(_, sticker_id, _, set_title)| InlineQueryData::AddToUserSet {
sticker_id: sticker_id.to_string(),
set_title: {
let title = set_title.trim();
Expand Down Expand Up @@ -544,6 +544,19 @@ mod tests {
use super::*;
use anyhow::Result;

#[test]
fn set_tag_operation() -> Result<()> {
assert_eq!(
InlineQueryData::try_from("(se:asdf) dragon uwu".to_string())?,
InlineQueryData::SearchTagsForStickerSet {
set_name: "asdf".to_string(),
operation: SetOperation::Tag,
tags: vec![vec!["dragon".to_string(), "uwu".to_string()]],
}
);
Ok(())
}

#[test]
fn stringify_query() {
let query = InlineQueryData::empty_sticker_query("asdf");
Expand Down
18 changes: 13 additions & 5 deletions fuzzle/src/util/parsers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use nom::branch::alt;
use nom::bytes::complete::{tag, take_while1};
use nom::character::complete::{alpha1, alphanumeric1};
use nom::character::complete::{alpha1, alphanumeric1, satisfy};
use nom::combinator::{fail, recognize, success, opt};
use nom::multi::{many1, many1_count, separated_list1};
use nom::sequence::{preceded, tuple};
Expand Down Expand Up @@ -30,10 +30,7 @@ pub fn tag_literal(input: &str) -> IResult<&str, &str> {
}

pub fn set_name_literal(input: &str) -> IResult<&str, &str> {
recognize(preceded(
tuple((alpha1, opt(tag("_")))),
separated_list1(tag("_"), alphanumeric1),
))(input)
recognize( tuple((satisfy(|c| c.is_ascii_alphabetic()), opt(tag("_")), separated_list1(tag("_"), alphanumeric1))))(input)
}

pub fn parse_emoji(input: &str) -> IResult<&str, Emoji> {
Expand All @@ -49,6 +46,17 @@ mod tests {
use anyhow::Result;
use nom::Finish;

#[tokio::test]
async fn parse_set_names() -> anyhow::Result<()> {
for name in ["asdf", "a_b_c", "a1234"] {
assert_eq!(Ok(("", name)), set_name_literal(name).finish());
}
for name in ["_asdf_", "a__b", "1234a"] {
assert!(matches!(set_name_literal(name).finish(), Err(_)));
}
Ok(())
}

#[tokio::test]
async fn parse_tag_literals_from_tag_manager() -> anyhow::Result<()> {
let tag_manager = get_default_tag_manager(std::env::temp_dir()).await?;
Expand Down

0 comments on commit 01af50d

Please sign in to comment.