Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to randomize instance statistics #634

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ description = "https://www.youtube.com/watch?v=6lnnPnr_0SU"
# This is the maximum length of posts that can be posted on your instance
# On Mastodon it's 500, on most other implementations it's freely configurable
character-limit = 5000
# Randomize the statistics of your server
#
# Kitsune will expose certain statistics about your server by default.
# Set this value to `true` to serve randomized statistics instead.
#
# Why you'd want this can have many reasons, one of them being privacy reasons.
# Perhaps you don't want to expose accurate information about your server publicly.
randomize-statistics = false
# Registrations open
#
# This signals to clients whether your registrations are currently open or not.
Expand Down
1 change: 1 addition & 0 deletions crates/kitsune-config/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ pub struct Configuration {
pub webfinger_domain: Option<SmolStr>,
pub character_limit: usize,
pub federation_filter: FederationFilterConfiguration,
pub randomize_statistics: bool,
pub registrations_open: bool,
}
22 changes: 22 additions & 0 deletions crates/kitsune-service/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ use kitsune_db::{
};
use kitsune_derive::kitsune_service;
use kitsune_error::{Error, Result};
use rand::seq::IteratorRandom;
use smol_str::SmolStr;
use std::ops::RangeInclusive;

const STATISTICS_RANGE: RangeInclusive<u64> = 24..=1312_1312;

#[inline]
fn random_statistic() -> u64 {
STATISTICS_RANGE.choose(&mut rand::thread_rng()).unwrap()
}

#[kitsune_service]
pub struct InstanceService {
Expand All @@ -16,6 +25,7 @@ pub struct InstanceService {
#[builder(setter(into))]
description: SmolStr,
character_limit: usize,
randomize_statistics: bool,
registrations_open: bool,
}

Expand All @@ -36,6 +46,10 @@ impl InstanceService {
}

pub async fn known_instances(&self) -> Result<u64> {
if self.randomize_statistics {
return Ok(random_statistic());
}

with_connection!(self.db_pool, |db_conn| {
accounts::table
.filter(accounts::local.eq(false))
Expand All @@ -50,6 +64,10 @@ impl InstanceService {
}

pub async fn local_post_count(&self) -> Result<u64> {
if self.randomize_statistics {
return Ok(random_statistic());
}

with_connection!(self.db_pool, |db_conn| {
posts::table
.filter(posts::is_local.eq(true))
Expand All @@ -67,6 +85,10 @@ impl InstanceService {
}

pub async fn user_count(&self) -> Result<u64> {
if self.randomize_statistics {
return Ok(random_statistic());
}

with_connection!(self.db_pool, |db_conn| {
users::table
.count()
Expand Down
1 change: 1 addition & 0 deletions kitsune/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub async fn initialise_state(
.db_pool(db_pool.clone())
.description(config.instance.description.clone())
.name(config.instance.name.clone())
.randomize_statistics(config.instance.randomize_statistics)
.registrations_open(config.instance.registrations_open)
.build();

Expand Down
10 changes: 10 additions & 0 deletions website/src/content/docs/configuration/instance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ Similar to `name`, this setting adjusts the description on the landing page and

This setting sets the character limit specific to your instance.

## `randomize-statistics`

Randomize the statistics of your server

Kitsune will expose certain statistics about your server by default.
Set this value to `true` to serve randomized statistics instead.

Why you'd want this can have many reasons, one of them being privacy reasons.
Perhaps you don't want to expose accurate information about your server publicly.

## `registrations-open`

Determines whether your instance accepts new users or not. When set to `false`, the registration APIs will return a failure code.
Expand Down
Loading