2020-04-03 16:32:09 +00:00
|
|
|
use crate::apub::*;
|
2020-04-07 16:47:19 +00:00
|
|
|
use crate::db::community::{Community, CommunityForm};
|
|
|
|
use crate::db::post::{Post, PostForm};
|
2020-04-07 21:02:32 +00:00
|
|
|
use crate::db::user::{UserForm, User_};
|
2020-04-07 16:47:19 +00:00
|
|
|
use crate::db::Crud;
|
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-04-08 11:23:59 +00:00
|
|
|
use diesel::result::Error::NotFound;
|
2020-04-07 16:47:19 +00:00
|
|
|
use diesel::PgConnection;
|
2020-03-12 00:01:25 +00:00
|
|
|
use failure::Error;
|
2020-03-28 19:41:42 +00:00
|
|
|
use isahc::prelude::*;
|
2020-03-14 21:03:05 +00:00
|
|
|
use serde::Deserialize;
|
2020-04-07 15:29:23 +00:00
|
|
|
use std::time::Duration;
|
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
|
|
|
|
2020-04-07 21:02:32 +00:00
|
|
|
fn fetch_communities_from_instance(
|
|
|
|
domain: &str,
|
|
|
|
conn: &PgConnection,
|
|
|
|
) -> Result<Vec<CommunityForm>, 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();
|
2020-04-07 16:47:19 +00:00
|
|
|
let communities: Result<Vec<CommunityForm>, Error> = object_boxes
|
|
|
|
.map(|c| {
|
2020-04-03 05:02:43 +00:00
|
|
|
let group = c.to_owned().to_concrete::<GroupExt>()?;
|
2020-04-07 21:02:32 +00:00
|
|
|
CommunityForm::from_group(&group, conn)
|
2020-04-03 05:02:43 +00:00
|
|
|
})
|
|
|
|
.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-07 21:02:32 +00:00
|
|
|
// TODO: add an optional param last_updated and only fetch if its too old
|
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-04-07 15:29:23 +00:00
|
|
|
let timeout = Duration::from_secs(60);
|
|
|
|
let text = Request::get(uri)
|
|
|
|
.header("Accept", APUB_JSON_CONTENT_TYPE)
|
|
|
|
.connect_timeout(timeout)
|
|
|
|
.timeout(timeout)
|
|
|
|
.body(())?
|
|
|
|
.send()?
|
|
|
|
.text()?;
|
2020-03-20 00:42:07 +00:00
|
|
|
let res: Response = serde_json::from_str(&text)?;
|
|
|
|
Ok(res)
|
2020-03-14 21:03:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 21:02:32 +00:00
|
|
|
fn fetch_remote_community_posts(
|
|
|
|
instance: &str,
|
|
|
|
community: &str,
|
|
|
|
conn: &PgConnection,
|
|
|
|
) -> Result<Vec<PostForm>, Error> {
|
2020-04-07 16:47:19 +00:00
|
|
|
let endpoint = format!("http://{}/federation/c/{}", instance, community);
|
|
|
|
let community = fetch_remote_object::<GroupExt>(&endpoint)?;
|
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-07 16:47:19 +00:00
|
|
|
let posts = 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();
|
2020-04-07 21:02:32 +00:00
|
|
|
PostForm::from_page(&page, conn)
|
2020-03-14 21:03:05 +00:00
|
|
|
})
|
2020-04-07 16:47:19 +00:00
|
|
|
.collect::<Result<Vec<PostForm>, Error>>()?;
|
|
|
|
Ok(posts)
|
2019-12-27 17:25:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 21:02:32 +00:00
|
|
|
pub fn fetch_remote_user(apub_id: &str, conn: &PgConnection) -> Result<User_, Error> {
|
|
|
|
let person = fetch_remote_object::<PersonExt>(apub_id)?;
|
|
|
|
let uf = UserForm::from_person(&person)?;
|
|
|
|
let existing = User_::read_from_apub_id(conn, &uf.actor_id);
|
|
|
|
Ok(match existing {
|
2020-04-08 11:23:59 +00:00
|
|
|
Err(NotFound {}) => User_::create(conn, &uf)?,
|
2020-04-07 21:02:32 +00:00
|
|
|
Ok(u) => User_::update(conn, u.id, &uf)?,
|
2020-04-08 11:23:59 +00:00
|
|
|
Err(e) => return Err(Error::from(e)),
|
2020-04-07 21:02:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:47:19 +00:00
|
|
|
// TODO: in the future, this should only be done when an instance is followed for the first time
|
|
|
|
// after that, we should rely in the inbox, and fetch on demand when needed
|
|
|
|
pub fn fetch_all(conn: &PgConnection) -> Result<(), Error> {
|
2020-03-14 21:03:05 +00:00
|
|
|
for instance in &get_following_instances() {
|
2020-04-07 21:02:32 +00:00
|
|
|
let communities = fetch_communities_from_instance(instance, conn)?;
|
2020-04-07 16:47:19 +00:00
|
|
|
|
|
|
|
for community in &communities {
|
|
|
|
let existing = Community::read_from_actor_id(conn, &community.actor_id);
|
|
|
|
let community_id = match existing {
|
2020-04-08 11:23:59 +00:00
|
|
|
Err(NotFound {}) => Community::create(conn, community)?.id,
|
2020-04-07 16:47:19 +00:00
|
|
|
Ok(c) => Community::update(conn, c.id, community)?.id,
|
2020-04-08 11:23:59 +00:00
|
|
|
Err(e) => return Err(Error::from(e)),
|
2020-04-07 16:47:19 +00:00
|
|
|
};
|
2020-04-07 21:02:32 +00:00
|
|
|
let mut posts = fetch_remote_community_posts(instance, &community.name, conn)?;
|
2020-04-07 16:47:19 +00:00
|
|
|
for post_ in &mut posts {
|
|
|
|
post_.community_id = community_id;
|
|
|
|
let existing = Post::read_from_apub_id(conn, &post_.ap_id);
|
|
|
|
match existing {
|
2020-04-08 11:23:59 +00:00
|
|
|
Err(NotFound {}) => Post::create(conn, post_)?,
|
2020-04-07 21:02:32 +00:00
|
|
|
Ok(p) => Post::update(conn, p.id, post_)?,
|
2020-04-08 11:23:59 +00:00
|
|
|
Err(e) => return Err(Error::from(e)),
|
2020-04-07 21:02:32 +00:00
|
|
|
};
|
2020-04-07 16:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-27 17:25:07 +00:00
|
|
|
}
|
2020-04-07 16:47:19 +00:00
|
|
|
Ok(())
|
2019-12-27 17:25:07 +00:00
|
|
|
}
|