Further modularization, split off WebFinger

This commit is contained in:
Natty 2023-02-26 03:51:59 +01:00
parent f8e86c74a7
commit b772833384
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
9 changed files with 104 additions and 73 deletions

10
Cargo.lock generated
View File

@ -417,6 +417,7 @@ dependencies = [
"dotenvy", "dotenvy",
"hyper", "hyper",
"magnetar_core", "magnetar_core",
"magnetar_webfinger",
"rand", "rand",
"ring", "ring",
"rsa", "rsa",
@ -451,6 +452,15 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "magnetar_webfinger"
version = "0.1.0"
dependencies = [
"magnetar_core",
"serde",
"serde_json",
]
[[package]] [[package]]
name = "matchers" name = "matchers"
version = "0.1.0" version = "0.1.0"

View File

@ -10,11 +10,13 @@ members = [
".", ".",
"ext_activity_pub", "ext_activity_pub",
"ext_nodeinfo", "ext_nodeinfo",
"ext_webfinger",
"core" "core"
] ]
[dependencies] [dependencies]
magnetar_core = { path = "./core", version = "0.1" } magnetar_core = { path = "./core", version = "0.1" }
magnetar_webfinger = { path = "./ext_webfinger", version = "0.1"}
anyhow = "1.0" anyhow = "1.0"

View File

@ -0,0 +1,69 @@
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::borrow::Cow;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Acct(String);
impl Acct {
pub fn new(uri_without_acct: Cow<'_, str>) -> Self {
Acct(uri_without_acct.to_string())
}
}
impl From<&str> for Acct {
fn from(value: &str) -> Self {
Acct(value.strip_prefix("acct:").unwrap_or(value).to_string())
}
}
impl AsRef<str> for Acct {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Serialize for Acct {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("acct:{}", self.0))
}
}
impl<'de> Deserialize<'de> for Acct {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let acct = String::deserialize(deserializer)?;
if let Some(rem) = acct.strip_prefix("acct:") {
Ok(Acct(rem.to_owned()))
} else {
Err(de::Error::custom(
"Missing acct protocol for account!".to_owned(),
))
}
}
}
#[cfg(test)]
mod test {
use crate::web_model::acct::Acct;
use serde_json::json;
#[test]
fn should_remove_acct_prefix() {
let json = json!("acct:natty@tech.lgbt");
let acct: Acct = serde_json::from_value(json).unwrap();
assert_eq!(acct, Acct::from("natty@tech.lgbt"))
}
#[test]
fn should_add_acct_prefix() {
let acct = Acct::from("natty@tech.lgbt");
let json = serde_json::to_value(acct).unwrap();
assert_eq!(json, json!("acct:natty@tech.lgbt"));
}
}

View File

@ -4,8 +4,8 @@ use serde_json::Value;
use std::fmt::Debug; use std::fmt::Debug;
use std::str::FromStr; use std::str::FromStr;
pub mod acct;
pub mod activity_streams; pub mod activity_streams;
pub mod webfinger;
trait ContentType: Serialize { trait ContentType: Serialize {
fn mime_type(&self) -> &'static str; fn mime_type(&self) -> &'static str;

9
ext_webfinger/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "magnetar_webfinger"
version = "0.1.0"
edition = "2021"
[dependencies]
magnetar_core = { path = "../core", version = "0.1" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

1
ext_webfinger/src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod webfinger;

View File

@ -1,8 +1,7 @@
use crate::web_model::content_type::{ContentActivityStreams, ContentHtml}; use magnetar_core::web_model::acct::Acct;
use crate::web_model::rel::{RelOStatusSubscribe, RelSelf, RelWebFingerProfilePage}; use magnetar_core::web_model::content_type::{ContentActivityStreams, ContentHtml};
use serde::de::Error; use magnetar_core::web_model::rel::{RelOStatusSubscribe, RelSelf, RelWebFingerProfilePage};
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Serialize};
use std::borrow::Cow;
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct WebFinger { pub struct WebFinger {
@ -40,75 +39,15 @@ pub enum WebFingerRel {
}, },
} }
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Acct(String);
impl Acct {
pub fn new(uri_without_acct: Cow<'_, str>) -> Self {
Acct(uri_without_acct.to_string())
}
}
impl From<&str> for Acct {
fn from(value: &str) -> Self {
Acct(value.strip_prefix("acct:").unwrap_or(value).to_string())
}
}
impl AsRef<str> for Acct {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Serialize for Acct {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("acct:{}", self.0))
}
}
impl<'de> Deserialize<'de> for Acct {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let acct = String::deserialize(deserializer)?;
if let Some(rem) = acct.strip_prefix("acct:") {
Ok(Acct(rem.to_owned()))
} else {
Err(Error::custom(
"Missing acct protocol for account!".to_owned(),
))
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::web_model::content_type::{ContentActivityStreams, ContentHtml}; use crate::webfinger::WebFingerSubject::Url;
use crate::web_model::rel::{RelOStatusSubscribe, RelSelf, RelWebFingerProfilePage}; use crate::webfinger::{WebFinger, WebFingerRel, WebFingerSubject};
use crate::web_model::webfinger::WebFingerSubject::Url; use magnetar_core::web_model::acct::Acct;
use crate::web_model::webfinger::{Acct, WebFinger, WebFingerRel, WebFingerSubject}; use magnetar_core::web_model::content_type::{ContentActivityStreams, ContentHtml};
use magnetar_core::web_model::rel::{RelOStatusSubscribe, RelSelf, RelWebFingerProfilePage};
use serde_json::json; use serde_json::json;
#[test]
fn should_remove_acct_prefix() {
let json = json!("acct:natty@tech.lgbt");
let acct: Acct = serde_json::from_value(json).unwrap();
assert_eq!(acct, Acct("natty@tech.lgbt".to_owned()))
}
#[test]
fn should_add_acct_prefix() {
let acct = Acct("natty@tech.lgbt".to_owned());
let json = serde_json::to_value(acct).unwrap();
assert_eq!(json, json!("acct:natty@tech.lgbt"));
}
#[test] #[test]
fn should_parse_webfinger() { fn should_parse_webfinger() {
let json = json!({ let json = json!({

View File

@ -1,7 +1,7 @@
pub mod config; pub mod config;
pub mod webfinger; pub mod webfinger;
use anyhow::{anyhow, Context}; use anyhow::anyhow;
use axum::routing::get; use axum::routing::get;
use axum::Router; use axum::Router;
use dotenvy::dotenv; use dotenvy::dotenv;

View File

@ -1,9 +1,10 @@
use axum::extract::Query; use axum::extract::Query;
use axum::http::StatusCode; use axum::http::StatusCode;
use axum::Json; use axum::Json;
use magnetar_core::web_model::acct::Acct;
use magnetar_core::web_model::content_type::{ContentActivityStreams, ContentHtml}; use magnetar_core::web_model::content_type::{ContentActivityStreams, ContentHtml};
use magnetar_core::web_model::rel::{RelOStatusSubscribe, RelSelf, RelWebFingerProfilePage}; use magnetar_core::web_model::rel::{RelOStatusSubscribe, RelSelf, RelWebFingerProfilePage};
use magnetar_core::web_model::webfinger::{Acct, WebFinger, WebFingerRel, WebFingerSubject}; use magnetar_webfinger::webfinger::{WebFinger, WebFingerRel, WebFingerSubject};
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]