Skip to content

Commit

Permalink
Gracefully handle missing metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
cyborch committed Aug 25, 2023
1 parent d137b68 commit b0a50b9
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ const NODEINFO_21: &str = "http://nodeinfo.diaspora.software/ns/schema/2.1";
#[derive(Deserialize, Debug)]
struct Nodeinfo10 {
software: Software,
metadata: Metadata,
metadata: Option<Metadata>,
}

#[derive(Deserialize, Debug)]
struct Nodeinfo20 {
software: Software,
metadata: Metadata,
metadata: Option<Metadata>,
}

#[derive(Deserialize, Debug)]
struct Nodeinfo21 {
software: Software,
metadata: Metadata,
metadata: Option<Metadata>,
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -80,9 +80,11 @@ pub async fn detector(url: &str) -> Result<SNS, error::Error> {
"friendica" => Ok(SNS::Friendica),
"wildebeest" => Ok(SNS::Mastodon),
_ => {
if let Some(upstream) = nodeinfo.metadata.upstream {
if upstream.name == "mastodon" {
return Ok(SNS::Mastodon);
if let Some(metadata) = nodeinfo.metadata {
if let Some(upstream) = metadata.upstream {
if upstream.name == "mastodon" {
return Ok(SNS::Mastodon);
}
}
}
Err(error::Error::new_own(
Expand All @@ -108,9 +110,11 @@ pub async fn detector(url: &str) -> Result<SNS, error::Error> {
"friendica" => Ok(SNS::Friendica),
"wildebeest" => Ok(SNS::Mastodon),
_ => {
if let Some(upstream) = nodeinfo.metadata.upstream {
if upstream.name == "mastodon" {
return Ok(SNS::Mastodon);
if let Some(metadata) = nodeinfo.metadata {
if let Some(upstream) = metadata.upstream {
if upstream.name == "mastodon" {
return Ok(SNS::Mastodon);
}
}
}
Err(error::Error::new_own(
Expand All @@ -136,9 +140,11 @@ pub async fn detector(url: &str) -> Result<SNS, error::Error> {
"friendica" => Ok(SNS::Friendica),
"wildebeest" => Ok(SNS::Mastodon),
_ => {
if let Some(upstream) = nodeinfo.metadata.upstream {
if upstream.name == "mastodon" {
return Ok(SNS::Mastodon);
if let Some(metadata) = nodeinfo.metadata {
if let Some(upstream) = metadata.upstream {
if upstream.name == "mastodon" {
return Ok(SNS::Mastodon);
}
}
}
Err(error::Error::new_own(
Expand Down Expand Up @@ -171,6 +177,14 @@ mod tests {
assert_eq!(sns.unwrap(), SNS::Mastodon);
}

#[tokio::test]
async fn test_detector_mastodon_without_metadata() {
let sns = detector("https://qoto.org").await;
assert!(sns.is_ok());
assert_eq!(sns.unwrap(), SNS::Mastodon);
}


#[tokio::test]
async fn test_detector_pleroma() {
let sns = detector("https://pleroma.io").await;
Expand Down

0 comments on commit b0a50b9

Please sign in to comment.