From 33ba1fc623522725319ea3d3ec9ac69349b3eda3 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sun, 16 Apr 2023 17:00:31 -0400 Subject: [PATCH] Separate federated_instances into its own endpoint. Fixes #1931 (#2813) --- crates/api/src/site/federated_instances.rs | 29 ++++++++++++++++++++++ crates/api/src/site/leave_admin.rs | 1 - crates/api/src/site/mod.rs | 1 + crates/api_common/src/site.rs | 9 ++++++- crates/api_common/src/websocket/mod.rs | 1 + crates/api_crud/src/site/read.rs | 6 +---- crates/apub/src/activities/unfederated.rs | 6 +++++ src/api_routes_http.rs | 6 +++++ src/api_routes_websocket.rs | 4 +++ 9 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 crates/api/src/site/federated_instances.rs diff --git a/crates/api/src/site/federated_instances.rs b/crates/api/src/site/federated_instances.rs new file mode 100644 index 000000000..107fece44 --- /dev/null +++ b/crates/api/src/site/federated_instances.rs @@ -0,0 +1,29 @@ +use crate::Perform; +use actix_web::web::Data; +use lemmy_api_common::{ + context::LemmyContext, + site::{GetFederatedInstances, GetFederatedInstancesResponse}, + utils::build_federated_instances, +}; +use lemmy_db_views::structs::SiteView; +use lemmy_utils::{error::LemmyError, ConnectionId}; + +#[async_trait::async_trait(?Send)] +impl Perform for GetFederatedInstances { + type Response = GetFederatedInstancesResponse; + + #[tracing::instrument(skip(context, _websocket_id))] + async fn perform( + &self, + context: &Data, + _websocket_id: Option, + ) -> Result { + let site_view = SiteView::read_local(context.pool()).await?; + let federated_instances = + build_federated_instances(&site_view.local_site, context.pool()).await?; + + Ok(Self::Response { + federated_instances, + }) + } +} diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index c0f20eaf0..4cec5fc4a 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -73,7 +73,6 @@ impl Perform for LeaveAdmin { online: 0, version: version::VERSION.to_string(), my_user: None, - federated_instances: None, all_languages, discussion_languages, taglines, diff --git a/crates/api/src/site/mod.rs b/crates/api/src/site/mod.rs index 4e3694ce4..c23839c94 100644 --- a/crates/api/src/site/mod.rs +++ b/crates/api/src/site/mod.rs @@ -1,3 +1,4 @@ +mod federated_instances; mod leave_admin; mod mod_log; mod purge; diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index 524d10204..a290beb10 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -222,13 +222,20 @@ pub struct GetSiteResponse { pub online: usize, pub version: String, pub my_user: Option, - pub federated_instances: Option, // Federation may be disabled pub all_languages: Vec, pub discussion_languages: Vec, pub taglines: Vec, pub custom_emojis: Vec, } +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct GetFederatedInstances {} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct GetFederatedInstancesResponse { + pub federated_instances: Option, // Federation may be disabled +} + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct MyUserInfo { pub local_user_view: LocalUserView, diff --git a/crates/api_common/src/websocket/mod.rs b/crates/api_common/src/websocket/mod.rs index 2726b7d4c..2f13d8b8c 100644 --- a/crates/api_common/src/websocket/mod.rs +++ b/crates/api_common/src/websocket/mod.rs @@ -94,6 +94,7 @@ pub enum UserOperation { PurgeCommunity, PurgePost, PurgeComment, + GetFederatedInstances, } #[derive(EnumString, Display, Debug, Clone)] diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index 2330d0f73..fc74ecaeb 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, site::{GetSite, GetSiteResponse, MyUserInfo}, - utils::{build_federated_instances, get_local_user_settings_view_from_jwt_opt}, + utils::get_local_user_settings_view_from_jwt_opt, websocket::handlers::online_users::GetUsersOnline, }; use lemmy_db_schema::source::{ @@ -84,9 +84,6 @@ impl PerformCrud for GetSite { None }; - let federated_instances = - build_federated_instances(&site_view.local_site, context.pool()).await?; - let all_languages = Language::read_all(context.pool()).await?; let discussion_languages = SiteLanguage::read_local_raw(context.pool()).await?; let taglines = Tagline::get_all(context.pool(), site_view.local_site.id).await?; @@ -98,7 +95,6 @@ impl PerformCrud for GetSite { online, version: version::VERSION.to_string(), my_user, - federated_instances, all_languages, discussion_languages, taglines, diff --git a/crates/apub/src/activities/unfederated.rs b/crates/apub/src/activities/unfederated.rs index e2fff292d..0a7981a47 100644 --- a/crates/apub/src/activities/unfederated.rs +++ b/crates/apub/src/activities/unfederated.rs @@ -93,6 +93,8 @@ use lemmy_api_common::{ ApproveRegistrationApplication, CreateSite, EditSite, + GetFederatedInstances, + GetFederatedInstancesResponse, GetModlog, GetModlogResponse, GetSite, @@ -373,3 +375,7 @@ impl SendActivity for EditCustomEmoji { impl SendActivity for DeleteCustomEmoji { type Response = DeleteCustomEmojiResponse; } + +impl SendActivity for GetFederatedInstances { + type Response = GetFederatedInstancesResponse; +} diff --git a/src/api_routes_http.rs b/src/api_routes_http.rs index d59253dee..725e5babd 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -86,6 +86,7 @@ use lemmy_api_common::{ ApproveRegistrationApplication, CreateSite, EditSite, + GetFederatedInstances, GetModlog, GetSite, GetUnreadRegistrationApplicationCount, @@ -165,6 +166,11 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { .route("/join", web::post().to(route_post::)) .route("/mod/join", web::post().to(route_post::)), ) + .service( + web::scope("/federated_instances") + .wrap(rate_limit.message()) + .route("", web::get().to(route_get::)), + ) // Post .service( // Handle POST to /post separately to add the post() rate limitter diff --git a/src/api_routes_websocket.rs b/src/api_routes_websocket.rs index b3ce85bdb..23b9a5b5e 100644 --- a/src/api_routes_websocket.rs +++ b/src/api_routes_websocket.rs @@ -98,6 +98,7 @@ use lemmy_api_common::{ ApproveRegistrationApplication, CreateSite, EditSite, + GetFederatedInstances, GetModlog, GetSite, GetUnreadRegistrationApplicationCount, @@ -640,6 +641,9 @@ pub async fn match_websocket_operation( do_websocket_operation::(context, id, op, data).await } UserOperation::LeaveAdmin => do_websocket_operation::(context, id, op, data).await, + UserOperation::GetFederatedInstances => { + do_websocket_operation::(context, id, op, data).await + } // Community ops UserOperation::FollowCommunity => {