new functions
This commit is contained in:
+65
-1
@@ -41,7 +41,8 @@ fn bearer_token(headers: &HeaderMap) -> Option<&str> {
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct PublishResponse {
|
||||
url: String,
|
||||
url: Option<String>,
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@@ -83,6 +84,7 @@ pub struct PublishRequest {
|
||||
allow_task_updates: bool,
|
||||
#[serde(default)]
|
||||
unprotect_page: bool,
|
||||
enabled: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -153,11 +155,16 @@ pub struct NoteInfo {
|
||||
note_protected: bool,
|
||||
allow_public_task_updates: bool,
|
||||
public_page_unprotected: bool,
|
||||
public_page_enabled: bool,
|
||||
private: bool,
|
||||
created_at: String,
|
||||
updated_at: String,
|
||||
can_delete_files: bool,
|
||||
global_color: Option<String>,
|
||||
note_color: Option<String>,
|
||||
authorship_mode: String,
|
||||
colors_enabled: bool,
|
||||
can_save_editor_settings: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -165,6 +172,44 @@ pub struct EditorColorRequest {
|
||||
color: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct EditorSettingsRequest {
|
||||
authorship_mode: String,
|
||||
colors_enabled: bool,
|
||||
}
|
||||
|
||||
async fn editor_settings(state: &SharedState, kind: &str, slug: &str) -> Result<(String, bool), ApiError> {
|
||||
let row: Option<(String, i64)> = sqlx::query_as(queries::get(
|
||||
state.db.kind(),
|
||||
queries::RESOURCE_EDITOR_SETTINGS_SELECT,
|
||||
))
|
||||
.bind(kind)
|
||||
.bind(slug)
|
||||
.fetch_optional(state.db.pool())
|
||||
.await?;
|
||||
Ok(row.map(|(mode, colors)| (if mode == "full" { "full".into() } else { "simple".into() }, colors != 0))
|
||||
.unwrap_or_else(|| ("simple".into(), true)))
|
||||
}
|
||||
|
||||
async fn save_editor_settings(
|
||||
state: &SharedState, headers: &HeaderMap, permission_kind: &str, permission_slug: &str,
|
||||
settings_kind: &str, settings_slug: &str, payload: EditorSettingsRequest,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
let permission = crate::auth::resource_permission(state, permission_kind, permission_slug, bearer_token(headers))
|
||||
.await.map_err(|e| ApiError::forbidden(&e.message))?;
|
||||
if permission.as_deref() != Some("rw") {
|
||||
return Err(ApiError::forbidden("Read and write access is required to save editor settings"));
|
||||
}
|
||||
let mode = match payload.authorship_mode.as_str() { "simple" => "simple", "full" | "advanced" => "full", _ => return Err(ApiError::bad_request("Invalid authorship mode")) };
|
||||
let mut tx = state.db.pool().begin().await?;
|
||||
sqlx::query(queries::get(state.db.kind(), queries::RESOURCE_EDITOR_SETTINGS_DELETE))
|
||||
.bind(settings_kind).bind(settings_slug).execute(&mut *tx).await?;
|
||||
sqlx::query(queries::get(state.db.kind(), queries::RESOURCE_EDITOR_SETTINGS_INSERT))
|
||||
.bind(settings_kind).bind(settings_slug).bind(mode).bind(payload.colors_enabled).execute(&mut *tx).await?;
|
||||
tx.commit().await?;
|
||||
Ok(Json(serde_json::json!({"authorship_mode": mode, "colors_enabled": payload.colors_enabled})))
|
||||
}
|
||||
|
||||
pub async fn create_workspace(
|
||||
State(state): State<SharedState>,
|
||||
headers: HeaderMap,
|
||||
@@ -439,7 +484,12 @@ pub async fn note_info(
|
||||
.ok_or_else(ApiError::not_found_note)?;
|
||||
let color_slug = format!("{}/{}", workspace_slug, note_slug);
|
||||
let (global_color, note_color) = editor_colors(&state, &headers, "note", &color_slug).await?;
|
||||
let (authorship_mode, colors_enabled) = editor_settings(&state, "note", &color_slug).await?;
|
||||
let can_save_editor_settings = crate::auth::resource_permission(&state, "workspace", &workspace_slug, bearer_token(&headers)).await.ok().flatten().as_deref() == Some("rw");
|
||||
|
||||
if workspace.is_private == 0 && !db::note_public_page_disabled(&state.db, note.id).await? && !db::note_public_page_enabled(&state.db, note.id).await? {
|
||||
db::publish_note(&state.db, note.id).await?;
|
||||
}
|
||||
Ok(Json(NoteInfo {
|
||||
workspace_slug: workspace.slug,
|
||||
workspace_title: workspace.title,
|
||||
@@ -449,6 +499,8 @@ pub async fn note_info(
|
||||
note_protected: note.protected,
|
||||
allow_public_task_updates: db::note_public_task_updates(&state.db, note.id).await?,
|
||||
public_page_unprotected: db::note_public_page_unprotected(&state.db, note.id).await?,
|
||||
public_page_enabled: db::note_public_page_enabled(&state.db, note.id).await?,
|
||||
private: workspace.is_private != 0,
|
||||
created_at: db::normalize_timestamp(¬e.created_at),
|
||||
updated_at: db::normalize_timestamp(¬e.updated_at),
|
||||
can_delete_files: {
|
||||
@@ -474,9 +526,21 @@ pub async fn note_info(
|
||||
},
|
||||
global_color,
|
||||
note_color,
|
||||
authorship_mode,
|
||||
colors_enabled,
|
||||
can_save_editor_settings,
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn set_note_editor_settings(
|
||||
State(state): State<SharedState>, headers: HeaderMap,
|
||||
Path((workspace_slug, note_slug)): Path<(String, String)>,
|
||||
Json(payload): Json<EditorSettingsRequest>,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
let settings_slug = format!("{}/{}", workspace_slug, note_slug);
|
||||
save_editor_settings(&state, &headers, "workspace", &workspace_slug, "note", &settings_slug, payload).await
|
||||
}
|
||||
|
||||
pub async fn history(
|
||||
State(state): State<SharedState>,
|
||||
headers: HeaderMap,
|
||||
|
||||
Reference in New Issue
Block a user