From 786b3c72083a253d12fdeadb93794a1afb8c14ec Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 3 Apr 2020 18:32:09 +0200 Subject: [PATCH] Move and rename some functions --- server/src/api/community.rs | 6 +++--- server/src/apub/community.rs | 6 ++---- server/src/apub/mod.rs | 29 ++++++++++++++++++++++++++++ server/src/apub/puller.rs | 37 ++++-------------------------------- 4 files changed, 38 insertions(+), 40 deletions(-) diff --git a/server/src/api/community.rs b/server/src/api/community.rs index d0c4b8d3e..0ba7effc9 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -1,5 +1,5 @@ use super::*; -use crate::apub::puller::{get_all_communities, get_remote_community}; +use crate::apub::puller::{fetch_all_communities, fetch_remote_community}; use crate::apub::{gen_keypair_str, make_apub_endpoint, EndpointType}; use crate::settings::Settings; use diesel::PgConnection; @@ -125,7 +125,7 @@ impl Perform for Oper { && Settings::get().federation.enabled && data.name.as_ref().unwrap().contains('@') { - return get_remote_community(data.name.as_ref().unwrap()); + return fetch_remote_community(data.name.as_ref().unwrap()); } let user_id: Option = match &data.auth { @@ -361,7 +361,7 @@ impl Perform for Oper { let local_only = data.local_only.unwrap_or(false); if Settings::get().federation.enabled && !local_only { return Ok(ListCommunitiesResponse { - communities: get_all_communities()?, + communities: fetch_all_communities()?, }); } diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 37ba7c100..aa6d832f4 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -1,7 +1,5 @@ -use crate::apub::puller::{fetch_remote_object, format_community_name}; -use crate::apub::{ - create_apub_response, get_apub_protocol_string, make_apub_endpoint, EndpointType, GroupExt, -}; +use crate::apub::puller::fetch_remote_object; +use crate::apub::*; use crate::convert_datetime; use crate::db::community::Community; use crate::db::community_view::{CommunityFollowerView, CommunityView}; diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index 8c915f710..0f08fc988 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -74,3 +74,32 @@ pub fn gen_keypair_str() -> (String, String) { fn vec_bytes_to_str(bytes: Vec) -> String { String::from_utf8_lossy(&bytes).into_owned() } + +/// If community is on local instance, don't include the @instance part. This is only for displaying +/// to the user and should never be used otherwise. +pub fn format_community_name(name: &str, instance: &str) -> String { + if instance == Settings::get().hostname { + format!("!{}", name) + } else { + format!("!{}@{}", name, instance) + } +} + +pub fn get_following_instances() -> Vec<&'static str> { + Settings::get() + .federation + .followed_instances + .split(',') + .collect() +} + +/// Returns a tuple of (username, domain) from an identifier like "main@dev.lemmy.ml" +fn split_identifier(identifier: &str) -> (String, String) { + let x: Vec<&str> = identifier.split('@').collect(); + (x[0].replace("!", ""), x[1].to_string()) +} + +fn get_remote_community_uri(identifier: &str) -> String { + let (name, domain) = split_identifier(identifier); + format!("http://{}/federation/c/{}", domain, name) +} diff --git a/server/src/apub/puller.rs b/server/src/apub/puller.rs index 6a81b8d06..915f1884c 100644 --- a/server/src/apub/puller.rs +++ b/server/src/apub/puller.rs @@ -1,6 +1,6 @@ use crate::api::community::GetCommunityResponse; use crate::api::post::GetPostsResponse; -use crate::apub::{get_apub_protocol_string, GroupExt}; +use crate::apub::*; use crate::db::community_view::CommunityView; use crate::db::post_view::PostView; use crate::routes::nodeinfo::{NodeInfo, NodeInfoWellKnown}; @@ -47,17 +47,6 @@ fn fetch_communities_from_instance(domain: &str) -> Result, E } } -/// Returns a tuple of (username, domain) from an identifier like "main@dev.lemmy.ml" -fn split_identifier(identifier: &str) -> (String, String) { - let x: Vec<&str> = identifier.split('@').collect(); - (x[0].replace("!", ""), x[1].to_string()) -} - -fn get_remote_community_uri(identifier: &str) -> String { - let (name, domain) = split_identifier(identifier); - format!("http://{}/federation/c/{}", domain, name) -} - pub fn fetch_remote_object(uri: &str) -> Result where Response: for<'de> Deserialize<'de>, @@ -72,7 +61,7 @@ where Ok(res) } -pub fn get_remote_community_posts(identifier: &str) -> Result { +pub fn fetch_remote_community_posts(identifier: &str) -> Result { let community = fetch_remote_object::(&get_remote_community_uri(identifier))?; let outbox_uri = &community.extension.get_outbox().to_string(); let outbox = fetch_remote_object::(outbox_uri)?; @@ -88,7 +77,7 @@ pub fn get_remote_community_posts(identifier: &str) -> Result Result { +pub fn fetch_remote_community(identifier: &str) -> Result { let group = fetch_remote_object::(&get_remote_community_uri(identifier))?; // TODO: this is only for testing until we can call that function from GetPosts // (once string ids are supported) @@ -103,15 +92,7 @@ pub fn get_remote_community(identifier: &str) -> Result Vec<&'static str> { - Settings::get() - .federation - .followed_instances - .split(',') - .collect() -} - -pub fn get_all_communities() -> Result, Error> { +pub fn fetch_all_communities() -> Result, Error> { let mut communities_list: Vec = vec![]; for instance in &get_following_instances() { match fetch_communities_from_instance(instance) { @@ -121,13 +102,3 @@ pub fn get_all_communities() -> Result, Error> { } Ok(communities_list) } - -/// If community is on local instance, don't include the @instance part. This is only for displaying -/// to the user and should never be used otherwise. -pub fn format_community_name(name: &str, instance: &str) -> String { - if instance == Settings::get().hostname { - format!("!{}", name) - } else { - format!("!{}@{}", name, instance) - } -}