split rs files

This commit is contained in:
Mateusz Gruszczyński
2026-07-27 23:27:56 +02:00
parent d6c1c52310
commit 9695b2b739
15 changed files with 1694 additions and 1645 deletions
+732 -4
View File
@@ -1,3 +1,13 @@
mod access_tokens;
mod error;
mod files;
mod pads_public;
pub use access_tokens::*;
pub use error::ApiError;
pub use files::*;
pub use pads_public::*;
use axum::{
Json,
extract::{Multipart, Path, State},
@@ -20,8 +30,726 @@ const MIN_PASSWORD_LENGTH: usize = 8;
const MAX_PASSWORD_LENGTH: usize = 128;
const MIN_WORKSPACE_SLUG_LENGTH: usize = 6;
fn bearer_token(headers: &HeaderMap) -> Option<&str> {
headers
.get(header::AUTHORIZATION)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.strip_prefix("Bearer "))
.map(str::trim)
.filter(|value| !value.is_empty())
}
#[derive(Debug, Serialize)]
pub struct PublishResponse {
url: String,
}
#[derive(Debug, Serialize)]
pub struct PublicPageResponse {
title: String,
content: String,
updated_at: String,
allow_task_updates: bool,
}
#[derive(Debug, Deserialize)]
pub struct CreateWorkspaceRequest {
name: String,
#[serde(default)]
password: Option<String>,
}
#[derive(Debug, Serialize)]
pub struct CreateWorkspaceResponse {
slug: String,
url: String,
}
#[derive(Debug, Deserialize)]
pub struct PasswordRequest {
#[serde(default)]
password: Option<String>,
#[serde(default)]
access_token: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct PublishRequest {
#[serde(default)]
password: Option<String>,
#[serde(default)]
access_token: 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,
#[serde(default)]
password: Option<String>,
#[serde(default)]
access_token: Option<String>,
#[serde(default)]
protect: bool,
#[serde(default)]
created_by: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct RestoreRequest {
#[serde(default)]
password: Option<String>,
#[serde(default)]
access_token: Option<String>,
revision_id: i64,
}
#[derive(Debug, Serialize)]
pub struct WorkspaceInfo {
slug: String,
title: String,
protected: bool,
created_at: String,
updated_at: String,
}
#[derive(Debug, Serialize)]
pub struct WorkspaceOpenResponse {
workspace: WorkspaceInfo,
notes: Vec<NoteListItem>,
}
#[derive(Debug, Serialize)]
pub struct NoteListItem {
slug: String,
title: String,
created_at: String,
updated_at: String,
url: String,
protected: bool,
created_by: Option<String>,
}
#[derive(Debug, Serialize)]
pub struct NoteInfo {
workspace_slug: String,
workspace_title: String,
slug: String,
title: String,
protected: bool,
note_protected: bool,
allow_public_task_updates: bool,
created_at: String,
updated_at: String,
can_delete_files: bool,
global_color: Option<String>,
note_color: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct EditorColorRequest {
color: Option<String>,
}
pub async fn create_workspace(
State(state): State<SharedState>,
headers: HeaderMap,
Json(payload): Json<CreateWorkspaceRequest>,
) -> Result<(StatusCode, Json<CreateWorkspaceResponse>), ApiError> {
let title = validate_name(&payload.name, "Workspace name")?;
let password = validate_password(payload.password.as_deref())?;
let slug = unique_workspace_slug(&state, title).await?;
let workspace = db::create_workspace(&state.db, &slug, title, password).await?;
if let Some(user) = crate::auth::optional_user(&state, &headers)
.await
.map_err(|e| ApiError::forbidden(&e.message))?
{
sqlx::query(queries::get(
state.db.kind(),
queries::USER_ATTACH_WORKSPACE,
))
.bind(user.id)
.bind(&workspace.slug)
.execute(state.db.pool())
.await?;
}
Ok((
StatusCode::CREATED,
Json(CreateWorkspaceResponse {
url: format!("/w/{slug}"),
slug,
}),
))
}
pub async fn workspace_info(
State(state): State<SharedState>,
headers: HeaderMap,
Path(workspace_slug): Path<String>,
) -> Result<Json<WorkspaceInfo>, ApiError> {
let workspace = db::find_workspace(&state.db, &workspace_slug)
.await?
.ok_or_else(ApiError::not_found_workspace)?;
ensure_private_resource_access(
&state,
"workspace",
&workspace.slug,
workspace.is_private,
bearer_token(&headers),
)
.await?;
Ok(Json(workspace_info_from(&workspace)))
}
pub async fn open_workspace(
State(state): State<SharedState>,
headers: HeaderMap,
Path(workspace_slug): Path<String>,
Json(payload): Json<PasswordRequest>,
) -> Result<Json<WorkspaceOpenResponse>, ApiError> {
let workspace = authorized_workspace(
&state,
&workspace_slug,
payload.password.as_deref(),
payload.access_token.as_deref(),
bearer_token(&headers),
)
.await?;
let notes = db::list_notes(&state.db, workspace.id)
.await?
.into_iter()
.map(|note| NoteListItem {
url: format!("/w/{}/n/{}", workspace.slug, note.slug),
slug: note.slug,
title: note.title,
created_at: db::normalize_timestamp(&note.created_at),
updated_at: db::normalize_timestamp(&note.updated_at),
protected: note.protected,
created_by: note.created_by,
})
.collect();
Ok(Json(WorkspaceOpenResponse {
workspace: workspace_info_from(&workspace),
notes,
}))
}
pub async fn create_note(
State(state): State<SharedState>,
headers: HeaderMap,
Path(workspace_slug): Path<String>,
Json(payload): Json<CreateNoteRequest>,
) -> Result<(StatusCode, Json<NoteListItem>), ApiError> {
let workspace = authorized_workspace(
&state,
&workspace_slug,
payload.password.as_deref(),
payload.access_token.as_deref(),
bearer_token(&headers),
)
.await?;
let level = if db::verify_workspace_password(&workspace, payload.password.as_deref())
|| (workspace.is_private == 0 && workspace.password_hash.is_none())
{
AccessLevel::Write
} else {
combined_token_access_level(
&state,
"workspace",
&workspace_slug,
payload.access_token.as_deref(),
bearer_token(&headers),
)
.await?
};
require_write(level)?;
let title = validate_name(&payload.name, "Note name")?;
let base = slugify(title);
if base.is_empty() {
return Err(ApiError::bad_request(
"The name cannot be converted into a valid address",
));
}
let slug = unique_note_slug(&state, workspace.id, &base).await?;
let created_by = payload
.created_by
.as_deref()
.map(str::trim)
.filter(|v| !v.is_empty())
.map(|v| v.chars().take(40).collect::<String>());
let note = db::create_note(
&state.db,
workspace.id,
&slug,
title,
payload.protect,
created_by.as_deref(),
)
.await?;
Ok((
StatusCode::CREATED,
Json(NoteListItem {
url: format!("/w/{workspace_slug}/n/{slug}"),
slug: note.slug,
title: note.title,
created_at: db::normalize_timestamp(&note.created_at),
updated_at: db::normalize_timestamp(&note.updated_at),
protected: note.protected,
created_by: note.created_by,
}),
))
}
fn clean_editor_color(value: Option<&str>) -> Result<Option<String>, ApiError> {
let Some(value) = value else {
return Ok(None);
};
let value = value.trim();
if value.len() == 7
&& value.starts_with('#')
&& value[1..].chars().all(|c| c.is_ascii_hexdigit())
{
Ok(Some(value.to_ascii_lowercase()))
} else {
Err(ApiError::bad_request("Invalid editor color"))
}
}
async fn editor_colors(
state: &SharedState,
headers: &HeaderMap,
kind: &str,
slug: &str,
) -> Result<(Option<String>, Option<String>), ApiError> {
let Some(user) = crate::auth::optional_user(state, headers)
.await
.map_err(|e| ApiError::forbidden(&e.message))?
else {
return Ok((None, None));
};
let global: Option<String> = sqlx::query_scalar(queries::get(
state.db.kind(),
queries::AUTH_EDITOR_COLOR_BY_USER,
))
.bind(user.id)
.fetch_one(state.db.pool())
.await?;
let note: Option<String> = sqlx::query_scalar(queries::get(
state.db.kind(),
queries::RESOURCE_COLOR_BY_USER,
))
.bind(user.id)
.bind(kind)
.bind(slug)
.fetch_optional(state.db.pool())
.await?;
Ok((global, note))
}
async fn save_editor_color(
state: &SharedState,
headers: &HeaderMap,
kind: &str,
slug: &str,
color: Option<&str>,
) -> Result<Json<serde_json::Value>, ApiError> {
let user = crate::auth::optional_user(state, headers)
.await
.map_err(|e| ApiError::forbidden(&e.message))?
.ok_or_else(|| ApiError::forbidden("Log in to save note colors"))?;
let color = clean_editor_color(color)?;
let mut tx = state.db.pool().begin().await?;
sqlx::query(queries::get(
state.db.kind(),
queries::RESOURCE_COLOR_DELETE,
))
.bind(user.id)
.bind(kind)
.bind(slug)
.execute(&mut *tx)
.await?;
if let Some(value) = color.as_deref() {
sqlx::query(queries::get(
state.db.kind(),
queries::RESOURCE_COLOR_INSERT,
))
.bind(user.id)
.bind(kind)
.bind(slug)
.bind(value)
.execute(&mut *tx)
.await?;
}
tx.commit().await?;
Ok(Json(serde_json::json!({"color": color})))
}
pub async fn note_info(
State(state): State<SharedState>,
headers: HeaderMap,
Path((workspace_slug, note_slug)): Path<(String, String)>,
) -> Result<Json<NoteInfo>, ApiError> {
let workspace = db::find_workspace(&state.db, &workspace_slug)
.await?
.ok_or_else(ApiError::not_found_workspace)?;
ensure_private_resource_access(
&state,
"workspace",
&workspace.slug,
workspace.is_private,
bearer_token(&headers),
)
.await?;
let note = db::find_note(&state.db, workspace.id, &note_slug)
.await?
.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?;
Ok(Json(NoteInfo {
workspace_slug: workspace.slug,
workspace_title: workspace.title,
slug: note.slug,
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),
can_delete_files: {
let workspace_owner = crate::auth::is_resource_owner(
&state,
"workspace",
&workspace_slug,
bearer_token(&headers),
)
.await
.unwrap_or(false);
let note_owner = crate::auth::optional_user(&state, &headers)
.await
.ok()
.flatten()
.and_then(|user| {
note.created_by
.as_deref()
.map(|creator| creator == user.nickname)
})
.unwrap_or(false);
workspace_owner || note_owner
},
global_color,
note_color,
}))
}
pub async fn history(
State(state): State<SharedState>,
headers: HeaderMap,
Path((workspace_slug, note_slug)): Path<(String, String)>,
Json(payload): Json<PasswordRequest>,
) -> Result<Json<Vec<db::Revision>>, ApiError> {
let (workspace, note) = authorized_note(
&state,
&workspace_slug,
&note_slug,
payload.password.as_deref(),
payload.access_token.as_deref(),
bearer_token(&headers),
)
.await?;
let _ = workspace;
let revisions = db::list_revisions(&state.db, note.id)
.await?
.into_iter()
.map(|mut revision| {
revision.created_at = db::normalize_timestamp(&revision.created_at);
revision
})
.collect();
Ok(Json(revisions))
}
pub async fn restore(
State(state): State<SharedState>,
headers: HeaderMap,
Path((workspace_slug, note_slug)): Path<(String, String)>,
Json(payload): Json<RestoreRequest>,
) -> Result<Json<serde_json::Value>, ApiError> {
let (workspace, note) = authorized_note(
&state,
&workspace_slug,
&note_slug,
payload.password.as_deref(),
payload.access_token.as_deref(),
bearer_token(&headers),
)
.await?;
let level = if db::verify_workspace_password(&workspace, payload.password.as_deref())
|| (workspace.is_private == 0 && workspace.password_hash.is_none())
{
AccessLevel::Write
} else {
combined_token_access_level(
&state,
"workspace",
&workspace_slug,
payload.access_token.as_deref(),
bearer_token(&headers),
)
.await?
};
require_write(level)?;
let content: Option<String> = sqlx::query_scalar(queries::get(state.db.kind(), queries::Q028))
.bind(payload.revision_id)
.bind(note.id)
.fetch_optional(state.db.pool())
.await?;
let content = content.ok_or_else(ApiError::not_found_revision)?;
let (revision_id, updated_at) = db::save_revision(
&state.db,
note.id,
workspace.id,
&content,
Some("restore"),
"[]",
)
.await?;
let update = NoteUpdate {
content,
revision_id,
updated_at,
author: Some("restore".into()),
owner_map: "[]".into(),
};
let _ = state
.note_channel(&workspace_slug, &note_slug)
.await
.send(RoomEvent::Document(update));
Ok(Json(serde_json::json!({"ok": true})))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum AccessLevel {
None,
Read,
Write,
}
fn permission_level(permission: Option<&str>) -> AccessLevel {
match permission {
Some("rw") => AccessLevel::Write,
Some("ro") => AccessLevel::Read,
_ => AccessLevel::None,
}
}
async fn anonymous_access_token_valid(
state: &SharedState,
kind: &str,
slug: &str,
token: Option<&str>,
) -> Result<bool, ApiError> {
let Some(token) = token.map(str::trim).filter(|value| !value.is_empty()) else {
return Ok(false);
};
let count: i64 = sqlx::query_scalar(queries::get(
state.db.kind(),
queries::RESOURCE_ACCESS_TOKENS_VALID_COUNT,
))
.bind(access_tokens::hash_access_token(token))
.bind(kind)
.bind(slug)
.bind(Utc::now().to_rfc3339())
.fetch_one(state.db.pool())
.await?;
Ok(count > 0)
}
async fn token_access_level(
state: &SharedState,
kind: &str,
slug: &str,
token: Option<&str>,
) -> Result<AccessLevel, ApiError> {
let permission = crate::auth::resource_permission(state, kind, slug, token)
.await
.map_err(|error| ApiError::forbidden(&error.message))?;
let level = permission_level(permission.as_deref());
if level != AccessLevel::None {
return Ok(level);
}
if anonymous_access_token_valid(state, kind, slug, token).await? {
// A server-issued token created after a correct resource password
// retains the historical read/write semantics of password access.
return Ok(AccessLevel::Write);
}
Ok(AccessLevel::None)
}
async fn combined_token_access_level(
state: &SharedState,
kind: &str,
slug: &str,
access_token: Option<&str>,
bearer: Option<&str>,
) -> Result<AccessLevel, ApiError> {
Ok(std::cmp::max(
token_access_level(state, kind, slug, access_token).await?,
token_access_level(state, kind, slug, bearer).await?,
))
}
fn require_write(level: AccessLevel) -> Result<(), ApiError> {
if level >= AccessLevel::Write {
Ok(())
} else {
Err(ApiError::forbidden("Read-only access."))
}
}
async fn ensure_private_resource_access(
state: &SharedState,
kind: &str,
slug: &str,
is_private: i64,
token: Option<&str>,
) -> Result<(), ApiError> {
if is_private == 0 {
return Ok(());
}
if verify_resource_access_token(state, kind, slug, token).await? {
return Ok(());
}
Err(ApiError::forbidden("This resource is private."))
}
pub async fn authorized_workspace(
state: &SharedState,
slug: &str,
password: Option<&str>,
access_token: Option<&str>,
bearer: Option<&str>,
) -> Result<db::Workspace, ApiError> {
let workspace = db::find_workspace(&state.db, slug)
.await?
.ok_or_else(ApiError::not_found_workspace)?;
let token_level =
combined_token_access_level(state, "workspace", slug, access_token, bearer).await?;
if workspace.is_private != 0 && token_level == AccessLevel::None {
return Err(ApiError::forbidden("This workspace is private."));
}
if workspace.password_hash.is_some()
&& !db::verify_workspace_password(&workspace, password)
&& token_level == AccessLevel::None
{
return Err(ApiError::unauthorized());
}
Ok(workspace)
}
async fn authorized_note(
state: &SharedState,
workspace_slug: &str,
note_slug: &str,
password: Option<&str>,
access_token: Option<&str>,
bearer: Option<&str>,
) -> Result<(db::Workspace, db::Note), ApiError> {
let workspace =
authorized_workspace(state, workspace_slug, password, access_token, bearer).await?;
let note = db::find_note(&state.db, workspace.id, note_slug)
.await?
.ok_or_else(ApiError::not_found_note)?;
Ok((workspace, note))
}
fn workspace_info_from(workspace: &db::Workspace) -> WorkspaceInfo {
WorkspaceInfo {
slug: workspace.slug.clone(),
title: workspace.title.clone(),
protected: workspace.password_hash.is_some(),
created_at: db::normalize_timestamp(&workspace.created_at),
updated_at: db::normalize_timestamp(&workspace.updated_at),
}
}
fn validate_name<'a>(value: &'a str, field: &str) -> Result<&'a str, ApiError> {
let value = value.trim();
if value.is_empty() || value.chars().count() > MAX_NAME_LENGTH {
return Err(ApiError::bad_request(&format!(
"{field} must contain between 1 and {MAX_NAME_LENGTH} characters"
)));
}
Ok(value)
}
fn validate_password(password: Option<&str>) -> Result<Option<&str>, ApiError> {
let Some(password) = password.map(str::trim).filter(|value| !value.is_empty()) else {
return Ok(None);
};
let length = password.chars().count();
if !(MIN_PASSWORD_LENGTH..=MAX_PASSWORD_LENGTH).contains(&length) {
return Err(ApiError::bad_request(
"Password must contain between 8 and 128 characters",
));
}
Ok(Some(password))
}
async fn unique_workspace_slug(state: &SharedState, title: &str) -> Result<String, ApiError> {
let base = slugify(title);
if base.is_empty() {
return Err(ApiError::bad_request(
"The name cannot be converted into a valid address",
));
}
let needs_suffix = base.chars().count() < MIN_WORKSPACE_SLUG_LENGTH
|| db::find_workspace(&state.db, &base).await?.is_some();
if !needs_suffix {
return Ok(base);
}
for _ in 0..8 {
let candidate = format!("{base}-{}", db::random_suffix(8));
if db::find_workspace(&state.db, &candidate).await?.is_none() {
return Ok(candidate);
}
}
Err(ApiError::internal("Failed to create a unique address"))
}
async fn unique_note_slug(
state: &SharedState,
workspace_id: i64,
base: &str,
) -> Result<String, ApiError> {
if db::find_note(&state.db, workspace_id, base)
.await?
.is_none()
{
return Ok(base.to_owned());
}
for _ in 0..8 {
let candidate = format!("{base}-{}", db::random_suffix(6));
if db::find_note(&state.db, workspace_id, &candidate)
.await?
.is_none()
{
return Ok(candidate);
}
}
Err(ApiError::internal("Failed to create a unique address"))
}
include!("workspace_notes.rs");
include!("pads_public.rs");
include!("files.rs");
include!("access_tokens.rs");