Add total numbers of local users and posts to NodeInfo response

This commit is contained in:
silverpill 2022-07-07 18:55:47 +00:00
parent 2a22a0bedd
commit d63c19a996
6 changed files with 57 additions and 7 deletions

View file

@ -1051,6 +1051,21 @@ pub async fn delete_post(
})
}
pub async fn get_local_post_count(
db_client: &impl GenericClient,
) -> Result<i64, DatabaseError> {
let row = db_client.query_one(
"
SELECT count(post)
FROM post
JOIN user_account ON (post.author_id = user_account.id)
WHERE post.in_reply_to_id IS NULL AND post.repost_of_id IS NULL
",
&[],
).await?;
let count = row.try_get("count")?;
Ok(count)
}
#[cfg(test)]
mod tests {

View file

@ -178,3 +178,14 @@ pub async fn get_user_by_wallet_address(
let user = User::new(db_user, db_profile);
Ok(user)
}
pub async fn get_user_count(
db_client: &impl GenericClient,
) -> Result<i64, DatabaseError> {
let row = db_client.query_one(
"SELECT count(user_account) FROM user_account",
&[],
).await?;
let count = row.try_get("count")?;
Ok(count)
}

16
src/nodeinfo/helpers.rs Normal file
View file

@ -0,0 +1,16 @@
use tokio_postgres::GenericClient;
use crate::errors::DatabaseError;
use crate::models::posts::queries::get_local_post_count;
use crate::models::users::queries::get_user_count;
use super::types::{Usage, Users};
pub async fn get_usage(db_client: &impl GenericClient) -> Result<Usage, DatabaseError> {
let user_count = get_user_count(db_client).await?;
let post_count = get_local_post_count(db_client).await?;
let usage = Usage {
users: Users { total: user_count },
local_posts: post_count,
};
Ok(usage)
}

View file

@ -1,2 +1,3 @@
mod helpers;
mod types;
pub mod views;

View file

@ -17,12 +17,15 @@ struct Services {
}
#[derive(Serialize)]
struct Users {
pub struct Users {
pub total: i64,
}
#[derive(Serialize)]
struct Usage {
users: Users,
#[serde(rename_all = "camelCase")]
pub struct Usage {
pub users: Users,
pub local_posts: i64,
}
#[derive(Serialize)]
@ -45,7 +48,7 @@ pub struct NodeInfo20 {
}
impl NodeInfo20 {
pub fn new(config: &Config) -> Self {
pub fn new(config: &Config, usage: Usage) -> Self {
let software = Software {
name: "mitra".to_string(),
version: config.version.clone(),
@ -64,7 +67,7 @@ impl NodeInfo20 {
protocols: vec!["activitypub".to_string()],
services,
open_registrations: config.registrations_open,
usage: Usage { users: Users { } },
usage,
metadata,
}
}

View file

@ -3,11 +3,13 @@
use actix_web::{get, web, HttpResponse};
use crate::config::Config;
use crate::database::{Pool, get_database_client};
use crate::errors::HttpError;
use crate::webfinger::types::{
Link,
JsonResourceDescriptor,
};
use super::helpers::get_usage;
use super::types::NodeInfo20;
#[get("/.well-known/nodeinfo")]
@ -31,9 +33,11 @@ pub async fn get_nodeinfo(
#[get("/nodeinfo/2.0")]
pub async fn get_nodeinfo_2_0(
config: web::Data<Config>,
db_pool: web::Data<Pool>,
) -> Result<HttpResponse, HttpError> {
let nodeinfo = NodeInfo20::new(&config);
let db_client = &**get_database_client(&db_pool).await?;
let usage = get_usage(db_client).await?;
let nodeinfo = NodeInfo20::new(&config, usage);
let response = HttpResponse::Ok().json(nodeinfo);
Ok(response)
}