add: post drafts
fix: allow question sender to view question
This commit is contained in:
parent
24162573ee
commit
f6cbeb9bd8
22 changed files with 642 additions and 100 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "tetratto-core"
|
||||
version = "3.0.0"
|
||||
version = "3.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[features]
|
||||
|
|
|
@ -291,6 +291,28 @@ impl DataManager {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// delete stacks
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"DELETE FROM stacks WHERE owner = $1",
|
||||
&[&(id as i64)]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// delete drafts
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"DELETE FROM drafts WHERE owner = $1",
|
||||
&[&(id as i64)]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// delete posts
|
||||
let res = execute!(&conn, "DELETE FROM posts WHERE owner = $1", &[&(id as i64)]);
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ impl DataManager {
|
|||
execute!(&conn, common::CREATE_TABLE_UPLOADS).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_EMOJIS).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_STACKS).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_DRAFTS).unwrap();
|
||||
|
||||
self.2
|
||||
.set("atto.active_connections:users".to_string(), "0".to_string())
|
||||
|
|
185
crates/core/src/database/drafts.rs
Normal file
185
crates/core/src/database/drafts.rs
Normal file
|
@ -0,0 +1,185 @@
|
|||
use super::*;
|
||||
use crate::cache::Cache;
|
||||
use crate::model::moderation::AuditLogEntry;
|
||||
use crate::model::{Error, Result, auth::User, communities::PostDraft, permissions::FinePermission};
|
||||
use crate::{auto_method, execute, get, query_row, query_rows, params};
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
use rusqlite::Row;
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
use tokio_postgres::Row;
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`PostDraft`] from an SQL row.
|
||||
pub(crate) fn get_draft_from_row(
|
||||
#[cfg(feature = "sqlite")] x: &Row<'_>,
|
||||
#[cfg(feature = "postgres")] x: &Row,
|
||||
) -> PostDraft {
|
||||
PostDraft {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
content: get!(x->2(String)),
|
||||
owner: get!(x->3(i64)) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_draft_by_id()@get_draft_from_row -> "SELECT * FROM drafts WHERE id = $1" --name="draft" --returns=PostDraft --cache-key-tmpl="atto.draft:{}");
|
||||
|
||||
/// Get all drafts from the given user (from most recent, paginated).
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the user the requested drafts belong to
|
||||
/// * `batch` - the limit of posts in each page
|
||||
/// * `page` - the page number
|
||||
pub async fn get_drafts_by_user(
|
||||
&self,
|
||||
id: usize,
|
||||
batch: usize,
|
||||
page: usize,
|
||||
) -> Result<Vec<PostDraft>> {
|
||||
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 drafts WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
|
||||
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|
||||
|x| { Self::get_draft_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("draft".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Get all drafts from the given user (from most recent).
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the user the requested drafts belong to
|
||||
pub async fn get_drafts_by_user_all(&self, id: usize) -> Result<Vec<PostDraft>> {
|
||||
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 drafts WHERE owner = $1 ORDER BY created DESC",
|
||||
&[&(id as i64)],
|
||||
|x| { Self::get_draft_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("draft".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Create a new post draft in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`PostDraft`] object to insert
|
||||
pub async fn create_draft(&self, data: PostDraft) -> Result<usize> {
|
||||
// check values
|
||||
if data.content.len() < 2 {
|
||||
return Err(Error::DataTooShort("content".to_string()));
|
||||
} else if data.content.len() > 4096 {
|
||||
return Err(Error::DataTooLong("content".to_string()));
|
||||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO drafts VALUES ($1, $2, $3, $4)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
&data.content,
|
||||
&(data.owner as i64),
|
||||
]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
Ok(data.id)
|
||||
}
|
||||
|
||||
pub async fn delete_draft(&self, id: usize, user: User) -> Result<()> {
|
||||
let y = self.get_draft_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 `delete_draft` 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, "DELETE FROM drafts WHERE id = $1", &[&(id as i64)]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!("atto.draft:{}", id)).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_draft_content(&self, id: usize, user: User, x: String) -> Result<()> {
|
||||
let y = self.get_draft_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_draft_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 drafts SET content = $1 WHERE id = $2",
|
||||
params![&x, &(id as i64)]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -18,3 +18,4 @@ pub const CREATE_TABLE_MESSAGES: &str = include_str!("./sql/create_messages.sql"
|
|||
pub const CREATE_TABLE_UPLOADS: &str = include_str!("./sql/create_uploads.sql");
|
||||
pub const CREATE_TABLE_EMOJIS: &str = include_str!("./sql/create_emojis.sql");
|
||||
pub const CREATE_TABLE_STACKS: &str = include_str!("./sql/create_stacks.sql");
|
||||
pub const CREATE_TABLE_DRAFTS: &str = include_str!("./sql/create_drafts.sql");
|
||||
|
|
6
crates/core/src/database/drivers/sql/create_drafts.sql
Normal file
6
crates/core/src/database/drivers/sql/create_drafts.sql
Normal file
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE IF NOT EXISTS drafts (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
created BIGINT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
owner BIGINT NOT NULL
|
||||
)
|
|
@ -3,6 +3,7 @@ mod auth;
|
|||
mod common;
|
||||
mod communities;
|
||||
pub mod connections;
|
||||
mod drafts;
|
||||
mod drivers;
|
||||
mod emojis;
|
||||
mod ipbans;
|
||||
|
|
|
@ -918,10 +918,10 @@ impl DataManager {
|
|||
}
|
||||
}
|
||||
|
||||
/// Create a new journal entry in the database.
|
||||
/// Create a new post in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`JournalEntry`] object to insert
|
||||
/// * `data` - a mock [`Post`] object to insert
|
||||
pub async fn create_post(&self, mut data: Post) -> Result<usize> {
|
||||
// check values (if this isn't reposting something else)
|
||||
let is_reposting = if let Some(ref repost) = data.context.repost {
|
||||
|
|
|
@ -349,3 +349,23 @@ pub struct QuestionContext {
|
|||
#[serde(default)]
|
||||
pub is_nsfw: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct PostDraft {
|
||||
pub id: usize,
|
||||
pub created: usize,
|
||||
pub content: String,
|
||||
pub owner: usize,
|
||||
}
|
||||
|
||||
impl PostDraft {
|
||||
/// Create a new [`PostDraft`].
|
||||
pub fn new(content: String, owner: usize) -> Self {
|
||||
Self {
|
||||
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
|
||||
created: unix_epoch_timestamp() as usize,
|
||||
content,
|
||||
owner,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue