2020-04-03 05:02:43 +00:00
|
|
|
use crate::api::community::GetCommunityResponse;
|
2020-03-14 00:05:42 +00:00
|
|
|
use crate::api::post::GetPostsResponse;
|
2020-04-03 05:02:43 +00:00
|
|
|
use crate::apub::{get_apub_protocol_string, GroupExt};
|
2019-12-27 17:25:07 +00:00
|
|
|
use crate::db::community_view::CommunityView;
|
2020-03-14 21:03:05 +00:00
|
|
|
use crate::db::post_view::PostView;
|
2020-03-18 15:08:08 +00:00
|
|
|
use crate::routes::nodeinfo::{NodeInfo, NodeInfoWellKnown};
|
2019-12-27 17:25:07 +00:00
|
|
|
use crate::settings::Settings;
|
2020-03-19 01:16:17 +00:00
|
|
|
use activitystreams::collection::{OrderedCollection, UnorderedCollection};
|
|
|
|
use activitystreams::object::Page;
|
2020-04-03 05:02:43 +00:00
|
|
|
use activitystreams::BaseBox;
|
2020-03-20 00:42:07 +00:00
|
|
|
use chttp::prelude::*;
|
2020-03-12 00:01:25 +00:00
|
|
|
use failure::Error;
|
2020-03-14 21:03:05 +00:00
|
|
|
use log::warn;
|
|
|
|
use serde::Deserialize;
|
2019-12-27 17:25:07 +00:00
|
|
|
|
2020-03-18 15:08:08 +00:00
|
|
|
fn fetch_node_info(domain: &str) -> Result<NodeInfo, Error> {
|
2020-03-18 21:09:00 +00:00
|
|
|
let well_known_uri = format!(
|
|
|
|
"{}://{}/.well-known/nodeinfo",
|
|
|
|
get_apub_protocol_string(),
|
|
|
|
domain
|
|
|
|
);
|
|
|
|
let well_known = fetch_remote_object::<NodeInfoWellKnown>(&well_known_uri)?;
|
|
|
|
Ok(fetch_remote_object::<NodeInfo>(&well_known.links.href)?)
|
2020-03-18 15:08:08 +00:00
|
|
|
}
|
2020-03-18 21:09:00 +00:00
|
|
|
|
2019-12-27 17:25:07 +00:00
|
|
|
fn fetch_communities_from_instance(domain: &str) -> Result<Vec<CommunityView>, Error> {
|
2020-03-18 15:08:08 +00:00
|
|
|
let node_info = fetch_node_info(domain)?;
|
2020-04-03 05:02:43 +00:00
|
|
|
|
|
|
|
if let Some(community_list_url) = node_info.metadata.community_list_url {
|
|
|
|
let collection = fetch_remote_object::<UnorderedCollection>(&community_list_url)?;
|
|
|
|
let object_boxes = collection
|
|
|
|
.collection_props
|
|
|
|
.get_many_items_base_boxes()
|
|
|
|
.unwrap();
|
|
|
|
let communities: Result<Vec<CommunityView>, Error> = object_boxes
|
|
|
|
.map(|c| -> Result<CommunityView, Error> {
|
|
|
|
let group = c.to_owned().to_concrete::<GroupExt>()?;
|
|
|
|
CommunityView::from_group(&group, domain)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Ok(communities?)
|
|
|
|
} else {
|
|
|
|
Err(format_err!(
|
2020-03-18 15:08:08 +00:00
|
|
|
"{} is not a Lemmy instance, federation is not supported",
|
|
|
|
domain
|
2020-04-03 05:02:43 +00:00
|
|
|
))
|
2020-03-18 15:08:08 +00:00
|
|
|
}
|
2020-04-03 05:02:43 +00:00
|
|
|
}
|
2020-03-18 15:08:08 +00:00
|
|
|
|
2020-04-03 05:02:43 +00:00
|
|
|
/// 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())
|
2019-12-27 17:25:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 12:15:23 +00:00
|
|
|
fn get_remote_community_uri(identifier: &str) -> String {
|
2020-04-03 05:02:43 +00:00
|
|
|
let (name, domain) = split_identifier(identifier);
|
|
|
|
format!("http://{}/federation/c/{}", domain, name)
|
2020-03-14 00:05:42 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 05:02:43 +00:00
|
|
|
pub fn fetch_remote_object<Response>(uri: &str) -> Result<Response, Error>
|
2020-03-14 21:03:05 +00:00
|
|
|
where
|
|
|
|
Response: for<'de> Deserialize<'de>,
|
|
|
|
{
|
2020-04-03 05:02:43 +00:00
|
|
|
if Settings::get().federation.tls_enabled && !uri.starts_with("https://") {
|
2020-03-18 21:09:00 +00:00
|
|
|
return Err(format_err!("Activitypub uri is insecure: {}", uri));
|
|
|
|
}
|
2020-03-14 21:03:05 +00:00
|
|
|
// TODO: should cache responses here when we are in production
|
|
|
|
// TODO: this function should return a future
|
2020-03-20 00:42:07 +00:00
|
|
|
let text = chttp::get(uri)?.text()?;
|
|
|
|
let res: Response = serde_json::from_str(&text)?;
|
|
|
|
Ok(res)
|
2020-03-14 21:03:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 12:15:23 +00:00
|
|
|
pub fn get_remote_community_posts(identifier: &str) -> Result<GetPostsResponse, Error> {
|
2020-04-03 05:02:43 +00:00
|
|
|
let community = fetch_remote_object::<GroupExt>(&get_remote_community_uri(identifier))?;
|
2020-03-19 01:16:17 +00:00
|
|
|
let outbox_uri = &community.extension.get_outbox().to_string();
|
2020-03-14 21:03:05 +00:00
|
|
|
let outbox = fetch_remote_object::<OrderedCollection>(outbox_uri)?;
|
2020-04-03 05:02:43 +00:00
|
|
|
let items = outbox.collection_props.get_many_items_base_boxes();
|
2020-03-14 21:03:05 +00:00
|
|
|
|
2020-04-03 05:02:43 +00:00
|
|
|
let posts: Result<Vec<PostView>, Error> = items
|
2020-03-14 21:03:05 +00:00
|
|
|
.unwrap()
|
2020-04-03 05:02:43 +00:00
|
|
|
.map(|obox: &BaseBox| {
|
|
|
|
let page = obox.clone().to_concrete::<Page>().unwrap();
|
|
|
|
PostView::from_page(&page)
|
2020-03-14 21:03:05 +00:00
|
|
|
})
|
|
|
|
.collect();
|
2020-04-03 05:02:43 +00:00
|
|
|
Ok(GetPostsResponse { posts: posts? })
|
2019-12-27 17:25:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 12:15:23 +00:00
|
|
|
pub fn get_remote_community(identifier: &str) -> Result<GetCommunityResponse, failure::Error> {
|
2020-04-03 05:02:43 +00:00
|
|
|
let group = fetch_remote_object::<GroupExt>(&get_remote_community_uri(identifier))?;
|
2020-03-14 21:03:05 +00:00
|
|
|
// TODO: this is only for testing until we can call that function from GetPosts
|
|
|
|
// (once string ids are supported)
|
|
|
|
//dbg!(get_remote_community_posts(identifier)?);
|
2019-12-27 17:25:07 +00:00
|
|
|
|
2020-04-03 05:02:43 +00:00
|
|
|
let (_, domain) = split_identifier(identifier);
|
2019-12-27 17:25:07 +00:00
|
|
|
Ok(GetCommunityResponse {
|
|
|
|
moderators: vec![],
|
|
|
|
admins: vec![],
|
2020-04-03 05:02:43 +00:00
|
|
|
community: CommunityView::from_group(&group, &domain)?,
|
2020-02-05 17:51:03 +00:00
|
|
|
online: 0,
|
2019-12-27 17:25:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-18 21:09:00 +00:00
|
|
|
pub fn get_following_instances() -> Vec<&'static str> {
|
|
|
|
Settings::get()
|
|
|
|
.federation
|
|
|
|
.followed_instances
|
|
|
|
.split(',')
|
|
|
|
.collect()
|
2019-12-27 17:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_all_communities() -> Result<Vec<CommunityView>, Error> {
|
|
|
|
let mut communities_list: Vec<CommunityView> = vec![];
|
2020-03-14 21:03:05 +00:00
|
|
|
for instance in &get_following_instances() {
|
|
|
|
match fetch_communities_from_instance(instance) {
|
|
|
|
Ok(mut c) => communities_list.append(c.as_mut()),
|
|
|
|
Err(e) => warn!("Failed to fetch instance list from remote instance: {}", e),
|
|
|
|
};
|
2019-12-27 17:25:07 +00:00
|
|
|
}
|
|
|
|
Ok(communities_list)
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:27:13 +00:00
|
|
|
/// 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.
|
2019-12-27 17:25:07 +00:00
|
|
|
pub fn format_community_name(name: &str, instance: &str) -> String {
|
|
|
|
if instance == Settings::get().hostname {
|
|
|
|
format!("!{}", name)
|
|
|
|
} else {
|
|
|
|
format!("!{}@{}", name, instance)
|
|
|
|
}
|
|
|
|
}
|