fedimovies/src/mastodon_api/instance/views.rs

39 lines
1.2 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;
2022-12-03 21:23:52 +00:00
use crate::database::{get_database_client, DbPool};
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
/// https://docs.joinmastodon.org/methods/instance/#v1
2021-11-07 08:52:57 +00:00
#[get("")]
async fn instance_view(
config: web::Data<Config>,
2022-12-03 21:23:52 +00:00
db_pool: web::Data<DbPool>,
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)
}