]> Untitled Git - lemmy.git/blob - server/src/apub/puller.rs
Merge branch 'master' into federation
[lemmy.git] / server / src / apub / puller.rs
1 extern crate reqwest;
2
3 use self::reqwest::Error;
4 use crate::api::community::{GetCommunityResponse, ListCommunitiesResponse};
5 use crate::api::post::GetPosts;
6 use crate::api::UserOperation;
7 use crate::db::community_view::CommunityView;
8 use crate::naive_now;
9 use crate::settings::Settings;
10 use activitypub::actor::Group;
11
12 // TODO: right now all of the data is requested on demand, for production we will need to store
13 //       things in the local database to not ruin the performance
14
15 fn fetch_communities_from_instance(domain: &str) -> Result<Vec<CommunityView>, Error> {
16   // TODO: check nodeinfo to make sure we are dealing with a lemmy instance
17   //       -> means we need proper nodeinfo json classes instead of inline generation
18   // TODO: follow pagination (seems like page count is missing?)
19   // TODO: see if there is any standard for discovering remote actors, so we dont have to rely on lemmy apis
20   let communities_uri = format!("http://{}/api/v1/communities/list?sort=Hot", domain);
21   let communities1: ListCommunitiesResponse = reqwest::get(&communities_uri)?.json()?;
22   let mut communities2 = communities1.communities;
23   for c in &mut communities2 {
24     c.name = format_community_name(&c.name, domain);
25   }
26   Ok(communities2)
27 }
28
29 pub fn get_remote_community_posts(name: String) -> Result<GetPosts, Error> {
30   // TODO: this is for urls like /c/!main@example.com, activitypub exposes it through the outbox
31   //       https://www.w3.org/TR/activitypub/#outbox
32   dbg!(name);
33   unimplemented!()
34 }
35
36 pub fn get_remote_community(identifier: String) -> Result<GetCommunityResponse, Error> {
37   let x: Vec<&str> = identifier.split('@').collect();
38   let name = x[0].replace("!", "");
39   let instance = x[1];
40   let community_uri = format!("http://{}/federation/c/{}", instance, name);
41   let community: Group = reqwest::get(&community_uri)?.json()?;
42
43   // TODO: looks like a bunch of data is missing from the activitypub response
44   // TODO: i dont think simple numeric ids are going to work, we probably need something like uuids
45   // TODO: why are the Group properties not typed?
46   Ok(GetCommunityResponse {
47     op: UserOperation::GetCommunity.to_string(),
48     moderators: vec![],
49     admins: vec![],
50     community: CommunityView {
51       id: -1,
52       name: identifier.clone(),
53       title: identifier,
54       description: community.object_props.summary.map(|c| c.to_string()),
55       category_id: -1,
56       creator_id: -1,
57       removed: false,
58       published: naive_now(),     // TODO: community.object_props.published
59       updated: Some(naive_now()), // TODO: community.object_props.updated
60       deleted: false,
61       nsfw: false,
62       creator_name: "".to_string(),
63       creator_avatar: None,
64       category_name: "".to_string(),
65       number_of_subscribers: -1,
66       number_of_posts: -1,
67       number_of_comments: -1,
68       hot_rank: -1,
69       user_id: None,
70       subscribed: None,
71     },
72   })
73 }
74
75 pub fn get_following_instances() -> Result<Vec<String>, Error> {
76   let instance_list = match Settings::get().federated_instance.clone() {
77     Some(f) => vec![f, Settings::get().hostname.clone()],
78     None => vec![Settings::get().hostname.clone()],
79   };
80   Ok(instance_list)
81 }
82
83 pub fn get_all_communities() -> Result<Vec<CommunityView>, Error> {
84   let mut communities_list: Vec<CommunityView> = vec![];
85   for instance in &get_following_instances()? {
86     communities_list.append(fetch_communities_from_instance(instance)?.as_mut());
87   }
88   Ok(communities_list)
89 }
90
91 /// If community is on local instance, don't include the @instance part
92 pub fn format_community_name(name: &str, instance: &str) -> String {
93   if instance == Settings::get().hostname {
94     format!("!{}", name)
95   } else {
96     format!("!{}@{}", name, instance)
97   }
98 }