From 1f29e9179607761e8830b3f712c0245ed4fd4b9b Mon Sep 17 00:00:00 2001 From: Felix Date: Sat, 29 Feb 2020 18:38:47 +0100 Subject: [PATCH] Various minor federation improvements --- docker/federation-test/Dockerfile | 11 ++++---- server/src/apub/community.rs | 10 +++---- server/src/apub/mod.rs | 7 ++++- server/src/apub/puller.rs | 44 +++++++++++++------------------ 4 files changed, 33 insertions(+), 39 deletions(-) diff --git a/docker/federation-test/Dockerfile b/docker/federation-test/Dockerfile index 09f3681c0..d8302ea7a 100644 --- a/docker/federation-test/Dockerfile +++ b/docker/federation-test/Dockerfile @@ -1,14 +1,15 @@ FROM ekidd/rust-musl-builder:1.38.0-openssl11 +USER root +RUN mkdir /app/dist/documentation/ -p \ + && addgroup --gid 1001 lemmy \ + && adduser --disabled-password --shell /bin/sh -u 1001 --ingroup lemmy lemmy + # Copy resources COPY server/config/defaults.hjson /app/config/defaults.hjson -COPY server/target/debug/lemmy_server /app/lemmy COPY ui/dist /app/dist +COPY server/target/debug/lemmy_server /app/lemmy -USER root -RUN mkdir /app/dist/documentation/ -RUN addgroup --gid 1001 lemmy -RUN adduser --disabled-password --shell /bin/sh -u 1001 --ingroup lemmy lemmy RUN chown lemmy:lemmy /app/ -R USER lemmy EXPOSE 8536 diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index bc07b3190..79eb68398 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -18,6 +18,7 @@ impl Community { // TODO: why the hell is this code so awkward? group.object_props.set_context_object(context()).ok(); + // TODO: id really needs to be a url group.object_props.set_id_string(self.id.to_string()).ok(); group .object_props @@ -64,17 +65,12 @@ impl Community { let connection = establish_unpooled_connection(); //As we are an object, we validated that the community id was valid + // TODO: add a method that only returns count for better performance let community_followers = CommunityFollowerView::for_community(&connection, self.id).unwrap(); - // TODO: we definitely dont want to make our follower list public, we should only expose the count - let ap_followers = community_followers - .iter() - .map(|follower| make_apub_endpoint("u", &follower.user_name)) - .collect(); - collection .collection_props - .set_items_string_vec(ap_followers) + .set_total_items_u64(community_followers.len() as u64) .unwrap(); collection } diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index 7a8b74f28..9bac64a6e 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -96,9 +96,14 @@ mod tests { pub fn make_apub_endpoint(point: S, value: T) -> String { format!( - "https://{}/federation/{}/{}", + "{}://{}/federation/{}/{}", + get_apub_protocol_string(), Settings::get().hostname, point, value ) } + +pub fn get_apub_protocol_string() -> &'static str { + "http" +} diff --git a/server/src/apub/puller.rs b/server/src/apub/puller.rs index b3177183c..3ab4c69bd 100644 --- a/server/src/apub/puller.rs +++ b/server/src/apub/puller.rs @@ -7,6 +7,7 @@ use crate::api::post::GetPosts; use crate::db::community_view::CommunityView; use crate::naive_now; use crate::settings::Settings; +use serde_json::Value; // TODO: right now all of the data is requested on demand, for production we will need to store // things in the local database to not ruin the performance @@ -32,7 +33,7 @@ pub fn get_remote_community_posts(name: String) -> Result { unimplemented!() } -pub fn get_remote_community(identifier: String) -> Result { +pub fn get_remote_community(identifier: String) -> Result { let x: Vec<&str> = identifier.split('@').collect(); let name = x[0].replace("!", ""); let instance = x[1]; @@ -48,34 +49,13 @@ pub fn get_remote_community(identifier: String) -> Result() - .unwrap(), + id: get_string_value(community.object_props.id).parse::()?, name, - title: community - .object_props - .name - .unwrap() - .as_str() - .unwrap() - .to_string(), // TODO: why does it still show !main@lemmy_beta:8541 - description: community.object_props.summary.map(|c| c.to_string()), // TODO: this has an extra quote somehow + title: get_string_value(community.object_props.name), + description: get_string_value_opt(community.object_props.summary), category_id: -1, - creator_id: community - .object_props - .attributed_to - .unwrap() - .as_str() - .unwrap() - .parse::() - .unwrap(), + creator_id: get_string_value(community.object_props.attributed_to).parse::()?, removed: false, published: naive_now(), // TODO: need to handle time conversion (or handle it in apub lib) updated: Some(naive_now()), // TODO: community.object_props.updated @@ -95,6 +75,18 @@ pub fn get_remote_community(identifier: String) -> Result) -> Option { + value + .as_ref() + .map(Value::as_str) + .flatten() + .map(str::to_string) +} + +fn get_string_value(value: Option) -> String { + get_string_value_opt(value).unwrap() +} + pub fn get_following_instances() -> Result, Error> { let instance_list = match Settings::get().federated_instance.clone() { Some(f) => vec![f, Settings::get().hostname.clone()],