Also attempt to verify from meta
This commit is contained in:
parent
863d20a79d
commit
968fe675cd
70
src/main.rs
70
src/main.rs
|
@ -1,5 +1,8 @@
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
use fluffer::{App, AppErr, Client, Fluff};
|
use fluffer::{App, AppErr, Client, Fluff};
|
||||||
use germ::ast::{Ast as GeminiAst, Node as GemtextNode};
|
use germ::ast::{Ast as GeminiAst, Node as GemtextNode};
|
||||||
|
use germ::meta::Meta as GeminiMetadata;
|
||||||
use germ::request::request as germ_request;
|
use germ::request::request as germ_request;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
@ -17,19 +20,39 @@ enum GementionError {
|
||||||
}
|
}
|
||||||
|
|
||||||
enum VerificationStatus {
|
enum VerificationStatus {
|
||||||
Verified(String),
|
Verified {
|
||||||
|
endpoint: String,
|
||||||
|
source: VerificationSource,
|
||||||
|
},
|
||||||
|
|
||||||
NotVerified(VerificationFailureReason),
|
NotVerified(VerificationFailureReason),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for VerificationStatus {
|
impl ToString for VerificationStatus {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Self::Verified(url) => format!("verified: {}", url),
|
Self::Verified { endpoint, source } => format!("verified: {} [{}]", endpoint, source),
|
||||||
Self::NotVerified(failure) => failure.to_string(),
|
Self::NotVerified(failure) => failure.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
enum VerificationSource {
|
||||||
|
Meta,
|
||||||
|
Page,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for VerificationSource {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::Meta => write!(f, "source=meta"),
|
||||||
|
Self::Page => write!(f, "source=page"),
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
enum VerificationFailureReason {
|
enum VerificationFailureReason {
|
||||||
/// No titan link to our endpoint exists on this page.
|
/// No titan link to our endpoint exists on this page.
|
||||||
NoMentionLinkFound,
|
NoMentionLinkFound,
|
||||||
|
@ -58,25 +81,48 @@ fn is_mention_link(gemtext_link: &str) -> Result<bool, GementionError> {
|
||||||
Ok(gemtext_link.starts_with(OUR_ENDPOINT))
|
Ok(gemtext_link.starts_with(OUR_ENDPOINT))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scan_for_mentions(ast: GeminiAst) -> Vec<String> {
|
fn scan_for_mentions(meta: GeminiMetadata, ast: GeminiAst) -> (VerificationSource, Vec<String>) {
|
||||||
ast.inner()
|
// Check metadata of the page for a gemention endpoint.
|
||||||
|
println!("meta is {:?}", meta);
|
||||||
|
if let Some(endpoint) = meta.parameters().get("gemention") {
|
||||||
|
let endpoint = endpoint.trim_start_matches("=");
|
||||||
|
return (VerificationSource::Meta, vec![endpoint.to_owned()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If that fails, check the page itself for the first available
|
||||||
|
// link that matches.
|
||||||
|
let endpoints = ast
|
||||||
|
.inner()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|node| match node {
|
.filter_map(|node| match node {
|
||||||
GemtextNode::Link { ref to, .. } if is_mention_link(to).unwrap_or(false) => Some(to),
|
GemtextNode::Link { ref to, .. } if is_mention_link(to).unwrap_or(false) => Some(to),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect()
|
.collect();
|
||||||
|
|
||||||
|
(VerificationSource::Page, endpoints)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn verify_mentions<S: AsRef<str>>(expected_link: S, mentions: Vec<String>) -> VerificationStatus {
|
fn verify_mentions<S: AsRef<str>>(
|
||||||
|
expected_link: S,
|
||||||
|
source_and_mentions: (VerificationSource, Vec<String>),
|
||||||
|
) -> VerificationStatus {
|
||||||
|
let (verification_source, mentions) = source_and_mentions;
|
||||||
let expected_link = expected_link.as_ref();
|
let expected_link = expected_link.as_ref();
|
||||||
|
|
||||||
if mentions.len() > 0 {
|
if mentions.len() > 0 {
|
||||||
|
// We have links that go to our endpoint. Scan links for the
|
||||||
|
// one we expect (i.e. for the target), otherwise we say
|
||||||
|
// incorrect link.
|
||||||
mentions
|
mentions
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find_map(|link| {
|
.find_map(|link| {
|
||||||
if link == expected_link {
|
if link == expected_link {
|
||||||
Some(VerificationStatus::Verified(link))
|
Some(VerificationStatus::Verified {
|
||||||
|
source: verification_source,
|
||||||
|
endpoint: link,
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -89,18 +135,20 @@ fn verify_mentions<S: AsRef<str>>(expected_link: S, mentions: Vec<String>) -> Ve
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn verify_mention(target: &str) -> Result<VerificationStatus, GementionError> {
|
async fn verify_mention(target_page: &str) -> Result<VerificationStatus, GementionError> {
|
||||||
let url = Url::parse(&format!("gemini://{}", target))?;
|
let url = Url::parse(&format!("gemini://{}", target_page))?;
|
||||||
let expected_link = Url::parse(OUR_ENDPOINT)?.join(target)?;
|
let expected_link = Url::parse(OUR_ENDPOINT)?.join(target_page)?;
|
||||||
|
|
||||||
let resp = germ_request(&url).await?;
|
let resp = germ_request(&url).await?;
|
||||||
|
let meta = GeminiMetadata::from_string(resp.meta());
|
||||||
|
|
||||||
let content = resp
|
let content = resp
|
||||||
.content()
|
.content()
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.ok_or(GementionError::NoContentFoundForTarget)?;
|
.ok_or(GementionError::NoContentFoundForTarget)?;
|
||||||
|
|
||||||
let ast = GeminiAst::from_string(content);
|
let ast = GeminiAst::from_string(content);
|
||||||
let mentions = scan_for_mentions(ast);
|
let mentions = scan_for_mentions(meta, ast);
|
||||||
Ok(verify_mentions(expected_link, mentions))
|
Ok(verify_mentions(expected_link, mentions))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue