diff options
author | Felix Ableitner <me@nutomic.com> | 2020-04-03 18:32:09 +0200 |
---|---|---|
committer | Felix Ableitner <me@nutomic.com> | 2020-04-03 18:32:09 +0200 |
commit | cb7059f832749836f865e2fcb90b089d949b8487 (patch) | |
tree | 9d6425a850a9622136df0b1de529e4d5c51fbdfd | |
parent | c16458b728396970fd5727566ed43d866dd04ee5 (diff) |
Move and rename some functions
-rw-r--r-- | server/src/api/community.rs | 6 | ||||
-rw-r--r-- | server/src/apub/community.rs | 6 | ||||
-rw-r--r-- | server/src/apub/mod.rs | 29 | ||||
-rw-r--r-- | 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 d0c4b8d3..0ba7effc 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<GetCommunityResponse> for Oper<GetCommunity> { && 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<i32> = match &data.auth { @@ -361,7 +361,7 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> { 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 37ba7c10..aa6d832f 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 8c915f71..0f08fc98 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<u8>) -> 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 6a81b8d0..915f1884 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<Vec<CommunityView>, 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<Response>(uri: &str) -> Result<Response, Error> where Response: for<'de> Deserialize<'de>, @@ -72,7 +61,7 @@ where Ok(res) } -pub fn get_remote_community_posts(identifier: &str) -> Result<GetPostsResponse, Error> { +pub fn fetch_remote_community_posts(identifier: &str) -> Result<GetPostsResponse, Error> { let community = fetch_remote_object::<GroupExt>(&get_remote_community_uri(identifier))?; let outbox_uri = &community.extension.get_outbox().to_string(); let outbox = fetch_remote_object::<OrderedCollection>(outbox_uri)?; @@ -88,7 +77,7 @@ pub fn get_remote_community_posts(identifier: &str) -> Result<GetPostsResponse, Ok(GetPostsResponse { posts: posts? }) } -pub fn get_remote_community(identifier: &str) -> Result<GetCommunityResponse, failure::Error> { +pub fn fetch_remote_community(identifier: &str) -> Result<GetCommunityResponse, failure::Error> { let group = fetch_remote_object::<GroupExt>(&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<GetCommunityResponse, fa }) } -pub fn get_following_instances() -> Vec<&'static str> { - Settings::get() - .federation - .followed_instances - .split(',') - .collect() -} - -pub fn get_all_communities() -> Result<Vec<CommunityView>, Error> { +pub fn fetch_all_communities() -> Result<Vec<CommunityView>, Error> { let mut communities_list: Vec<CommunityView> = vec![]; for instance in &get_following_instances() { match fetch_communities_from_instance(instance) { @@ -121,13 +102,3 @@ pub fn get_all_communities() -> Result<Vec<CommunityView>, 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) - } -} |