bc0b258e04
Sidecars (*.comments.json) stay out of the index and listings and follow rename and delete of their note; end-to-end comments test over a stub server (Ana comments and mentions Bob, Bob resolves, second sync changes nothing); viewer extension tables gain ico, ogv, 3gp, opus, aac; annotate crate: Edit::Replace for undoable text moves and edits, line items carry an arrow flag; syntax test fixed (TeX is a bundled grammar). Built by MiniMax M3.1 Flash and Claude Sonnet; verified with the full cargo xtask ci on the combined tree (4422 tests, clippy -D warnings, layers, tree, i18n). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
78 lines
3.5 KiB
Rust
78 lines
3.5 KiB
Rust
//! The files of the fake: the change log, the bytes of a note and the writes
|
|
//! of a device.
|
|
//!
|
|
//! The files go through the ledger of `notescraft-sync`, so the decisions about
|
|
//! a stale writer, an unchanged file and a conflict are the ones the real
|
|
//! server makes.
|
|
|
|
use notescraft_sync::{Outcome, Write, hash_hex};
|
|
use serde_json::{Value, json};
|
|
|
|
use super::{Reply, State, header_number, query_number, user_of};
|
|
use crate::comments_e2e_stub::feed;
|
|
use crate::comments_e2e_stub::wire::Request;
|
|
|
|
/// Most records one page of the change log holds.
|
|
const MAX_RECORDS: u64 = 5000;
|
|
|
|
/// The change log of the vault.
|
|
pub(crate) fn changes(state: &State, query: &str) -> Reply {
|
|
let since = query_number(query, "since").unwrap_or(0);
|
|
let limit =
|
|
usize::try_from(query_number(query, "limit").unwrap_or(1000).clamp(1, MAX_RECORDS)).unwrap_or(usize::try_from(MAX_RECORDS).unwrap_or(usize::MAX));
|
|
let page = state.ledger.changes_since(since, limit);
|
|
Reply::json(200, &serde_json::to_value(&page).unwrap_or(Value::Null))
|
|
}
|
|
|
|
/// The route of one file of the vault.
|
|
pub(crate) fn file_route(state: &mut State, request: &Request, path: &str) -> Reply {
|
|
match request.method.as_str() {
|
|
"GET" => get_file(state, path),
|
|
"PUT" => put_file(state, request, path),
|
|
"DELETE" => delete_file(state, request, path),
|
|
_ => Reply::not_found(),
|
|
}
|
|
}
|
|
|
|
/// The bytes of a file of the vault.
|
|
fn get_file(state: &State, path: &str) -> Reply {
|
|
let live = state.ledger.get(path).is_some_and(|record| !record.deleted);
|
|
match (live, state.blobs.get(path)) {
|
|
(true, Some(bytes)) => Reply::bytes(200, bytes.clone()),
|
|
_ => Reply::json(404, &json!({ "error": "no such file" })),
|
|
}
|
|
}
|
|
|
|
/// Stores a file of the vault.
|
|
fn put_file(state: &mut State, request: &Request, path: &str) -> Reply {
|
|
let hash = hash_hex(&request.body);
|
|
let size = u64::try_from(request.body.len()).unwrap_or(u64::MAX);
|
|
let write =
|
|
Write { path: path.to_owned(), hash: Some(hash), size, mtime_ms: header_number(request, "x-mtime-ms"), base_seq: header_number(request, "x-base-seq") };
|
|
match state.ledger.apply(write) {
|
|
Ok(Outcome::Applied(record)) => {
|
|
state.blobs.insert(path.to_owned(), request.body.clone());
|
|
feed::derive_events(state, &user_of(request), path, &request.body);
|
|
Reply::json(201, &serde_json::to_value(&record).unwrap_or(Value::Null))
|
|
}
|
|
Ok(Outcome::Unchanged(record)) => Reply::json(200, &serde_json::to_value(&record).unwrap_or(Value::Null)),
|
|
Ok(Outcome::Conflict(current)) => Reply::json(409, &json!({ "current": current })),
|
|
Err(error) => Reply::json(400, &json!({ "error": error.to_string() })),
|
|
}
|
|
}
|
|
|
|
/// Removes a file of the vault.
|
|
fn delete_file(state: &mut State, request: &Request, path: &str) -> Reply {
|
|
let write =
|
|
Write { path: path.to_owned(), hash: None, size: 0, mtime_ms: header_number(request, "x-mtime-ms"), base_seq: header_number(request, "x-base-seq") };
|
|
match state.ledger.apply(write) {
|
|
Ok(Outcome::Applied(record)) => {
|
|
state.blobs.remove(path);
|
|
Reply::json(201, &serde_json::to_value(&record).unwrap_or(Value::Null))
|
|
}
|
|
Ok(Outcome::Unchanged(record)) => Reply::json(200, &serde_json::to_value(&record).unwrap_or(Value::Null)),
|
|
Ok(Outcome::Conflict(current)) => Reply::json(409, &json!({ "current": current })),
|
|
Err(error) => Reply::json(404, &json!({ "error": error.to_string() })),
|
|
}
|
|
}
|