Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions crates/engine/src/attachments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::Engine;
use crate::db::{Db, lock};
use crate::error::{AppError, StorageError};
use crate::index::Changed;
use crate::sensitive::Sensitive;
use files::{
directory, read_outside_lock, read_within_limit, remove_files, store_new, write_plain,
write_plaintext_copy,
Expand All @@ -31,14 +32,17 @@ pub async fn attach_file(
})
.await?;
let (file_name, bytes) = read?;
crate::db::run(engine, move |engine| {
let attachment = crate::db::run(engine, move |engine| {
engine.write(|db| {
let answer = store_new(note_id, file_name, &bytes, db)?;
let changed = Changed { attachments: true, ..Changed::default() };
Ok((answer, changed))
})
})
.await
.await?;
let name = Sensitive(&attachment.file_name);
log::info!("file {} {name} attached to {}", attachment.id, attachment.note_id);
Ok(attachment)
}

pub fn list_attachments(engine: &Engine, note_id: &str) -> Result<Vec<Attachment>, AppError> {
Expand Down Expand Up @@ -79,7 +83,9 @@ pub fn save_attachment(engine: &Engine, id: &str, path: &str) -> Result<(), AppE
let db = &engine.db;
let (_, bytes) = read_outside_lock(db, id)?;

Ok(write_plain(path, &bytes)?)
write_plain(path, &bytes)?;
log::info!("attachment {id} saved to {}", Sensitive(path));
Ok(())
}

/// An image the interface read, as raw RGBA, stored as a PNG.
Expand All @@ -90,21 +96,26 @@ pub fn attach_image(
(width, height): (u32, u32),
rgba: &[u8],
) -> Result<Attachment, AppError> {
engine.write(|db| {
let attachment = engine.write(|db| {
let png = model::encode_png(width, height, rgba)?;
model::validate_size(png.len() as u64, &model::png_name(file_name))?;
let answer = store_new(note_id, model::png_name(file_name), &png, db)?;
let changed = Changed { attachments: true, ..Changed::default() };
Ok((answer, changed))
})
})?;
let name = Sensitive(&attachment.file_name);
log::info!("image {} {name} attached to {}", attachment.id, attachment.note_id);
Ok(attachment)
}

pub fn delete_attachment(engine: &Engine, id: &str) -> Result<(), AppError> {
engine.write(|db| {
delete(db, id)?;
let changed = Changed { attachments: true, ..Changed::default() };
Ok(((), changed))
})
})?;
log::info!("attachment {id} deleted");
Ok(())
}

/// The record, then the file, which is removed outside the lock.
Expand Down
76 changes: 55 additions & 21 deletions crates/engine/src/folders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use crate::count::saturating_u32 as count;
use crate::db::lock;
use crate::error::AppError;
use crate::index::Changed;
use crate::logged::listed;
use crate::notes::model::{HeldNotes, NotePlacement};
use crate::sensitive::Sensitive;

use board::{
BoardArrangement, BoardQuery, BoardScope, BoardView, CardPlacement, Occupancy, PreviewLines,
Expand Down Expand Up @@ -69,7 +71,10 @@ pub fn save_board_layout(
let db = &engine.db;
let mut connection = lock(db)?;

Ok(store::board::save_layout(&mut connection, root_id.map(std::string::String::as_str), zones, cards)?)
store::board::save_layout(&mut connection, root_id.map(std::string::String::as_str), zones, cards)?;
let board = root_id.map_or("the space", String::as_str);
log::info!("board of {board} laid out: {} zones, {} cards", zones.len(), cards.len());
Ok(())
}

/// `None` = every space, like [`crate::notes::view::NotesQuery::space_id`].
Expand All @@ -81,7 +86,7 @@ pub fn list_folders(engine: &Engine, space_id: Option<&String>) -> Result<Vec<Fo
}

