Skip to content

Commit

Permalink
feat: negative lookaround to avoid parsing links on page shares from …
Browse files Browse the repository at this point in the history
…tachiyomi
  • Loading branch information
j1nxie committed Jan 8, 2025
1 parent 9e50c8d commit ada6223
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
30 changes: 28 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ build = "build.rs"
anyhow = "1.0.86"
chrono = "0.4.38"
dotenvy = "0.15.7"
fancy-regex = "0.14.0"
futures = "0.3.31"
mangadex-api = { version = "3.4.1", features = ["multi-thread", "tokio-multi-thread"] }
mangadex-api-schema-rust = "0.10.0"
mangadex-api-types-rust = "0.10.0"
poise = { version = "0.6.1", features = ["cache"] }
rand = "0.8.5"
regex = "1.11.0"
rustc_version_runtime = "0.3.0"
sea-orm = { version = "1.0.1", features = [
"macros",
Expand Down
17 changes: 15 additions & 2 deletions src/commands/manga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub async fn add(
.inspect_err(|e| tracing::error!(err = ?e, "an error occurred when refreshing token"))?;

let uuid = match MD_URL_REGEX.captures(&input) {
Some(captures) => match uuid::Uuid::try_parse(&captures[1]) {
Ok(Some(captures)) => match uuid::Uuid::try_parse(&captures[1]) {
Ok(u) => {
tracing::info!(uuid = %u, "got uuid from link");
u
Expand All @@ -102,7 +102,7 @@ pub async fn add(
return Ok(());
}
},
None => match uuid::Uuid::try_parse(&input) {
Ok(None) => match uuid::Uuid::try_parse(&input) {
Ok(u) => {
tracing::info!(uuid = %u, "got uuid from input string");
u
Expand All @@ -122,6 +122,19 @@ pub async fn add(
return Ok(());
}
},
Err(e) => {
tracing::error!(err = ?e, "an error occurred when parsing input");
ctx.send(
poise::CreateReply::default()
.reply(true)
.allowed_mentions(CreateAllowedMentions::new().replied_user(false))
.content("an error occurred when parsing input."),
)
.await
.inspect_err(|e| tracing::error!(err = ?e, "an error occurred when sending reply"))?;

return Ok(());
}
};

let manga_list = manga::Entity::find()
Expand Down
4 changes: 2 additions & 2 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub mod version;
pub static POISE_VERSION: &str = "0.6.1";
pub static STARTUP_TIME: LazyLock<std::time::SystemTime> =
LazyLock::new(std::time::SystemTime::now);
pub static MD_URL_REGEX: LazyLock<regex::Regex> = LazyLock::new(|| {
regex::Regex::new(r"https://mangadex\.org/title/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})").unwrap()
pub static MD_URL_REGEX: LazyLock<fancy_regex::Regex> = LazyLock::new(|| {
fancy_regex::Regex::new(r"(?<!<)https://mangadex\.org/title/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})(?!>)").unwrap()
});
pub static AZUKI_MANGA: LazyLock<uuid::Uuid> =
LazyLock::new(|| uuid::Uuid::try_parse("5fed0576-8b94-4f9a-b6a7-08eecd69800d").unwrap());
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async fn event_handler(
return Ok(());
}

if let Some(captures) = MD_URL_REGEX.captures(&new_message.content) {
if let Ok(Some(captures)) = MD_URL_REGEX.captures(&new_message.content) {
let uuid = uuid::Uuid::try_parse(&captures[1]);

match uuid {
Expand Down

0 comments on commit ada6223

Please sign in to comment.