2019-12-27 17:25:07 +00:00
|
|
|
extern crate reqwest;
|
|
|
|
|
|
|
|
use crate::api::community::{GetCommunityResponse, ListCommunitiesResponse};
|
2020-03-14 00:05:42 +00:00
|
|
|
use crate::api::post::GetPostsResponse;
|
2020-03-18 21:09:00 +00:00
|
|
|
use crate::apub::get_apub_protocol_string;
|
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;
|
|
|
|
use crate::naive_now;
|
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::actor::{properties::ApActorProperties, Group};
|
|
|
|
use activitystreams::collection::{OrderedCollection, UnorderedCollection};
|
|
|
|
use activitystreams::ext::Ext;
|
2020-03-14 21:03:05 +00:00
|
|
|
use activitystreams::object::ObjectBox;
|
2020-03-19 01:16:17 +00:00
|
|
|
use activitystreams::object::Page;
|
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)?;
|
|
|
|
if node_info.software.name != "lemmy" {
|
|
|
|
return Err(format_err!(
|
|
|
|
"{} is not a Lemmy instance, federation is not supported",
|
|
|
|
domain
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-12-27 17:25:07 +00:00
|
|
|
// TODO: follow pagination (seems like page count is missing?)
|
|
|
|
// TODO: see if there is any standard for discovering remote actors, so we dont have to rely on lemmy apis
|
2020-03-14 21:03:05 +00:00
|
|
|
let communities_uri = format!(
|
|
|
|
"http://{}/api/v1/communities/list?sort=Hot&local_only=true",
|
|
|
|
domain
|
|
|
|
);
|
|
|
|
let communities1 = fetch_remote_object::<ListCommunitiesResponse>(&communities_uri)?;
|
2020-01-14 15:26:19 +00:00
|
|
|
let mut communities2 = communities1.communities;
|
2019-12-27 17:25:07 +00:00
|
|
|
for c in &mut communities2 {
|
|
|
|
c.name = format_community_name(&c.name, domain);
|
|
|
|
}
|
|
|
|
Ok(communities2)
|
|
|
|
}
|
|
|
|
|
2020-03-14 12:15:23 +00:00
|
|
|
fn get_remote_community_uri(identifier: &str) -> String {
|
2020-03-14 00:05:42 +00:00
|
|
|
let x: Vec<&str> = identifier.split('@').collect();
|
|
|
|
let name = x[0].replace("!", "");
|
|
|
|
let instance = x[1];
|
|
|
|
format!("http://{}/federation/c/{}", instance, name)
|
|
|
|
}
|
|
|
|
|
2020-03-14 21:03:05 +00:00
|
|
|
fn fetch_remote_object<Response>(uri: &str) -> Result<Response, Error>
|
|
|
|
where
|
|
|
|
Response: for<'de> Deserialize<'de>,
|
|
|
|
{
|
2020-03-18 21:09:00 +00:00
|
|
|
if Settings::get().federation.tls_enabled && !uri.starts_with("https") {
|
|
|
|
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
|
|
|
|
let x: Response = reqwest::get(uri)?.json()?;
|
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
|
2020-03-14 12:15:23 +00:00
|
|
|
pub fn get_remote_community_posts(identifier: &str) -> Result<GetPostsResponse, Error> {
|
2020-03-19 01:16:17 +00:00
|
|
|
let community =
|
|
|
|
fetch_remote_object::<Ext<Group, ApActorProperties>>(&get_remote_community_uri(identifier))?;
|
|
|
|
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-03-14 00:05:42 +00:00
|
|
|
let items = outbox.collection_props.get_many_items_object_boxs();
|
2020-03-14 21:03:05 +00:00
|
|
|
|
|
|
|
let posts: Vec<PostView> = items
|
|
|
|
.unwrap()
|
|
|
|
.map(|obox: &ObjectBox| {
|
|
|
|
let page: Page = obox.clone().to_concrete::<Page>().unwrap();
|
|
|
|
PostView {
|
|
|
|
id: -1,
|
|
|
|
name: page.object_props.get_name_xsd_string().unwrap().to_string(),
|
2020-03-19 16:27:13 +00:00
|
|
|
url: page
|
|
|
|
.object_props
|
|
|
|
.get_url_xsd_any_uri()
|
|
|
|
.map(|u| u.to_string()),
|
|
|
|
body: page
|
|
|
|
.object_props
|
|
|
|
.get_content_xsd_string()
|
|
|
|
.map(|c| c.to_string()),
|
2020-03-14 21:03:05 +00:00
|
|
|
creator_id: -1,
|
|
|
|
community_id: -1,
|
|
|
|
removed: false,
|
|
|
|
locked: false,
|
2020-03-19 16:27:13 +00:00
|
|
|
published: page
|
|
|
|
.object_props
|
|
|
|
.get_published()
|
|
|
|
.unwrap()
|
|
|
|
.as_ref()
|
|
|
|
.naive_local()
|
|
|
|
.to_owned(),
|
|
|
|
updated: page
|
|
|
|
.object_props
|
|
|
|
.get_updated()
|
|
|
|
.map(|u| u.as_ref().to_owned().naive_local()),
|
2020-03-14 21:03:05 +00:00
|
|
|
deleted: false,
|
|
|
|
nsfw: false,
|
|
|
|
stickied: false,
|
|
|
|
embed_title: None,
|
|
|
|
embed_description: None,
|
|
|
|
embed_html: None,
|
|
|
|
thumbnail_url: None,
|
|
|
|
banned: false,
|
|
|
|
banned_from_community: false,
|
|
|
|
creator_name: "".to_string(),
|
|
|
|
creator_avatar: None,
|
|
|
|
community_name: "".to_string(),
|
|
|
|
community_removed: false,
|
|
|
|
community_deleted: false,
|
|
|
|
community_nsfw: false,
|
|
|
|
number_of_comments: -1,
|
|
|
|
score: -1,
|
|
|
|
upvotes: -1,
|
|
|
|
downvotes: -1,
|
|
|
|
hot_rank: -1,
|
|
|
|
newest_activity_time: naive_now(),
|
|
|
|
user_id: None,
|
|
|
|
my_vote: None,
|
|
|
|
subscribed: None,
|
|
|
|
read: None,
|
|
|
|
saved: None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Ok(GetPostsResponse { 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-03-19 01:16:17 +00:00
|
|
|
let community =
|
|
|
|
fetch_remote_object::<Ext<Group, ApActorProperties>>(&get_remote_community_uri(identifier))?;
|
|
|
|
let followers_uri = &community.extension.get_followers().unwrap().to_string();
|
|
|
|
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)?;
|
|
|
|
let followers = fetch_remote_object::<UnorderedCollection>(followers_uri)?;
|
|
|
|
// 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
|
|
|
|
|
|
|
Ok(GetCommunityResponse {
|
|
|
|
moderators: vec![],
|
|
|
|
admins: vec![],
|
|
|
|
community: CommunityView {
|
2020-02-29 02:11:39 +00:00
|
|
|
// TODO: we need to merge id and name into a single thing (stuff like @user@instance.com)
|
2020-03-14 00:05:42 +00:00
|
|
|
id: 1337, //community.object_props.get_id()
|
2020-03-14 12:15:23 +00:00
|
|
|
name: identifier.to_string(),
|
2020-03-12 00:01:25 +00:00
|
|
|
title: community
|
2020-03-19 01:16:17 +00:00
|
|
|
.as_ref()
|
2020-03-12 00:01:25 +00:00
|
|
|
.get_name_xsd_string()
|
|
|
|
.unwrap()
|
|
|
|
.to_string(),
|
|
|
|
description: community
|
2020-03-19 01:16:17 +00:00
|
|
|
.as_ref()
|
2020-03-12 00:01:25 +00:00
|
|
|
.get_summary_xsd_string()
|
|
|
|
.map(|s| s.to_string()),
|
2019-12-27 17:25:07 +00:00
|
|
|
category_id: -1,
|
2020-03-12 02:35:32 +00:00
|
|
|
creator_id: -1, //community.object_props.get_attributed_to_xsd_any_uri()
|
2019-12-27 17:25:07 +00:00
|
|
|
removed: false,
|
2020-03-12 00:01:25 +00:00
|
|
|
published: community
|
2020-03-19 01:16:17 +00:00
|
|
|
.as_ref()
|
2020-03-12 00:01:25 +00:00
|
|
|
.get_published()
|
|
|
|
.unwrap()
|
|
|
|
.as_ref()
|
|
|
|
.naive_local()
|
|
|
|
.to_owned(),
|
|
|
|
updated: community
|
2020-03-19 01:16:17 +00:00
|
|
|
.as_ref()
|
2020-03-12 00:01:25 +00:00
|
|
|
.get_updated()
|
|
|
|
.map(|u| u.as_ref().to_owned().naive_local()),
|
2019-12-27 17:25:07 +00:00
|
|
|
deleted: false,
|
|
|
|
nsfw: false,
|
|
|
|
creator_name: "".to_string(),
|
2020-01-02 18:22:23 +00:00
|
|
|
creator_avatar: None,
|
2019-12-27 17:25:07 +00:00
|
|
|
category_name: "".to_string(),
|
2020-03-12 02:35:32 +00:00
|
|
|
number_of_subscribers: *followers
|
|
|
|
.collection_props
|
|
|
|
.get_total_items()
|
|
|
|
.unwrap()
|
|
|
|
.as_ref() as i64, // TODO: need to use the same type
|
2020-03-14 00:05:42 +00:00
|
|
|
number_of_posts: *outbox.collection_props.get_total_items().unwrap().as_ref() as i64,
|
2019-12-27 17:25:07 +00:00
|
|
|
number_of_comments: -1,
|
|
|
|
hot_rank: -1,
|
|
|
|
user_id: None,
|
2020-03-11 11:29:10 +00:00
|
|
|
subscribed: None,
|
2019-12-27 17:25:07 +00:00
|
|
|
},
|
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)
|
|
|
|
}
|
|
|
|
}
|