pub fn create_folder(engine: &Engine, draft: &FolderDraft) -> Result<Folder, AppError> {
engine.write(|db| {
let folder = engine.write(|db| {
let name = draft.validated_name()?;

let mut connection = lock(db)?;
Expand All @@ -90,29 +95,36 @@ pub fn create_folder(engine: &Engine, draft: &FolderDraft) -> Result<Folder, App
store::create(&mut connection, &draft.space_id, draft.parent_id.as_deref(), &name, Utc::now())?;
let changed = Changed { folders: true, ..Changed::default() };
Ok((answer, changed))
})
})?;
let name = Sensitive(&folder.name);
log::info!("created folder {} {name} in space {}", folder.id, folder.space_id);
Ok(folder)
}

pub fn rename_folder(engine: &Engine, id: &str, name: &str) -> Result<Folder, AppError> {
engine.write(|db| {
let folder = engine.write(|db| {
let name = model::validated_name(name)?;

let mut connection = lock(db)?;

let answer = store::rename(&mut connection, id, &name)?;
let changed = Changed { folders: true, ..Changed::default() };
Ok((answer, changed))
})
})?;
log::info!("folder {id} renamed {}", Sensitive(&folder.name));
Ok(folder)
}

pub fn recolour_folder(engine: &Engine, id: &str, colour: FolderColour) -> Result<Folder, AppError> {
engine.write(|db| {
let folder = engine.write(|db| {
let mut connection = lock(db)?;

let answer = store::recolour(&mut connection, id, colour)?;
let changed = Changed { folders: true, ..Changed::default() };
Ok((answer, changed))
})
})?;
log::info!("folder {id} recoloured {colour}");
Ok(folder)
}

/// Under a folder of `space_id`, `None` at its top, with everything it holds: into another
Expand All @@ -123,25 +135,31 @@ pub fn move_folder(
id: &str,
(space_id, parent_id): (&str, Option<&String>),
) -> Result<FolderMove, AppError> {
engine.write(|db| {
let was = engine.write(|db| {
let mut connection = lock(db)?;

let answer =
store::move_to(&mut connection, id, (space_id, parent_id.map(std::string::String::as_str)))?;
let changed = Changed { folders: true, ..Changed::notes(answer.notes.clone()) };
Ok((answer, changed))
})
})?;
let parent = parent_id.map_or("the top", String::as_str);
log::info!("folder {id} moved under {parent} of space {space_id}, {} notes with it", was.notes.len());
Ok(was)
}

/// The undo of [`move_folder`]; `false` when its old parent is gone or no longer a place for it.
pub fn move_folder_back(engine: &Engine, was: &FolderMove) -> Result<bool, AppError> {
engine.write(|db| {
let went = engine.write(|db| {
let mut connection = lock(db)?;

let went = store::move_back(&mut connection, was)?;
let changed = Changed { folders: true, ..Changed::notes(went.clone().unwrap_or_default()) };
Ok((went.is_some(), changed))
})
})?;
let back = if went { "moved back" } else { "left where it is, its place gone" };
log::info!("folder {} {back}", was.folder_id);
Ok(went)
}

/// What deleting the folder would keep or trash, counted under the lock: its live notes and its
Expand All @@ -163,14 +181,20 @@ pub fn delete_folder(
lines: PreviewLines,
held: HeldNotes,
) -> Result<u32, AppError> {
engine.write(|db| {
let held_where = match held {
HeldNotes::Keep => "handed to its parent",
HeldNotes::Trash => "to the trash",
};
let held_count = engine.write(|db| {
let mut connection = lock(db)?;

let trashed_at = (held == HeldNotes::Trash).then(Utc::now);
let held = store::delete(&mut connection, id, lines.card_height(), trashed_at)?;
let changed = Changed::everything();
Ok((count(held), changed))
})
})?;
log::info!("folder {id} deleted, its {held_count} notes {held_where}");
Ok(held_count)
}

