add: post editing, profile pinned posts, theme settings

TODO: nsfw profile/community options
This commit is contained in:
trisua 2025-04-07 16:07:01 -04:00
parent 31f63c90cd
commit f83cfa3756
10 changed files with 255 additions and 8 deletions

View file

@ -16,6 +16,7 @@ use crate::{auto_method, execute, get, query_row, query_rows, params};
#[cfg(feature = "sqlite")]
use rusqlite::Row;
use tetratto_shared::unix_epoch_timestamp;
#[cfg(feature = "postgres")]
use tokio_postgres::Row;
@ -143,7 +144,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM posts WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
"SELECT * FROM posts WHERE owner = $1 AND NOT context LIKE '%\"is_profile_pinned\":true%' ORDER BY created DESC LIMIT $2 OFFSET $3",
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }
);
@ -210,6 +211,30 @@ impl DataManager {
Ok(res.unwrap())
}
/// Get all pinned posts from the given user (from most recent).
///
/// # Arguments
/// * `id` - the ID of the user the requested posts belong to
pub async fn get_pinned_posts_by_user(&self, id: usize) -> Result<Vec<Post>> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM posts WHERE owner = $1 AND context LIKE '%\"is_profile_pinned\":true%' ORDER BY created DESC",
&[&(id as i64),],
|x| { Self::get_post_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("post".to_string()));
}
Ok(res.unwrap())
}
/// Get posts from all communities, sorted by likes.
///
/// # Arguments
@ -529,6 +554,19 @@ impl DataManager {
}
}
// check if we can manage profile pins
if (x.is_profile_pinned != y.context.is_profile_pinned) && (user.id != y.owner) {
if !user.permissions.check(FinePermission::MANAGE_POSTS) {
return Err(Error::NotAllowed);
} else {
self.create_audit_log_entry(AuditLogEntry::new(
user.id,
format!("invoked `update_post_context(profile_pinned)` with x value `{id}`"),
))
.await?
}
}
// ...
let conn = match self.connect().await {
Ok(c) => c,
@ -551,7 +589,44 @@ impl DataManager {
Ok(())
}
auto_method!(update_post_content(String)@get_post_by_id:MANAGE_POSTS -> "UPDATE posts SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.post:{}");
pub async fn update_post_content(&self, id: usize, user: User, x: String) -> Result<()> {
let mut y = self.get_post_by_id(id).await?;
if user.id != y.owner {
if !user.permissions.check(FinePermission::MANAGE_POSTS) {
return Err(Error::NotAllowed);
} else {
self.create_audit_log_entry(AuditLogEntry::new(
user.id,
format!("invoked `update_post_content` with x value `{id}`"),
))
.await?
}
}
// ...
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"UPDATE posts SET content = $1 WHERE id = $2",
params![&x, &(id as i64)]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
// update context
y.context.edited = unix_epoch_timestamp() as usize;
self.update_post_context(id, user, y.context).await?;
// return
Ok(())
}
auto_method!(incr_post_likes() -> "UPDATE posts SET likes = likes + 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --incr);
auto_method!(incr_post_dislikes() -> "UPDATE posts SET dislikes = dislikes + 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --incr);

View file

@ -62,6 +62,12 @@ pub struct UserSettings {
pub theme_preference: ThemePreference,
#[serde(default)]
pub private_last_seen: bool,
#[serde(default)]
pub theme_hue: String,
#[serde(default)]
pub theme_sat: String,
#[serde(default)]
pub theme_lit: String,
}
impl Default for UserSettings {
@ -74,6 +80,9 @@ impl Default for UserSettings {
private_communities: false,
theme_preference: ThemePreference::default(),
private_last_seen: false,
theme_hue: String::new(),
theme_sat: String::new(),
theme_lit: String::new(),
}
}
}

View file

@ -167,6 +167,10 @@ pub struct PostContext {
pub comments_enabled: bool,
#[serde(default)]
pub is_pinned: bool,
#[serde(default)]
pub is_profile_pinned: bool,
#[serde(default)]
pub edited: usize,
}
fn default_comments_enabled() -> bool {
@ -178,6 +182,8 @@ impl Default for PostContext {
Self {
comments_enabled: default_comments_enabled(),
is_pinned: false,
is_profile_pinned: false,
edited: 0,
}
}
}