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;
8 use crate::settings::Settings;
9 use activitypub::actor::Group;
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
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);
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
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("!", "");
39 let community_uri = format!("http://{}/federation/c/{}", instance, name);
40 let community: Group = reqwest::get(&community_uri)?.json()?;
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 {
48 community: CommunityView {
50 name: identifier.clone(),
52 description: community.object_props.summary.map(|c| c.to_string()),
56 published: naive_now(), // TODO: community.object_props.published
57 updated: Some(naive_now()), // TODO: community.object_props.updated
60 creator_name: "".to_string(),
62 category_name: "".to_string(),
63 number_of_subscribers: -1,
65 number_of_comments: -1,
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()],
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());
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 {
95 format!("!{}@{}", name, instance)