feat: make constants configurable

This commit is contained in:
Anna 2024-06-05 13:44:10 -04:00
parent 97a1cb97a9
commit 5826d3586f
Signed by: anna
GPG Key ID: D0943384CD9F87D1
9 changed files with 506 additions and 492 deletions

978
server/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -7,9 +7,9 @@ edition = "2021"
[dependencies]
anyhow = "1"
base64 = "0.21"
bytes = "1"
chrono = "0.4"
data-encoding = "2.6.0"
if_chain = "1"
parking_lot = "0.12"
rand = "0.8"

View File

@ -7,4 +7,6 @@ pub struct Config {
pub address: String,
pub packs: PathBuf,
pub database: String,
pub vote_threshold_hide: i32,
pub max_messages: i32,
}

View File

@ -1 +0,0 @@
pub const VOTE_THRESHOLD_HIDE: i32 = -1;

View File

@ -23,7 +23,6 @@ mod message;
mod web;
mod util;
mod config;
mod consts;
static MIGRATOR: Migrator = sqlx::migrate!();

View File

@ -1,5 +1,3 @@
use base64::Engine;
use base64::prelude::BASE64_STANDARD;
use sha3::{Digest, Sha3_384};
// TerritoryIntendedUse = 13 or 14
@ -50,5 +48,5 @@ pub fn hash(input: &str) -> String {
let mut hasher = Sha3_384::default();
hasher.update(input.as_bytes());
let result = hasher.finalize();
BASE64_STANDARD.encode(result)
data_encoding::BASE64.encode(&result)
}

View File

@ -122,14 +122,14 @@ async fn logic(state: Arc<State>, id: i64, location: u32, query: GetLocationQuer
.map_err(warp::reject::custom)?
};
filter_messages(&mut messages, id);
filter_messages(&mut messages, id, state.config.vote_threshold_hide);
Ok(warp::reply::json(&messages))
}
fn filter_messages(messages: &mut Vec<RetrievedMessage>, id: i64) {
fn filter_messages(messages: &mut Vec<RetrievedMessage>, id: i64, vote_threshold_hide: i32) {
// remove messages where the user has been offline for over 35 minutes
// also remove messages with low score (that aren't the from the user)
messages.retain(|msg| msg.last_seen_minutes < 35 && (msg.user == id || (msg.positive_votes - msg.negative_votes) >= crate::consts::VOTE_THRESHOLD_HIDE));
messages.retain(|msg| msg.last_seen_minutes < 35 && (msg.user == id || (msg.positive_votes - msg.negative_votes) >= vote_threshold_hide));
// shuffle messages since we'll be excluding later based on messages
// that have already been included, so this will be more fair

View File

@ -63,7 +63,7 @@ async fn logic(state: Arc<State>, id: i64, extra: i64, mut query: HashMap<String
messages.reverse();
for msg in &mut messages {
msg.is_hidden = msg.positive_votes - msg.negative_votes < crate::consts::VOTE_THRESHOLD_HIDE;
msg.is_hidden = msg.positive_votes - msg.negative_votes < state.config.vote_threshold_hide;
}
if version == 1 {

View File

@ -58,7 +58,7 @@ async fn logic(state: Arc<State>, id: i64, extra: i64, message: Message) -> Resu
.map_err(AnyhowRejection)
.map_err(warp::reject::custom)?;
if existing.count >= 10 + extra as i32 {
if existing.count >= state.config.max_messages + extra as i32 {
return Err(warp::reject::custom(WebError::TooManyMessages));
}