Add /api/v1/instance/peers API endpoint

This commit is contained in:
silverpill 2023-03-20 17:06:24 +00:00
parent 28be8dbb31
commit 608ec096cd
3 changed files with 27 additions and 1 deletions

View file

@ -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 `fep-e232` feature flag (disabled by default).
- Added `account_index` parameter to Monero configuration. - Added `account_index` parameter to Monero configuration.
- Added `/api/v1/instance/peers` API endpoint.
### Changed ### Changed

View file

@ -6,7 +6,7 @@ use crate::database::{get_database_client, DbPool};
use crate::ethereum::contracts::ContractSet; use crate::ethereum::contracts::ContractSet;
use crate::mastodon_api::errors::MastodonError; use crate::mastodon_api::errors::MastodonError;
use crate::models::{ use crate::models::{
instances::queries::get_peer_count, instances::queries::{get_peers, get_peer_count},
posts::queries::get_local_post_count, posts::queries::get_local_post_count,
users::queries::get_user_count, users::queries::get_user_count,
}; };
@ -33,7 +33,17 @@ async fn instance_view(
Ok(HttpResponse::Ok().json(instance)) Ok(HttpResponse::Ok().json(instance))
} }
#[get("/peers")]
async fn instance_peers_view(
db_pool: web::Data<DbPool>,
) -> Result<HttpResponse, MastodonError> {
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 { pub fn instance_api_scope() -> Scope {
web::scope("/api/v1/instance") web::scope("/api/v1/instance")
.service(instance_view) .service(instance_view)
.service(instance_peers_view)
} }

View file

@ -14,6 +14,21 @@ pub async fn create_instance(
Ok(()) Ok(())
} }
pub async fn get_peers(
db_client: &impl DatabaseClient,
) -> Result<Vec<String>, DatabaseError> {
let rows = db_client.query(
"
SELECT instance.hostname FROM instance
",
&[],
).await?;
let peers = rows.iter()
.map(|row| row.try_get("hostname"))
.collect::<Result<_, _>>()?;
Ok(peers)
}
pub async fn get_peer_count( pub async fn get_peer_count(
db_client: &impl DatabaseClient, db_client: &impl DatabaseClient,
) -> Result<i64, DatabaseError> { ) -> Result<i64, DatabaseError> {