]> Untitled Git - lemmy.git/commitdiff
Use instance struct instead of raw string
authorFelix Ableitner <me@nutomic.com>
Wed, 8 Apr 2020 16:22:44 +0000 (18:22 +0200)
committerFelix Ableitner <me@nutomic.com>
Wed, 8 Apr 2020 16:22:44 +0000 (18:22 +0200)
server/src/apub/mod.rs
server/src/apub/puller.rs

index cbfbe735c52158e5020e3588c3247bc5a78a6dbe..c9d5e0e57fb3a767054acf219442428343d155d6 100644 (file)
@@ -16,6 +16,17 @@ type PersonExt = Ext<Person, ApActorProperties>;
 
 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>
 where
   T: serde::ser::Serialize,
@@ -25,13 +36,6 @@ where
     .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
 //       types are handled at the same endpoint, so that you can copy the url into mastodon search
 //       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()
     .federation
     .followed_instances
     .split(',')
+    .map(|i| Instance {
+      domain: i.to_string(),
+    })
     .collect()
 }
index 4dacf60fbec5ec1c61d187bcf7abec50cbecc2f2..7610d4640e0652ed6fd57d458e9df5721b59d6de 100644 (file)
@@ -17,11 +17,11 @@ use serde::Deserialize;
 use std::time::Duration;
 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!(
     "{}://{}/.well-known/nodeinfo",
     get_apub_protocol_string(),
-    domain
+    instance.domain
   ))?;
   let well_known = fetch_remote_object::<NodeInfoWellKnown>(&well_known_uri)?;
   Ok(fetch_remote_object::<NodeInfo>(&well_known.links.href)?)
@@ -75,17 +75,17 @@ where
 }
 
 fn fetch_remote_community_posts(
-  instance: &str,
+  instance: &Instance,
   community: &Community,
   conn: &PgConnection,
 ) -> Result<Vec<Post>, Error> {
+  // TODO: need to add outbox field to Community
   let endpoint = Url::parse(&format!(
     "http://{}/federation/c/{}",
-    instance, community.name
+    instance.domain, community.name
   ))?;
   let group = fetch_remote_object::<GroupExt>(&endpoint)?;
   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 items = outbox.collection_props.get_many_items_base_boxes();
 
@@ -134,7 +134,7 @@ pub fn fetch_all(conn: &PgConnection) -> Result<(), Error> {
     } else {
       warn!(
         "{} is not a Lemmy instance, federation is not supported",
-        instance
+        instance.domain
       );
     }
   }