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
31 changes: 24 additions & 7 deletions crates/engine/src/folders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use crate::count::saturating_u32 as count;
use crate::db::lock;
use crate::error::AppError;
use crate::index::Changed;
use crate::notes::model::NotePlacement;
use crate::notes::model::{HeldNotes, NotePlacement};

use board::{
BoardArrangement, BoardQuery, BoardScope, BoardView, CardPlacement, Occupancy, PreviewLines,
ZonePlacement,
};
use model::{Folder, FolderColour, FolderDraft, FolderMove, Lineage, NoteFiling};
use model::{Folder, FolderColour, FolderContents, FolderDraft, FolderMove, Lineage, NoteFiling};

/// The second way to look at a space, one level at a time: the root's sub-folders as zones,
/// each gathering what is under it, and what is filed in the root itself free beside them. It
Expand Down Expand Up @@ -179,15 +179,32 @@ pub fn move_folder_back(engine: &Engine, was: &FolderMove) -> Result<bool, AppEr
})
}

/// No refuge, unlike [`crate::spaces::delete_space`]: what it held goes to its parent, or
/// loose at the top of the space. `lines` sizes the rows the parent's zone grows by.
pub fn delete_folder(engine: &Engine, id: &str, lines: PreviewLines) -> Result<(), AppError> {
/// What deleting the folder would keep or trash, counted under the lock: its live notes and its
/// sub-folders, all the way down.
pub fn count_folder_contents(engine: &Engine, id: &str) -> Result<FolderContents, AppError> {
let db = &engine.db;
let mut connection = lock(db)?;

Ok(store::contents(&mut connection, id)?)
}

/// No refuge, unlike [`crate::spaces::delete_space`]: what it held goes to its parent, or loose
/// at the top of the space. With `HeldNotes::Trash` its sub-folders go with it and every note
/// under it goes to the trash, restored loose in the space. `lines` sizes the rows the parent's
/// zone grows by. Answers how many live notes it held.
pub fn delete_folder(
engine: &Engine,
id: &str,
lines: PreviewLines,
held: HeldNotes,
) -> Result<u32, AppError> {
engine.write(|db| {
let mut connection = lock(db)?;

store::delete(&mut connection, id, lines.card_height())?;
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(((), changed))
Ok((count(held), changed))
})
}

Expand Down
8 changes: 8 additions & 0 deletions crates/engine/src/folders/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ pub struct FolderMove {
pub notes: Vec<String>,
}

/// What a folder's deletion would keep or trash: the live notes under it, sub-folders included,
/// and how many sub-folders it has.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FolderContents {
pub notes: u32,
pub folders: u32,
}

/// Which folder each note left, the only thing that can put a filing back; `None` was loose.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NoteFiling {
Expand Down
91 changes: 74 additions & 17 deletions crates/engine/src/folders/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use diesel::prelude::*;
use uuid::Uuid;

