new functions

This commit is contained in:
Mateusz Gruszczyński
2026-07-21 23:58:01 +02:00
parent fd2641780b
commit fa8a0d687f
18 changed files with 478 additions and 79 deletions
+43 -2
View File
@@ -27,6 +27,7 @@ pub struct PublicPageResponse {
title: String,
content: String,
updated_at: String,
allow_task_updates: bool,
}
#[derive(Debug, Deserialize)]
@@ -48,6 +49,20 @@ pub struct PasswordRequest {
password: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct PublishRequest {
#[serde(default)]
password: Option<String>,
#[serde(default)]
allow_task_updates: bool,
}
#[derive(Debug, Deserialize)]
pub struct PublicTaskUpdateRequest {
source_line: usize,
checked: bool,
}
#[derive(Debug, Deserialize)]
pub struct CreateNoteRequest {
name: String,
@@ -100,6 +115,7 @@ pub struct NoteInfo {
title: String,
protected: bool,
note_protected: bool,
allow_public_task_updates: bool,
created_at: String,
updated_at: String,
}
@@ -206,6 +222,7 @@ pub async fn note_info(
title: note.title,
protected: workspace.password_hash.is_some(),
note_protected: note.protected,
allow_public_task_updates: db::note_public_task_updates(&state.db, note.id).await?,
created_at: db::normalize_timestamp(&note.created_at),
updated_at: db::normalize_timestamp(&note.updated_at),
}))
@@ -374,6 +391,7 @@ pub struct PadInfo {
slug: String,
title: String,
protected: bool,
allow_public_task_updates: bool,
created_at: String,
updated_at: String,
}
@@ -410,6 +428,7 @@ pub async fn pad_info(
slug: pad.slug,
title: pad.title,
protected: pad.password_hash.is_some(),
allow_public_task_updates: db::pad_public_task_updates(&state.db, pad.id).await?,
created_at: db::normalize_timestamp(&pad.created_at),
updated_at: db::normalize_timestamp(&pad.updated_at),
}))
@@ -418,20 +437,22 @@ pub async fn pad_info(
pub async fn publish_pad_page(
State(state): State<SharedState>,
Path(slug): Path<String>,
Json(payload): Json<PasswordRequest>,
Json(payload): Json<PublishRequest>,
) -> Result<Json<PublishResponse>, ApiError> {
let pad = authorized_pad(&state, &slug, payload.password.as_deref()).await?;
let token = db::publish_pad(&state.db, pad.id).await?;
db::set_pad_public_task_updates(&state.db, pad.id, payload.allow_task_updates).await?;
Ok(Json(PublishResponse { url: format!("/s/{token}") }))
}
pub async fn publish_note_page(
State(state): State<SharedState>,
Path((workspace_slug, note_slug)): Path<(String, String)>,
Json(payload): Json<PasswordRequest>,
Json(payload): Json<PublishRequest>,
) -> Result<Json<PublishResponse>, ApiError> {
let (_, note) = authorized_note(&state, &workspace_slug, &note_slug, payload.password.as_deref()).await?;
let token = db::publish_note(&state.db, note.id).await?;
db::set_note_public_task_updates(&state.db, note.id, payload.allow_task_updates).await?;
Ok(Json(PublishResponse { url: format!("/s/{token}") }))
}
@@ -446,6 +467,23 @@ pub async fn public_page(
title: page.title,
content: page.content,
updated_at: db::normalize_timestamp(&page.updated_at),
allow_task_updates: page.allow_task_updates,
}))
}
pub async fn update_public_task(
State(state): State<SharedState>,
Path(token): Path<String>,
Json(payload): Json<PublicTaskUpdateRequest>,
) -> Result<Json<PublicPageResponse>, ApiError> {
let current = db::find_published_page(&state.db, &token).await?.ok_or_else(ApiError::not_found_note)?;
if !current.allow_task_updates { return Err(ApiError::forbidden("Task updates are disabled for this page")); }
let page = db::update_public_task(&state.db, &token, payload.source_line, payload.checked).await?.ok_or_else(ApiError::not_found_note)?;
Ok(Json(PublicPageResponse {
title: page.title,
content: page.content,
updated_at: db::normalize_timestamp(&page.updated_at),
allow_task_updates: page.allow_task_updates,
}))
}
@@ -767,6 +805,9 @@ impl ApiError {
message: "Invalid password".into(),
}
}
fn forbidden(message: &str) -> Self {
Self { status: StatusCode::FORBIDDEN, message: message.into() }
}
fn not_found_workspace() -> Self {
Self {
status: StatusCode::NOT_FOUND,