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