add: ability to mute phrases

add: ability to disable gpa experiment
This commit is contained in:
trisua 2025-06-10 13:49:17 -04:00
parent 9d839a1a63
commit f034cc4f27
7 changed files with 212 additions and 63 deletions

View file

@ -21,8 +21,20 @@ use oiseau::SqliteRow;
#[cfg(feature = "postgres")]
use oiseau::PostgresRow;
#[cfg(feature = "redis")]
use oiseau::cache::redis::Commands;
use oiseau::{execute, get, query_row, query_rows, params};
pub type FullPost = (
Post,
User,
Community,
Option<(User, Post)>,
Option<(Question, User)>,
Option<(Poll, bool, bool)>,
);
macro_rules! private_post_replying {
($post:ident, $replying_posts:ident, $ua1:ident, $data:ident) => {
// post owner is not following us
@ -376,16 +388,7 @@ impl DataManager {
user_id: usize,
ignore_users: &[usize],
user: &Option<User>,
) -> Result<
Vec<(
Post,
User,
Community,
Option<(User, Post)>,
Option<(Question, User)>,
Option<(Poll, bool, bool)>,
)>,
> {
) -> Result<Vec<FullPost>> {
let mut out = Vec::new();
let mut seen_before: HashMap<(usize, usize), (User, Community)> = HashMap::new();
@ -463,6 +466,33 @@ impl DataManager {
Ok(out)
}
/// Update posts which contain a muted phrase.
pub fn posts_muted_phrase_filter(
&self,
posts: &Vec<Post>,
muted: Option<&Vec<String>>,
) -> Vec<Post> {
let muted = match muted {
Some(m) => m,
None => return posts.to_owned(),
};
let mut out: Vec<Post> = Vec::new();
for mut post in posts.clone() {
for phrase in muted {
if post.content.contains(phrase) {
post.context.content_warning = "Contains muted phrase".to_string();
break;
}
}
out.push(post);
}
out
}
/// Get all posts from the given user (from most recent).
///
/// # Arguments
@ -533,7 +563,7 @@ impl DataManager {
let res = query_rows!(
&conn,
&format!("SELECT * FROM posts WHERE owner = $1 ORDER BY created DESC LIMIT 50"),
&format!("SELECT * FROM posts WHERE owner = $1 ORDER BY created DESC LIMIT 12"),
&[&(id as i64)],
|x| { Self::get_post_from_row(x) }
);
@ -581,11 +611,21 @@ impl DataManager {
let gpa = (good_posts as f32 / real_posts_count as f32) * 4.0;
let gpa_rounded = format!("{gpa:.2}").parse::<f32>().unwrap();
self.0
.1
.set(format!("atto.user.gpa:{}", id), gpa_rounded.to_string())
.await;
let mut redis_con = self.0.1.get_con().await;
// expires in one day
if redis_con
.set_ex::<String, String, usize>(
format!("atto.user.gpa:{}", id),
gpa_rounded.to_string(),
86400,
)
.is_err()
{
return 0.0;
};
// ...
gpa_rounded
}