use super::model::{Folder, FolderColour, FolderMove, Lineage, NoteFiling, NoteFolder};
use crate::count::saturating_u32;
use crate::db::schema::{folders, notes};
use crate::db::{Library, iso8601};
use crate::error::StorageError;
Expand Down Expand Up @@ -413,35 +414,91 @@ fn place(
/// What it held goes to its parent — its notes, filed there, and its sub-folders, each to a
/// free slot on that board — or loose at the top of the space. `updated_at` is not touched:
/// the user aimed at the folder.
pub fn delete(connection: &mut Library, id: &str, card: i32) -> Result<(), StorageError> {
pub fn contents(
connection: &mut Library,
id: &str,
) -> Result<super::model::FolderContents, StorageError> {
let (connection, _vault) = connection.split();
let under = lineage_of(connection, id)?.subtree(id);
Ok(super::model::FolderContents {
notes: saturating_u32(count_filed(connection, &under)?),
folders: saturating_u32(under.len().saturating_sub(1)),
})
}

/// Answers how many live notes it held; `trashed_at`, they and its sub-folders go with it.
pub fn delete(
connection: &mut Library,
id: &str,
card: i32,
trashed_at: Option<DateTime<Utc>>,
) -> Result<usize, StorageError> {
connection.transaction(|connection, _vault| {
let lineage = lineage_of(connection, id)?;
let parent = lineage.parent(id).map(str::to_string);

for child in lineage.children(Some(id)) {
place(connection, child, parent.as_deref(), None)?;
let under = lineage.subtree(id);
let held = count_filed(connection, &under)?;
match trashed_at {
Some(at) => trash_under(connection, &under, at)?,
None => hand_up(connection, &lineage, id, parent.as_deref())?,
}

let held: Vec<String> = notes::table
.filter(notes::folder_id.eq(id))
.select(notes::id)
.load::<String>(connection)?;
// A place on the deleted folder's board means nothing on its parent's.
board::forget_positions(connection, &held)?;
diesel::update(notes::table.filter(notes::folder_id.eq(id)))
.set(notes::folder_id.eq(parent.as_deref()))
.execute(connection)?;

diesel::delete(folders::table.find(id)).execute(connection)?;

if let Some(parent) = &parent {
let after = lineage_of(connection, parent)?;
grow_with_ancestors(connection, &after, parent, card)?;
}
Ok(())
Ok(held)
})
}

/// Its sub-folders and its notes to its parent, or loose at the top; then the folder goes.
fn hand_up(
connection: &mut SqliteConnection,
lineage: &Lineage,
id: &str,
parent: Option<&str>,
) -> Result<(), StorageError> {
for child in lineage.children(Some(id)) {
place(connection, child, parent, None)?;
}

let held: Vec<String> = notes::table
.filter(notes::folder_id.eq(id))
.select(notes::id)
.load::<String>(connection)?;
// A place on the deleted folder's board means nothing on its parent's.
board::forget_positions(connection, &held)?;
diesel::update(notes::table.filter(notes::folder_id.eq(id)))
.set(notes::folder_id.eq(parent))
.execute(connection)?;

diesel::delete(folders::table.find(id)).execute(connection)?;
Ok(())
}

/// Every note under the folders `under` to the trash, then the folders: the notes' `folder_id`
/// is emptied by its key, so a restore brings them back loose in the space.
fn trash_under(
connection: &mut SqliteConnection,
under: &[String],
at: DateTime<Utc>,
) -> Result<(), StorageError> {
let held: Vec<String> = notes::table
.filter(notes::folder_id.eq_any(under))
.select(notes::id)
.load::<String>(connection)?;
board::forget_positions(connection, &held)?;
diesel::update(
notes::table
.filter(notes::folder_id.eq_any(under))
.filter(notes::deleted_at.is_null()),
)
.set(notes::deleted_at.eq(iso8601::format(at)))
.execute(connection)?;
diesel::delete(folders::table.filter(folders::id.eq_any(under))).execute(connection)?;
Ok(())
}

/// Every zone that shows a folder's notes grows to fit them: its own, and its ancestors',
/// which gather what is under them.
fn grow_with_ancestors(
Expand Down
9 changes: 9 additions & 0 deletions crates/engine/src/notes/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,3 +1205,12 @@ mod tests {
assert_eq!(note.placeholders[0].name, "host");
}
}

/// What deleting a space or a folder does to the notes it holds: they live on elsewhere, or go to
/// the trash with it. Never erased: only emptying the trash, or its retention, is final.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum HeldNotes {
#[default]
Keep,
Trash,
}
18 changes: 15 additions & 3 deletions crates/engine/src/spaces.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
pub mod model;
pub mod store;

use chrono::Utc;

use crate::Engine;
use crate::count::saturating_u32 as count;
use crate::db::lock;
use crate::error::AppError;
use crate::index::Changed;
use crate::notes::model::HeldNotes;
use model::{Space, SpaceContents, SpaceDraft};

pub fn list_spaces(engine: &Engine) -> Result<Vec<Space>, AppError> {
Expand Down Expand Up @@ -47,16 +51,24 @@ pub fn count_space_contents(engine: &Engine, id: &str) -> Result<SpaceContents,
Ok(store::contents(&mut connection, id)?)
}

pub fn delete_space(engine: &Engine, id: &str, target_space_id: &str) -> Result<(), AppError> {
/// Its notes go to `target_space_id`, out of any folder; with `HeldNotes::Trash` they go to the
/// trash as well, and a restore brings them back there. Answers how many live notes it moved.
pub fn delete_space(
engine: &Engine,
id: &str,
target_space_id: &str,
held: HeldNotes,
) -> Result<u32, AppError> {
engine.write(|db| {
// A space as its own refuge would see its notes swept away by the cascade right
// after the transfer.
model::validate_move_target(id, target_space_id)?;

let mut connection = lock(db)?;

store::delete(&mut connection, id, target_space_id)?;
let trashed_at = (held == HeldNotes::Trash).then(Utc::now);
let moved = store::delete(&mut connection, id, target_space_id, trashed_at)?;
let changed = Changed::everything();
Ok(((), changed))
Ok((count(moved), changed))
})
}
23 changes: 20 additions & 3 deletions crates/engine/src/spaces/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ use diesel::prelude::*;

use super::model::{Space, SpaceContents};
use crate::count::saturating_u32 as count;
use crate::db::Library;
use crate::db::schema::{folders, notes, spaces};
use crate::db::{Library, iso8601};
use crate::error::StorageError;
use crate::name;
use crate::vault::key::Vault;
use chrono::{DateTime, Utc};
use uuid::Uuid;

/// Pinned first, then by name — the same shape the canvas gives notes. Folded for the
Expand Down Expand Up @@ -191,19 +192,35 @@ pub fn contents(connection: &mut Library, id: &str) -> Result<SpaceContents, Sto
})
}

pub fn delete(connection: &mut Library, id: &str, target_id: &str) -> Result<(), StorageError> {
/// Answers how many live notes it moved; `trashed_at`, they are trashed on the way.
pub fn delete(
connection: &mut Library,
id: &str,
target_id: &str,
trashed_at: Option<DateTime<Utc>>,
) -> Result<usize, StorageError> {
connection.transaction(|connection, _vault| {
if !exists(connection, target_id)? {
return Err(StorageError::SpaceNotFound(target_id.to_string()));
}

let live = notes::table
.filter(notes::space_id.eq(id))
.filter(notes::deleted_at.is_null());
// Before the move: after it, the refuge's own notes would be among them.
let moved = match trashed_at {
Some(at) => diesel::update(live)
.set(notes::deleted_at.eq(iso8601::format(at)))
.execute(connection)?,
None => usize::try_from(live.count().get_result::<i64>(connection)?).unwrap_or(0),
};
diesel::update(notes::table.filter(notes::space_id.eq(id)))
.set(notes::space_id.eq(target_id))
.execute(connection)?;
if diesel::delete(spaces::table.find(id)).execute(connection)? == 0 {
return Err(StorageError::SpaceNotFound(id.to_string()));
}

Ok(())
Ok(moved)
})
}
Loading
Loading