add: better pinned posts ui

This commit is contained in:
trisua 2025-08-06 23:13:31 -04:00
parent b5f841a990
commit 81a7628861
12 changed files with 25 additions and 192 deletions

View file

@ -13,9 +13,7 @@ use crate::model::{
};
use tetratto_shared::unix_epoch_timestamp;
use crate::{auto_method, DataManager};
use oiseau::{PostgresRow, cache::redis::Commands};
use oiseau::{execute, get, query_row, query_rows, params, cache::Cache};
use oiseau::{PostgresRow, execute, get, query_row, query_rows, params, cache::Cache};
pub type FullPost = (
Post,
@ -816,95 +814,6 @@ impl DataManager {
Ok(res.unwrap())
}
/// Calculate the GPA (great post average) of a given user.
///
/// To be considered a "great post", a post must have a score ((likes - dislikes) / (likes + dislikes))
/// of at least 0.6.
///
/// GPA is calculated based on the user's last 48 posts.
pub async fn calculate_user_gpa(&self, id: usize) -> f32 {
// just for note, this is SUPER bad for performance... which is why we
// only calculate this when it expires in the cache (every day)
if let Some(cached) = self.0.1.get(format!("atto.user.gpa:{}", id)).await {
if let Ok(c) = cached.parse() {
return c;
}
}
// ...
let conn = match self.0.connect().await {
Ok(c) => c,
Err(_) => return 0.0,
};
let res = query_rows!(
&conn,
&format!("SELECT * FROM posts WHERE owner = $1 ORDER BY created DESC LIMIT 48"),
&[&(id as i64)],
|x| { Self::get_post_from_row(x) }
);
if res.is_err() {
return 0.0;
}
// ...
let mut real_posts_count: usize = 0; // posts which can be scored
let mut good_posts: usize = 0;
// let mut bad_posts: usize = 0;
let posts = res.unwrap();
for post in posts {
if post.likes == 0 && post.dislikes == 0 {
// post has no likes or dislikes... doesn't count
if good_posts > 8 {
good_posts -= 1; // we're going to say this is a bad post because it isn't liked enough
}
continue;
}
real_posts_count += 1;
// likes percentage / total likes
let score: f32 = (post.likes as f32 - post.dislikes as f32)
/ (post.likes as f32 + post.dislikes as f32);
if score.is_sign_negative() {
// bad_posts += 1;
continue;
}
if score > 0.6 {
good_posts += 1;
}
// } else {
// bad_posts += 1;
// }
}
let gpa = (good_posts as f32 / real_posts_count as f32) * 4.0;
let gpa_rounded = format!("{gpa:.2}").parse::<f32>().unwrap();
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
}
/// Get all replies from the given user (from most recent).
///
/// # Arguments
@ -1924,6 +1833,8 @@ impl DataManager {
} else {
return Err(Error::GeneralNotFound("topic".to_string()));
}
} else if data.topic != 0 {
return Err(Error::DoesNotSupportField("Community".to_string()));
}
// ...