Compare commits

..

2 Commits

Author SHA1 Message Date
projectmoon 4055d5443b Add openssl build deps to the build image
continuous-integration/drone/push Build is passing Details
2024-03-28 20:33:16 +01:00
projectmoon 4ff8db7ee6 Start adding a webmention.io client for retrieving comments.
continuous-integration/drone/push Build is failing Details
continuous-integration/drone Build is passing Details
2024-03-28 20:23:31 +01:00
9 changed files with 998 additions and 595 deletions

1281
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "gemfreely"
version = "0.1.9"
version = "0.1.7"
edition = "2021"
license = "AGPL-3.0-or-later"
description = "Synchronize Gemini protocol blogs to the Fediverse"
@ -15,6 +15,7 @@ gemini-feed = "0.1.0"
germ = {version = "0.4", features = ["blocking"] }
once_cell = "1.19.0"
regex = "1.10.3"
reqwest = { version = "0.12", features = ["json"] }
tokio = {version = "1.36", features = [ "full" ] }
url = "2.5.0"
writefreely_client = "0.2.0"

View File

@ -3,3 +3,5 @@
FROM rust:1.76-slim
RUN rustup component add rustfmt
RUN cargo install --locked cargo-deny
RUN apt update && apt install -y pkg-config
RUN apt install -y libssl-dev

View File

@ -27,9 +27,8 @@ allow = [
"MPL-2.0",
"ISC",
"Unicode-DFS-2016",
"Unicode-3.0",
"OpenSSL",
"BSD-3-Clause",
"BSD-3-Clause"
]
# Some crates don't have (easily) machine readable licensing information,
@ -57,4 +56,4 @@ multiple-versions = "warn"
wildcards = "allow"
highlight = "all"
workspace-default-features = "allow"
external-default-features = "allow"
external-default-features = "allow"

View File

@ -1,3 +1,4 @@
pub(crate) mod sync;
pub(crate) mod login;
pub(crate) mod logout;
pub(crate) mod sync_webmentions;

View File

@ -0,0 +1,18 @@
use anyhow::Result;
pub(crate) struct SyncWebmentionsCommand<'a> {
webmention_io_url: &'a str,
webmention_io_token: &'a str,
}
// How will this work? This tool is stateless. The easiest solution is
// to require last ID passed in, but that doesn't really make sense.
// We can have it operate on a directory of comment files, and store
// the state in the files themselves. Replicate the logic in the nu
// shell stuff.
impl SyncWebmentionsCommand<'_> {
pub async fn execute(self) -> Result<()> {
Ok(())
}
}

View File

