From 608ec096cd85c2abed2061a0531377d714595e64 Mon Sep 17 00:00:00 2001 From: silverpill Date: Mon, 20 Mar 2023 17:06:24 +0000 Subject: [PATCH] Add /api/v1/instance/peers API endpoint --- CHANGELOG.md | 1 + src/mastodon_api/instance/views.rs | 12 +++++++++++- src/models/instances/queries.rs | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f6c830..d80f62c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Added `fep-e232` feature flag (disabled by default). - Added `account_index` parameter to Monero configuration. +- Added `/api/v1/instance/peers` API endpoint. ### Changed diff --git a/src/mastodon_api/instance/views.rs b/src/mastodon_api/instance/views.rs index 66166cd..79b801f 100644 --- a/src/mastodon_api/instance/views.rs +++ b/src/mastodon_api/instance/views.rs @@ -6,7 +6,7 @@ use crate::database::{get_database_client, DbPool}; use crate::ethereum::contracts::ContractSet; use crate::mastodon_api::errors::MastodonError; use crate::models::{ - instances::queries::get_peer_count, + instances::queries::{get_peers, get_peer_count}, posts::queries::get_local_post_count, users::queries::get_user_count, }; @@ -33,7 +33,17 @@ async fn instance_view( Ok(HttpResponse::Ok().json(instance)) } +#[get("/peers")] +async fn instance_peers_view( + db_pool: web::Data, +) -> Result { + let db_client = &**get_database_client(&db_pool).await?; + let peers = get_peers(db_client).await?; + Ok(HttpResponse::Ok().json(peers)) +} + pub fn instance_api_scope() -> Scope { web::scope("/api/v1/instance") .service(instance_view) + .service(instance_peers_view) } diff --git a/src/models/instances/queries.rs b/src/models/instances/queries.rs index 1a02883..f097d7f 100644 --- a/src/models/instances/queries.rs +++ b/src/models/instances/queries.rs @@ -14,6 +14,21 @@ pub async fn create_instance( Ok(()) } +pub async fn get_peers( + db_client: &impl DatabaseClient, +) -> Result, DatabaseError> { + let rows = db_client.query( + " + SELECT instance.hostname FROM instance + ", + &[], + ).await?; + let peers = rows.iter() + .map(|row| row.try_get("hostname")) + .collect::>()?; + Ok(peers) +} + pub async fn get_peer_count( db_client: &impl DatabaseClient, ) -> Result {