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