Generate XsdAnyUri rather than String, remove XsdString
This commit is contained in:
+53
-16
@@ -1,4 +1,5 @@
|
||||
use crate::{data::ActorCache, error::MyError, middleware::MyVerify, requests::Requests};
|
||||
use activitystreams_new::primitives::XsdAnyUri;
|
||||
use config::Environment;
|
||||
use http_signature_normalization_actix::prelude::{VerifyDigest, VerifySignature};
|
||||
use sha2::{Digest, Sha256};
|
||||
@@ -6,7 +7,7 @@ use std::net::IpAddr;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone, Debug, serde::Deserialize)]
|
||||
pub struct Config {
|
||||
pub struct ParsedConfig {
|
||||
hostname: String,
|
||||
addr: IpAddr,
|
||||
port: u16,
|
||||
@@ -20,6 +21,21 @@ pub struct Config {
|
||||
max_connections: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Config {
|
||||
hostname: String,
|
||||
addr: IpAddr,
|
||||
port: u16,
|
||||
debug: bool,
|
||||
whitelist_mode: bool,
|
||||
validate_signatures: bool,
|
||||
database_url: String,
|
||||
pretty_log: bool,
|
||||
publish_blocks: bool,
|
||||
max_connections: usize,
|
||||
base_uri: XsdAnyUri,
|
||||
}
|
||||
|
||||
pub enum UrlKind {
|
||||
Activity,
|
||||
Actor,
|
||||
@@ -49,7 +65,24 @@ impl Config {
|
||||
.set_default("max_connections", 2)?
|
||||
.merge(Environment::new())?;
|
||||
|
||||
Ok(config.try_into()?)
|
||||
let config: ParsedConfig = config.try_into()?;
|
||||
|
||||
let scheme = if config.https { "https" } else { "http" };
|
||||
let base_uri = format!("{}://{}", scheme, config.hostname).parse()?;
|
||||
|
||||
Ok(Config {
|
||||
hostname: config.hostname,
|
||||
addr: config.addr,
|
||||
port: config.port,
|
||||
debug: config.debug,
|
||||
whitelist_mode: config.whitelist_mode,
|
||||
validate_signatures: config.validate_signatures,
|
||||
database_url: config.database_url,
|
||||
pretty_log: config.pretty_log,
|
||||
publish_blocks: config.publish_blocks,
|
||||
max_connections: config.max_connections,
|
||||
base_uri,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn pretty_log(&self) -> bool {
|
||||
@@ -124,22 +157,26 @@ impl Config {
|
||||
"https://git.asonix.dog/asonix/ap-relay".to_owned()
|
||||
}
|
||||
|
||||
pub fn generate_url(&self, kind: UrlKind) -> String {
|
||||
let scheme = if self.https { "https" } else { "http" };
|
||||
pub fn generate_url(&self, kind: UrlKind) -> XsdAnyUri {
|
||||
let mut uri = self.base_uri.clone();
|
||||
let url = uri.as_url_mut();
|
||||
|
||||
match kind {
|
||||
UrlKind::Activity => {
|
||||
format!("{}://{}/activity/{}", scheme, self.hostname, Uuid::new_v4())
|
||||
UrlKind::Activity => url.set_path(&format!("activity/{}", Uuid::new_v4())),
|
||||
UrlKind::Actor => url.set_path("actor"),
|
||||
UrlKind::Followers => url.set_path("followers"),
|
||||
UrlKind::Following => url.set_path("following"),
|
||||
UrlKind::Inbox => url.set_path("inbox"),
|
||||
UrlKind::Index => (),
|
||||
UrlKind::MainKey => {
|
||||
url.set_path("actor");
|
||||
url.set_fragment(Some("main-key"));
|
||||
}
|
||||
UrlKind::Actor => format!("{}://{}/actor", scheme, self.hostname),
|
||||
UrlKind::Followers => format!("{}://{}/followers", scheme, self.hostname),
|
||||
UrlKind::Following => format!("{}://{}/following", scheme, self.hostname),
|
||||
UrlKind::Inbox => format!("{}://{}/inbox", scheme, self.hostname),
|
||||
UrlKind::Index => format!("{}://{}/", scheme, self.hostname),
|
||||
UrlKind::MainKey => format!("{}://{}/actor#main-key", scheme, self.hostname),
|
||||
UrlKind::Media(uuid) => format!("{}://{}/media/{}", scheme, self.hostname, uuid),
|
||||
UrlKind::NodeInfo => format!("{}://{}/nodeinfo/2.0.json", scheme, self.hostname),
|
||||
UrlKind::Outbox => format!("{}://{}/outbox", scheme, self.hostname),
|
||||
}
|
||||
UrlKind::Media(uuid) => url.set_path(&format!("media/{}", uuid)),
|
||||
UrlKind::NodeInfo => url.set_path("nodeinfo/2.0.json"),
|
||||
UrlKind::Outbox => url.set_path("outbox"),
|
||||
};
|
||||
|
||||
uri
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -38,7 +38,7 @@ impl State {
|
||||
|
||||
pub fn requests(&self) -> Requests {
|
||||
Requests::new(
|
||||
self.config.generate_url(UrlKind::MainKey),
|
||||
self.config.generate_url(UrlKind::MainKey).to_string(),
|
||||
self.private_key.clone(),
|
||||
format!(
|
||||
"Actix Web 3.0.0-alpha.1 ({}/{}; +{})",
|
||||
|
||||
@@ -23,7 +23,7 @@ impl Announce {
|
||||
}
|
||||
|
||||
async fn perform(self, state: JobState) -> Result<(), anyhow::Error> {
|
||||
let activity_id: XsdAnyUri = state.config.generate_url(UrlKind::Activity).parse()?;
|
||||
let activity_id = state.config.generate_url(UrlKind::Activity);
|
||||
|
||||
let announce = generate_announce(&state.config, &activity_id, &self.object_id)?;
|
||||
let inboxes = get_inboxes(&state.state, &self.actor, &self.object_id).await?;
|
||||
@@ -42,10 +42,7 @@ fn generate_announce(
|
||||
activity_id: &XsdAnyUri,
|
||||
object_id: &XsdAnyUri,
|
||||
) -> Result<AsAnnounce, MyError> {
|
||||
let announce = AsAnnounce::new(
|
||||
config.generate_url(UrlKind::Actor).parse::<XsdAnyUri>()?,
|
||||
object_id.clone(),
|
||||
);
|
||||
let announce = AsAnnounce::new(config.generate_url(UrlKind::Actor), object_id.clone());
|
||||
|
||||
prepare_activity(
|
||||
announce,
|
||||
|
||||
@@ -33,7 +33,7 @@ impl Follow {
|
||||
if !self.is_listener {
|
||||
state.db.add_listener(self.actor.inbox.clone()).await?;
|
||||
}
|
||||
let my_id: XsdAnyUri = state.config.generate_url(UrlKind::Actor).parse()?;
|
||||
let my_id = state.config.generate_url(UrlKind::Actor);
|
||||
|
||||
// if following relay directly, not just following 'public', followback
|
||||
if self.input.object_is(&my_id) && !state.actors.is_following(&self.actor.id).await {
|
||||
|
||||
@@ -57,7 +57,7 @@ fn generate_undo_follow(
|
||||
) -> Result<AsUndo, MyError> {
|
||||
let mut follow = AsFollow::new(my_id.clone(), actor_id.clone());
|
||||
|
||||
follow.set_id(config.generate_url(UrlKind::Activity).parse()?);
|
||||
follow.set_id(config.generate_url(UrlKind::Activity));
|
||||
|
||||
let undo = AsUndo::new(my_id.clone(), follow.into_any_base()?);
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ use crate::{
|
||||
data::Actor,
|
||||
jobs::{apub::generate_undo_follow, Deliver, JobState},
|
||||
};
|
||||
use activitystreams_new::primitives::XsdAnyUri;
|
||||
use background_jobs::ActixJob;
|
||||
use std::{future::Future, pin::Pin};
|
||||
|
||||
@@ -16,7 +15,7 @@ impl Reject {
|
||||
state.db.remove_listener(self.0.inbox.clone()).await?;
|
||||
}
|
||||
|
||||
let my_id: XsdAnyUri = state.config.generate_url(UrlKind::Actor).parse()?;
|
||||
let my_id = state.config.generate_url(UrlKind::Actor);
|
||||
let undo = generate_undo_follow(&state.config, &self.0.id, &my_id)?;
|
||||
|
||||
state.job_server.queue(Deliver::new(self.0.inbox, undo)?)?;
|
||||
|
||||
@@ -4,7 +4,6 @@ use crate::{
|
||||
data::Actor,
|
||||
jobs::{apub::generate_undo_follow, Deliver, JobState},
|
||||
};
|
||||
use activitystreams_new::primitives::XsdAnyUri;
|
||||
use background_jobs::ActixJob;
|
||||
use std::{future::Future, pin::Pin};
|
||||
|
||||
@@ -27,7 +26,7 @@ impl Undo {
|
||||
}
|
||||
|
||||
if was_following {
|
||||
let my_id: XsdAnyUri = state.config.generate_url(UrlKind::Actor).parse()?;
|
||||
let my_id = state.config.generate_url(UrlKind::Actor);
|
||||
let undo = generate_undo_follow(&state.config, &self.actor.id, &my_id)?;
|
||||
state
|
||||
.job_server
|
||||
|
||||
@@ -48,11 +48,11 @@ impl QueryInstance {
|
||||
|
||||
if let Some(mut contact) = instance.contact {
|
||||
let uuid = if let Some(uuid) = state.media.get_uuid(&contact.avatar).await? {
|
||||
contact.avatar = state.config.generate_url(UrlKind::Media(uuid)).parse()?;
|
||||
contact.avatar = state.config.generate_url(UrlKind::Media(uuid));
|
||||
uuid
|
||||
} else {
|
||||
let uuid = state.media.store_url(&contact.avatar).await?;
|
||||
contact.avatar = state.config.generate_url(UrlKind::Media(uuid)).parse()?;
|
||||
contact.avatar = state.config.generate_url(UrlKind::Media(uuid));
|
||||
uuid
|
||||
};
|
||||
|
||||
|
||||
@@ -36,9 +36,9 @@ impl Resolver for RelayResolver {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let mut wf = Webfinger::new(&config.generate_resource());
|
||||
wf.add_alias(&config.generate_url(UrlKind::Actor))
|
||||
.add_activitypub(&config.generate_url(UrlKind::Actor))
|
||||
let mut wf = Webfinger::new(config.generate_resource().as_str());
|
||||
wf.add_alias(config.generate_url(UrlKind::Actor).as_str())
|
||||
.add_activitypub(config.generate_url(UrlKind::Actor).as_str())
|
||||
.add_magic_public_key(&state.public_key.as_magic_public_key());
|
||||
|
||||
Ok(Some(wf))
|
||||
|
||||
+11
-23
@@ -10,7 +10,6 @@ use activitystreams_new::{
|
||||
actor::{ApActor, Application, Endpoints},
|
||||
context,
|
||||
prelude::*,
|
||||
primitives::{XsdAnyUri, XsdString},
|
||||
security,
|
||||
};
|
||||
use actix_web::{web, Responder};
|
||||
@@ -21,39 +20,28 @@ pub async fn route(
|
||||
config: web::Data<Config>,
|
||||
) -> Result<impl Responder, MyError> {
|
||||
let mut application = Ext1::new(
|
||||
ApActor::new(
|
||||
config.generate_url(UrlKind::Inbox).parse()?,
|
||||
Application::new(),
|
||||
),
|
||||
ApActor::new(config.generate_url(UrlKind::Inbox), Application::new()),
|
||||
PublicKey {
|
||||
public_key: PublicKeyInner {
|
||||
id: config.generate_url(UrlKind::MainKey).parse()?,
|
||||
owner: config.generate_url(UrlKind::Actor).parse()?,
|
||||
id: config.generate_url(UrlKind::MainKey),
|
||||
owner: config.generate_url(UrlKind::Actor),
|
||||
public_key_pem: state.public_key.to_pem_pkcs8()?,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
application
|
||||
.set_id(config.generate_url(UrlKind::Actor).parse()?)
|
||||
.set_summary(XsdString::from("AodeRelay bot"))
|
||||
.set_name(XsdString::from("AodeRelay"))
|
||||
.set_url(config.generate_url(UrlKind::Actor).parse::<XsdAnyUri>()?)
|
||||
.set_id(config.generate_url(UrlKind::Actor))
|
||||
.set_summary("AodeRelay bot".to_owned())
|
||||
.set_name("AodeRelay".to_owned())
|
||||
.set_url(config.generate_url(UrlKind::Actor))
|
||||
.set_many_contexts(vec![context(), security()])
|
||||
.set_preferred_username("relay".into())
|
||||
.set_outbox(config.generate_url(UrlKind::Outbox).parse()?)
|
||||
.set_followers(
|
||||
config
|
||||
.generate_url(UrlKind::Followers)
|
||||
.parse::<XsdAnyUri>()?,
|
||||
)
|
||||
.set_following(
|
||||
config
|
||||
.generate_url(UrlKind::Following)
|
||||
.parse::<XsdAnyUri>()?,
|
||||
)
|
||||
.set_outbox(config.generate_url(UrlKind::Outbox))
|
||||
.set_followers(config.generate_url(UrlKind::Followers))
|
||||
.set_following(config.generate_url(UrlKind::Following))
|
||||
.set_endpoints(Endpoints {
|
||||
shared_inbox: Some(config.generate_url(UrlKind::Inbox).parse::<XsdAnyUri>()?),
|
||||
shared_inbox: Some(config.generate_url(UrlKind::Inbox)),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
|
||||
+4
-4
@@ -115,7 +115,7 @@ async fn handle_accept(config: &Config, input: AcceptedActivities) -> Result<(),
|
||||
));
|
||||
};
|
||||
|
||||
if !follow.actor_is(&config.generate_url(UrlKind::Actor).parse()?) {
|
||||
if !follow.actor_is(&config.generate_url(UrlKind::Actor)) {
|
||||
return Err(MyError::WrongActor(id_string(
|
||||
follow.actor().as_single_id(),
|
||||
)?));
|
||||
@@ -139,7 +139,7 @@ async fn handle_reject(
|
||||
));
|
||||
};
|
||||
|
||||
if !follow.actor_is(&config.generate_url(UrlKind::Actor).parse()?) {
|
||||
if !follow.actor_is(&config.generate_url(UrlKind::Actor)) {
|
||||
return Err(MyError::WrongActor(id_string(
|
||||
follow.actor().as_single_id(),
|
||||
)?));
|
||||
@@ -170,7 +170,7 @@ async fn handle_undo(
|
||||
}
|
||||
}
|
||||
|
||||
let my_id: XsdAnyUri = config.generate_url(UrlKind::Actor).parse()?;
|
||||
let my_id: XsdAnyUri = config.generate_url(UrlKind::Actor);
|
||||
|
||||
if !undone_object.object_is(&my_id) && !undone_object.object_is(&public()) {
|
||||
return Err(MyError::WrongActor(id_string(
|
||||
@@ -220,7 +220,7 @@ async fn handle_follow(
|
||||
actor: Actor,
|
||||
is_listener: bool,
|
||||
) -> Result<(), MyError> {
|
||||
let my_id: XsdAnyUri = config.generate_url(UrlKind::Actor).parse()?;
|
||||
let my_id: XsdAnyUri = config.generate_url(UrlKind::Actor);
|
||||
|
||||
if !input.object_is(&my_id) && !input.object_is(&public()) {
|
||||
return Err(MyError::WrongActor(id_string(
|
||||
|
||||
@@ -9,7 +9,7 @@ pub async fn well_known(config: web::Data<Config>) -> impl Responder {
|
||||
web::Json(Links {
|
||||
links: vec![Link {
|
||||
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0".to_owned(),
|
||||
href: Some(config.generate_url(UrlKind::NodeInfo)),
|
||||
href: Some(config.generate_url(UrlKind::NodeInfo).to_string()),
|
||||
template: None,
|
||||
kind: None,
|
||||
}],
|
||||
|
||||
Reference in New Issue
Block a user