add: post titles

This commit is contained in:
trisua 2025-06-08 15:34:29 -04:00
parent 1279536609
commit 5b1db42c51
14 changed files with 230 additions and 13 deletions

View file

@ -111,6 +111,7 @@ impl DataManager {
is_deleted: get!(x->11(i32)) as i8 == 1,
// SKIP tsvector (12)
poll_id: get!(x->13(i64)) as usize,
title: get!(x->14(String)),
}
}
@ -1212,6 +1213,8 @@ impl DataManager {
}
}
let community = self.get_community_by_id(data.community).await?;
// check values (if this isn't reposting something else)
let is_reposting = if let Some(ref repost) = data.context.repost {
repost.reposting.is_some()
@ -1225,14 +1228,24 @@ impl DataManager {
} else if data.content.len() > 4096 {
return Err(Error::DataTooLong("content".to_string()));
}
// check title
if !community.context.enable_titles {
if !data.title.is_empty() {
return Err(Error::MiscError(
"Community does not allow titles".to_string(),
));
}
} else {
if data.title.len() < 2 && community.context.require_titles {
return Err(Error::DataTooShort("title".to_string()));
} else if data.title.len() > 128 {
return Err(Error::DataTooLong("title".to_string()));
}
}
}
// check permission in community
let community = match self.get_community_by_id(data.community).await {
Ok(p) => p,
Err(e) => return Err(e),
};
if !self.check_can_post(&community, data.owner).await {
return Err(Error::NotAllowed);
}
@ -1428,7 +1441,7 @@ impl DataManager {
let res = execute!(
&conn,
"INSERT INTO posts VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, DEFAULT, $13)",
"INSERT INTO posts VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, DEFAULT, $13, $14)",
params![
&(data.id as i64),
&(data.created as i64),
@ -1447,6 +1460,7 @@ impl DataManager {
&serde_json::to_string(&data.uploads).unwrap(),
&{ if data.is_deleted { 1 } else { 0 } },
&(data.poll_id as i64),
&data.title
]
);
@ -1743,6 +1757,13 @@ impl DataManager {
}
}
// check length
if x.len() < 2 {
return Err(Error::DataTooShort("content".to_string()));
} else if x.len() > 4096 {
return Err(Error::DataTooLong("content".to_string()));
}
// ...
let conn = match self.0.connect().await {
Ok(c) => c,
@ -1767,6 +1788,59 @@ impl DataManager {
Ok(())
}
pub async fn update_post_title(&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_title` with x value `{id}`"),
))
.await?
}
}
let community = self.get_community_by_id(y.community).await?;
if !community.context.enable_titles {
return Err(Error::MiscError(
"Community does not allow titles".to_string(),
));
}
if x.len() < 2 && community.context.require_titles {
return Err(Error::DataTooShort("title".to_string()));
} else if x.len() > 128 {
return Err(Error::DataTooLong("title".to_string()));
}
// ...
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"UPDATE posts SET title = $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);
auto_method!(decr_post_likes() -> "UPDATE posts SET likes = likes - 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --decr);