From: Felix Ableitner Date: Sat, 14 Mar 2020 12:15:23 +0000 (+0100) Subject: Move apub related code from websocket into api package X-Git-Url: http://these/git/%7B%60https:/%7B%60css/themes/%22https:/nerdica.net/%7BpictrsAvatarThumbnail%28this.props.site.site.icon%29%7D?a=commitdiff_plain;h=5896a9d251f13e4387fe34e0f8cd21317494ec15;p=lemmy.git Move apub related code from websocket into api package --- diff --git a/server/src/api/community.rs b/server/src/api/community.rs index 7d5f8d61..0f104c2d 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -1,4 +1,6 @@ use super::*; +use crate::apub::puller::{get_all_communities, get_remote_community}; +use crate::settings::Settings; use diesel::PgConnection; use std::str::FromStr; @@ -117,6 +119,13 @@ impl Perform for Oper { fn perform(&self, conn: &PgConnection) -> Result { let data: &GetCommunity = &self.data; + if data.name.is_some() + && Settings::get().federation_enabled + && data.name.as_ref().unwrap().contains('@') + { + return get_remote_community(data.name.as_ref().unwrap()); + } + let user_id: Option = match &data.auth { Some(auth) => match Claims::decode(&auth) { Ok(claims) => { @@ -333,6 +342,12 @@ impl Perform for Oper { fn perform(&self, conn: &PgConnection) -> Result { let data: &ListCommunities = &self.data; + if Settings::get().federation_enabled { + return Ok(ListCommunitiesResponse { + communities: get_all_communities()?, + }); + } + let user_claims: Option = match &data.auth { Some(auth) => match Claims::decode(&auth) { Ok(claims) => Some(claims.claims), diff --git a/server/src/api/post.rs b/server/src/api/post.rs index cefa5b07..e19d4ee9 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -1,4 +1,5 @@ use super::*; +use crate::settings::Settings; use diesel::PgConnection; use std::str::FromStr; @@ -220,6 +221,12 @@ impl Perform for Oper { fn perform(&self, conn: &PgConnection) -> Result { let data: &GetPosts = &self.data; + if Settings::get().federation_enabled { + dbg!(&data); + // TODO: intercept here (but the type is wrong) + //get_remote_community_posts(get_posts.community_id.unwrap()) + } + let user_claims: Option = match &data.auth { Some(auth) => match Claims::decode(&auth) { Ok(claims) => Some(claims.claims), diff --git a/server/src/apub/puller.rs b/server/src/apub/puller.rs index 941fbc3f..edadb7bf 100644 --- a/server/src/apub/puller.rs +++ b/server/src/apub/puller.rs @@ -26,14 +26,14 @@ fn fetch_communities_from_instance(domain: &str) -> Result, E } // TODO: this should be cached or stored in the database -fn get_remote_community_uri(identifier: String) -> String { +fn get_remote_community_uri(identifier: &str) -> String { let x: Vec<&str> = identifier.split('@').collect(); let name = x[0].replace("!", ""); let instance = x[1]; format!("http://{}/federation/c/{}", instance, name) } -pub fn get_remote_community_posts(identifier: String) -> Result { +pub fn get_remote_community_posts(identifier: &str) -> Result { let community: Group = reqwest::get(&get_remote_community_uri(identifier))?.json()?; let outbox_uri = &community.ap_actor_props.get_outbox().to_string(); let outbox: OrderedCollection = reqwest::get(outbox_uri)?.json()?; @@ -42,8 +42,8 @@ pub fn get_remote_community_posts(identifier: String) -> Result Result { - let community: Group = reqwest::get(&get_remote_community_uri(identifier.clone()))?.json()?; +pub fn get_remote_community(identifier: &str) -> Result { + let community: Group = reqwest::get(&get_remote_community_uri(identifier))?.json()?; let followers_uri = &community .ap_actor_props .get_followers() @@ -62,7 +62,7 @@ pub fn get_remote_community(identifier: String) -> Result Result Result { let get_community: GetCommunity = serde_json::from_str(data)?; - let mut res = if Settings::get().federation_enabled { - if let Some(community_name) = get_community.name.to_owned() { - if community_name.contains('@') { - // TODO: need to support sort, filter etc for remote communities - // TODO: need to to this for http api as well - get_remote_community(community_name)? - } else { - Oper::new(get_community).perform(&conn)? - } - } else { - Oper::new(get_community).perform(&conn)? - } - } else { - Oper::new(get_community).perform(&conn)? - }; + let mut res = Oper::new(get_community).perform(&conn)?; let community_id = res.community.id; @@ -581,13 +563,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { - if Settings::get().federation_enabled { - let res = get_all_communities()?; - let val = ListCommunitiesResponse { communities: res }; - to_json_string(&user_operation, &val) - } else { - do_user_operation::(user_operation, data, &conn) - } + do_user_operation::(user_operation, data, &conn) } UserOperation::CreateCommunity => { chat.check_rate_limit_register(msg.id, true)?; @@ -648,9 +624,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { let get_posts: GetPosts = serde_json::from_str(data)?; - dbg!(&get_posts); - // TODO: intercept here (but the type is wrong) - //get_remote_community_posts(get_posts.community_id.unwrap()) + if get_posts.community_id.is_none() { // 0 is the "all" community chat.join_community_room(0, msg.id);