Use instance struct instead of raw string

This commit is contained in:
Felix Ableitner 2020-04-08 18:22:44 +02:00
parent 6962b9c433
commit 5706b533fd
2 changed files with 21 additions and 14 deletions

View File

@ -16,6 +16,17 @@ type PersonExt = Ext<Person, ApActorProperties>;
static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json"; static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json";
pub enum EndpointType {
Community,
User,
Post,
Comment,
}
pub struct Instance {
domain: String,
}
fn create_apub_response<T>(json: &T) -> HttpResponse<Body> fn create_apub_response<T>(json: &T) -> HttpResponse<Body>
where where
T: serde::ser::Serialize, T: serde::ser::Serialize,
@ -25,13 +36,6 @@ where
.json(json) .json(json)
} }
pub enum EndpointType {
Community,
User,
Post,
Comment,
}
// TODO: we will probably need to change apub endpoint urls so that html and activity+json content // TODO: we will probably need to change apub endpoint urls so that html and activity+json content
// types are handled at the same endpoint, so that you can copy the url into mastodon search // types are handled at the same endpoint, so that you can copy the url into mastodon search
// and have it fetch the object. // and have it fetch the object.
@ -93,10 +97,13 @@ pub fn format_community_name(name: &str, instance: &str) -> String {
} }
} }
pub fn get_following_instances() -> Vec<&'static str> { pub fn get_following_instances() -> Vec<Instance> {
Settings::get() Settings::get()
.federation .federation
.followed_instances .followed_instances
.split(',') .split(',')
.map(|i| Instance {
domain: i.to_string(),
})
.collect() .collect()
} }

View File

@ -17,11 +17,11 @@ use serde::Deserialize;
use std::time::Duration; use std::time::Duration;
use url::Url; use url::Url;
fn fetch_node_info(domain: &str) -> Result<NodeInfo, Error> { fn fetch_node_info(instance: &Instance) -> Result<NodeInfo, Error> {
let well_known_uri = Url::parse(&format!( let well_known_uri = Url::parse(&format!(
"{}://{}/.well-known/nodeinfo", "{}://{}/.well-known/nodeinfo",
get_apub_protocol_string(), get_apub_protocol_string(),
domain instance.domain
))?; ))?;
let well_known = fetch_remote_object::<NodeInfoWellKnown>(&well_known_uri)?; let well_known = fetch_remote_object::<NodeInfoWellKnown>(&well_known_uri)?;
Ok(fetch_remote_object::<NodeInfo>(&well_known.links.href)?) Ok(fetch_remote_object::<NodeInfo>(&well_known.links.href)?)
@ -75,17 +75,17 @@ where
} }
fn fetch_remote_community_posts( fn fetch_remote_community_posts(
instance: &str, instance: &Instance,
community: &Community, community: &Community,
conn: &PgConnection, conn: &PgConnection,
) -> Result<Vec<Post>, Error> { ) -> Result<Vec<Post>, Error> {
// TODO: need to add outbox field to Community
let endpoint = Url::parse(&format!( let endpoint = Url::parse(&format!(
"http://{}/federation/c/{}", "http://{}/federation/c/{}",
instance, community.name instance.domain, community.name
))?; ))?;
let group = fetch_remote_object::<GroupExt>(&endpoint)?; let group = fetch_remote_object::<GroupExt>(&endpoint)?;
let outbox_uri = Url::parse(&group.extension.get_outbox().to_string())?; let outbox_uri = Url::parse(&group.extension.get_outbox().to_string())?;
// TODO: outbox url etc should be stored in local db
let outbox = fetch_remote_object::<OrderedCollection>(&outbox_uri)?; let outbox = fetch_remote_object::<OrderedCollection>(&outbox_uri)?;
let items = outbox.collection_props.get_many_items_base_boxes(); let items = outbox.collection_props.get_many_items_base_boxes();
@ -134,7 +134,7 @@ pub fn fetch_all(conn: &PgConnection) -> Result<(), Error> {
} else { } else {
warn!( warn!(
"{} is not a Lemmy instance, federation is not supported", "{} is not a Lemmy instance, federation is not supported",
instance instance.domain
); );
} }
} }