/// A batch, like [`crate::notes::move_notes`]: the selection bar files a whole selection,
Expand All @@ -182,7 +206,7 @@ pub fn file_notes(
folder_id: Option<&String>,
lines: PreviewLines,
) -> Result<Vec<NoteFiling>, AppError> {
engine.write(|db| {
let filed = engine.write(|db| {
let mut connection = lock(db)?;

let answer = store::file_many(
Expand All @@ -193,7 +217,10 @@ pub fn file_notes(
)?;
let changed = Changed::notes(ids.to_vec());
Ok((answer, changed))
})
})?;
let folder = folder_id.map_or("no folder", String::as_str);
log::info!("{} filed into {folder}: {}", ids.len(), listed(ids));
Ok(filed)
}

/// Into a folder from whatever space each note is in, the folder's own becoming theirs. Undone
Expand All @@ -204,13 +231,15 @@ pub fn move_notes_into(
folder_id: &str,
lines: PreviewLines,
) -> Result<Vec<NotePlacement>, AppError> {
engine.write(|db| {
let placed = engine.write(|db| {
let mut connection = lock(db)?;

let answer = store::move_into(&mut connection, ids, folder_id, lines.card_height())?;
let changed = Changed::notes(ids.to_vec());
Ok((answer, changed))
})
})?;
log::info!("{} moved into folder {folder_id}: {}", ids.len(), listed(ids));
Ok(placed)
}

/// The undo of [`file_notes`]: each note goes back to the folder it left, or to loose.
Expand All @@ -219,13 +248,15 @@ pub fn file_notes_back(
filings: &[NoteFiling],
lines: PreviewLines,
) -> Result<u32, AppError> {
engine.write(|db| {
let count = engine.write(|db| {
let mut connection = lock(db)?;

let answer = count(store::restore_filings(&mut connection, filings, lines.card_height())?);
let changed = Changed::notes(filings.iter().map(|one| one.note_id.clone()));
Ok((answer, changed))
})
})?;
log::info!("{count} filed back: {}", listed(filings.iter().map(|one| &one.note_id)));
Ok(count)
}

/// Puts one board back in order — the space's, or a folder's, `rootId` — as far as `scope`
Expand All @@ -241,11 +272,14 @@ pub fn arrange_board(
let db = &engine.db;
let mut connection = lock(db)?;

Ok(store::board::arrange(
let replaced = store::board::arrange(
&mut connection,
space_id,
root_id.map(std::string::String::as_str),
scope,
lines.card_height(),
)?)
)?;
let board = root_id.map_or(space_id, String::as_str);
log::info!("board of {board} arranged");
Ok(replaced)
}
1 change: 1 addition & 0 deletions crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub(crate) mod closed_enum;
pub(crate) mod count;
pub(crate) mod durable;
pub mod layout;
pub(crate) mod logged;
pub(crate) mod name;
pub mod sensitive;

Expand Down
42 changes: 42 additions & 0 deletions crates/engine/src/logged.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! What a write says in the log (D35): the ids it touched, and what a person wrote through
//! [`Sensitive`].

use crate::sensitive::Sensitive;

/// How many ids a line names before it counts the rest.
const NAMED: usize = 10;

/// The ids, whole, the first ten of them.
pub(crate) fn listed<T: AsRef<str>>(ids: impl IntoIterator<Item = T>) -> String {
let all: Vec<T> = ids.into_iter().collect();
let named = all.iter().take(NAMED).map(AsRef::as_ref).collect::<Vec<_>>().join(", ");
match all.len().saturating_sub(NAMED) {
0 => named,
more => format!("{named} and {more} more"),
}
}

/// Several things a person wrote — tags, field values, items —, as one [`Sensitive`].
pub(crate) fn written<T: AsRef<str>>(texts: impl IntoIterator<Item = T>) -> String {
let texts: Vec<T> = texts.into_iter().collect();
let texts: Vec<&str> = texts.iter().map(AsRef::as_ref).collect();
Sensitive(&texts.join(", ")).to_string()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn ten_ids_are_named_and_the_rest_counted() {
let many: Vec<String> = (1..=12).map(|n| format!("id-{n}")).collect();

assert_eq!(listed(["a", "b"]), "a, b");
assert_eq!(listed(&many), format!("{} and 2 more", many[..10].join(", ")));
}

#[test]
fn what_was_written_is_its_length_unrevealed() {
assert_eq!(written(["urgent", "prod"]), "‹12 chars›");
}
}
Loading
Loading