@ -146,11 +146,7 @@ impl Gemfeed {
settings: &GemfeedParserSettings,
) -> Result<Gemfeed> {
if let Some(content) = resp.content() {
let feed = match content.parse::<AtomFeed>() {
Ok(feed) => feed,
Err(_) => return Err(anyhow!("Could not parse Atom feed")),
};
let feed = content.parse::<AtomFeed>()?;
let entries = parse_atom(&feed, settings)?;
let title = feed.title();
Ok(Self::new(url, title, entries))
@ -249,7 +245,7 @@ impl GemfeedEntry {
slug: self.slug,
published: self.published,
url: self.url,
body: OnceCell::from(body),
body: OnceCell::from(body)
}
}

View File

@ -4,6 +4,7 @@ use commands::{login::LoginCommand, logout::LogoutCommand};
use anyhow::Result;
mod webmentions;
mod gemfeed;
mod sanitization;
mod wf;

274
src/webmentions.rs Normal file
View File

@ -0,0 +1,274 @@
use anyhow::{anyhow, Result};
use chrono::{DateTime, FixedOffset};
use url::Url;
const WEBMENTIONS_IO_ENDPOINT: &'static str = "/api/mentions.jf2";
trait ToQueryPair<T> {
fn to_query_pair(&self) -> T;
}
pub(crate) enum WebmentionsSince {
SinceId(usize),
SinceDate(DateTime<FixedOffset>),
}
impl ToQueryPair<(String, String)> for WebmentionsSince {
fn to_query_pair(&self) -> (String, String) {
match self {
Self::SinceId(id) => ("since_id".to_string(), id.to_string()),
Self::SinceDate(date) => ("since".to_string(), format!("{}", date.format("%FT%T%z"))),
}
}
}
pub(crate) enum WebmentionType {
InReplyTo,
LikeOf,
RepostOf,
BookmarkOf,
MentionOf,
Rsvp,
}
impl ToString for WebmentionType {
fn to_string(&self) -> String {
match self {
Self::InReplyTo => "in-reply-to".to_string(),
Self::LikeOf => "like-of".to_string(),
Self::RepostOf => "repost-of".to_string(),
Self::BookmarkOf => "bookmark-of".to_string(),
Self::MentionOf => "mention-of".to_string(),
Self::Rsvp => "rsvp".to_string(),
}
}
}
impl ToQueryPair<Vec<(String, String)>> for Vec<WebmentionType> {
fn to_query_pair(&self) -> Vec<(String, String)> {
self.iter()
.map(|mention_type| ("wm-property[]".to_string(), mention_type.to_string()))
.collect()
}
}
impl<'a> ToQueryPair<Vec<(String, String)>> for &'a [WebmentionType] {
fn to_query_pair(&self) -> Vec<(String, String)> {
self.iter()
.map(|mention_type| ("wm-property[]".to_string(), mention_type.to_string()))
.collect()
}
}
impl ToQueryPair<(String, String)> for WebmentionType {
fn to_query_pair(&self) -> (String, String) {
("wm-property".to_string(), self.to_string())
}
}
enum NumWebmentionTypes<'a> {
Single(&'a WebmentionType),
Multiple(&'a [WebmentionType]),
Zero,
}
pub(crate) struct GetWebmentionsRequest {
/// If specified, retrieve webmentions since an ID or date/time.
/// If not specified, fetch all possible webmentions from the
/// server.
since: Option<WebmentionsSince>,
/// If specified, fetch only these types of web mentions. An empty
/// vec will result in no webmentions fetched. If not specified,
/// fetch all kinds of webmentions.
types: Option<Vec<WebmentionType>>,
}
impl GetWebmentionsRequest {
fn types(&self) -> Option<NumWebmentionTypes> {
self.types.as_ref().map(|types| {
if types.len() > 1 {
NumWebmentionTypes::Multiple(types.as_slice())
} else if types.len() == 1 {
NumWebmentionTypes::Single(types.first().unwrap())
} else {
NumWebmentionTypes::Zero
}
})
}
}
fn create_querystring(req: &GetWebmentionsRequest) -> Result<String> {
let mut query_pairs: Vec<String> = vec![];
if let Some((key, value)) = req.since.as_ref().map(|s| s.to_query_pair()) {
query_pairs.push(format!("{}={}", &key, &value));
}
if let Some(num_types) = req.types() {
let pairs = match num_types {
NumWebmentionTypes::Multiple(types) => types.to_query_pair(),
NumWebmentionTypes::Single(wm_type) => vec![wm_type.to_query_pair()],
_ => {
return Err(anyhow!(
"Webmention types filter specified, but no types given"
))
}
};
for (key, value) in pairs {
query_pairs.push(format!("{}={}", &key, &value));
}
}
if query_pairs.len() > 1 {
Ok(query_pairs.join("&"))
} else if query_pairs.len() == 1 {
Ok(query_pairs.swap_remove(0))
} else {
Ok("".to_string())
}
}
fn create_request_url(base_url: &Url, req: &GetWebmentionsRequest) -> Result<Url> {
let mut url = base_url.join(WEBMENTIONS_IO_ENDPOINT)?;
let mut querystring = create_querystring(req)?;
url.set_query(Some(&querystring));
Ok(url)
}
pub(crate) struct WebmentionIoClient {
url: Url,
domain: String,
}
impl WebmentionIoClient {
pub async fn get_mentions(params: GetWebmentionsRequest) {
//
}
}
#[cfg(test)]
mod create_querystring_tests {
use super::*;
#[test]
fn create_querystring_with_since_date() -> Result<()> {
let date = DateTime::parse_from_str("2022-03-08T13:05:27-0100", "%FT%T%z")?;
let req = GetWebmentionsRequest {
since: Some(WebmentionsSince::SinceDate(date)),
types: None,
};
let expected = "since=2022-03-08T13:05:27-0100";
let querystring = create_querystring(&req)?;
assert_eq!(querystring.as_str(), expected);
Ok(())
}
#[test]
fn create_querystring_with_since_id() -> Result<()> {
let req = GetWebmentionsRequest {
since: Some(WebmentionsSince::SinceId(12345)),
types: None,
};
let expected = "since_id=12345";
let querystring = create_querystring(&req)?;
assert_eq!(querystring, expected);
Ok(())
}
#[test]
fn create_querystring_with_one_type() -> Result<()> {
let req = GetWebmentionsRequest {
since: None,
types: Some(vec![WebmentionType::InReplyTo]),
};
let expected = "wm-property=in-reply-to";
let querystring = create_querystring(&req)?;
assert_eq!(querystring, expected);
Ok(())
}
#[test]
fn create_querystring_with_since_and_one_type() -> Result<()> {
let req = GetWebmentionsRequest {
since: Some(WebmentionsSince::SinceId(12345)),
types: Some(vec![WebmentionType::InReplyTo]),
};
let expected = "since_id=12345&wm-property=in-reply-to";
let querystring = create_querystring(&req)?;
assert_eq!(querystring, expected);
Ok(())
}
#[test]
fn create_querystring_with_mutiple_types() -> Result<()> {
let req = GetWebmentionsRequest {
since: None,
types: Some(vec![WebmentionType::InReplyTo, WebmentionType::BookmarkOf]),
};
let expected = "wm-property[]=in-reply-to&wm-property[]=bookmark-of";
let querystring = create_querystring(&req)?;
assert_eq!(querystring, expected);
Ok(())
}
#[test]
fn create_querystring_with_since_and_mutiple_types() -> Result<()> {
let req = GetWebmentionsRequest {
since: Some(WebmentionsSince::SinceId(12345)),
types: Some(vec![WebmentionType::InReplyTo, WebmentionType::BookmarkOf]),
};
let expected = "since_id=12345&wm-property[]=in-reply-to&wm-property[]=bookmark-of";
let querystring = create_querystring(&req)?;
assert_eq!(querystring, expected);
Ok(())
}
#[test]
fn create_querystring_with_no_types_in_filter() -> Result<()> {
let req = GetWebmentionsRequest {
since: None,
types: Some(vec![]),
};
let querystring = create_querystring(&req);
assert!(matches!(querystring, Err(_)));
Ok(())
}
}
#[cfg(test)]
mod create_request_url_tests {
use super::*;
#[test]
fn test_create_url_with_since_date() -> Result<()> {
let base_url = Url::parse("https://webmention.io")?;
let date = DateTime::parse_from_str("2022-03-08T13:05:27-0100", "%FT%T%z")?;
let req = GetWebmentionsRequest {
since: Some(WebmentionsSince::SinceDate(date)),
types: None,
};
let expected = "https://webmention.io/api/mentions.jf2?since=2022-03-08T13:05:27-0100";
let api_url = create_request_url(&base_url, &req)?;
assert_eq!(api_url.as_str(), expected);
Ok(())
}
}