Move and rename some functions

This commit is contained in:
Felix Ableitner 2020-04-03 18:32:09 +02:00
parent 75dbfeef4f
commit 786b3c7208
4 changed files with 38 additions and 40 deletions

View file

@ -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()?,
});
}

View file

@ -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};

View file

@ -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)
}

View file

@ -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)
}
}