fedimovies/src/mastodon_api/instance/views.rs

38 lines
1.1 KiB
Rust
Raw Normal View History

2021-11-07 08:52:57 +00:00
use actix_web::{get, web, HttpResponse, Scope};
2021-04-09 00:22:17 +00:00
use crate::config::Config;
use crate::database::{Pool, get_database_client};
2021-04-09 00:22:17 +00:00
use crate::errors::HttpError;
use crate::ethereum::contracts::ContractSet;
use crate::models::{
instances::queries::get_peer_count,
posts::queries::get_local_post_count,
users::queries::get_user_count,
};
use super::types::InstanceInfo;
2021-04-09 00:22:17 +00:00
2021-11-07 08:52:57 +00:00
#[get("")]
async fn instance_view(
config: web::Data<Config>,
db_pool: web::Data<Pool>,
maybe_blockchain: web::Data<Option<ContractSet>>,
2021-04-09 00:22:17 +00:00
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
let user_count = get_user_count(db_client).await?;
let post_count = get_local_post_count(db_client).await?;
let peer_count = get_peer_count(db_client).await?;
let instance = InstanceInfo::create(
config.as_ref(),
maybe_blockchain.as_ref().as_ref(),
user_count,
post_count,
peer_count,
);
2021-04-09 00:22:17 +00:00
Ok(HttpResponse::Ok().json(instance))
}
2021-11-07 08:52:57 +00:00
pub fn instance_api_scope() -> Scope {
web::scope("/api/v1/instance")
.service(instance_view)
}