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
4 changes: 4 additions & 0 deletions document/format/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ pub enum Error {
#[cfg(feature = "network")]
#[error("network error: {0}")]
Network(#[from] peer_transport::PacketError),
/// The room knows this device by its peer, so the user can't change.
#[cfg(feature = "network")]
#[error("the user cannot change while the document is connected")]
UserChangeWhileConnected,
}
10 changes: 5 additions & 5 deletions document/format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ impl<L: Layout + Default> Gdd<L> {

/// Create a fresh, empty working copy at `path` bound to `peer`. Writes a default manifest
/// and session state; the caller fills in editor metadata via [`Gdd::update_manifest`].
pub fn create(path: &Path, peer: PeerId, document_uuid: u64, editor_version: String, stdlib_version: String) -> Result<Self, Error> {
pub fn create(path: &Path, peer: PeerId, user: UserId, document_uuid: u64, editor_version: String, stdlib_version: String) -> Result<Self, Error> {
let working = AnyContainer::Folder(FolderBackend::create(path)?);
let layout = L::default();
Self::create_in(working, layout, peer, document_uuid, editor_version, stdlib_version)
Self::create_in(working, layout, peer, user, document_uuid, editor_version, stdlib_version)
}
}

Expand Down Expand Up @@ -344,18 +344,18 @@ impl<L: Layout> Gdd<L> {

/// Backend-agnostic create. Records the working-copy default codecs (see `DEFAULT_*_CODEC`) in
/// the manifest and writes each payload with its recorded codec.
pub fn create_in(working: AnyContainer, layout: L, peer: PeerId, document_uuid: u64, editor_version: String, stdlib_version: String) -> Result<Self, Error> {
pub fn create_in(working: AnyContainer, layout: L, peer: PeerId, user: UserId, document_uuid: u64, editor_version: String, stdlib_version: String) -> Result<Self, Error> {
let manifest = Manifest::new(document_uuid, editor_version, stdlib_version);
let codecs = manifest.codecs;
io::write_single(&working, layout.manifest_basename(), MANIFEST_CODEC, &manifest)?;
let session_state = SessionState {
peer_id: peer,
user_id: UserId(peer.0),
user_id: user,
..Default::default()
};
io::write_single(&working, layout.session_basename(), codecs.session, &Sorted(&session_state))?;

let session = Session::with_peer(peer);
let session = Session::with_identity(peer, user);
io::write_single(&working, layout.registry_basename(), codecs.registry, &Sorted(session.registry()))?;

Ok(Self::assemble(session, working, layout, manifest, HistoryMetadata::default()))
Expand Down
16 changes: 15 additions & 1 deletion document/format/src/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use document_container::{AnyContainer, AsyncContainer};
#[cfg(feature = "conversion")]
use document_graph_storage::NodeMetadataSource;
use document_graph_storage::{HistoryMetadata, HotOp, MetadataFact, RegistryDelta, Rev, Sorted, Subject, TimeStamp, Value, WallStamp};
use document_graph_storage::{HistoryMetadata, HotOp, MetadataFact, PeerId, RegistryDelta, Rev, Sorted, Subject, TimeStamp, UserId, Value, WallStamp};
#[cfg(feature = "conversion")]
use graphene_resource::ResourceStorage;

Expand Down Expand Up @@ -65,6 +65,20 @@ impl<L: Layout> Gdd<L> {
Ok(rev)
}

/// Set the person using this device. `peer` must be unused, so the previous user keeps their steps. Refused while connected.
pub fn set_user(&mut self, user: UserId, peer: PeerId) -> Result<(), Error> {
if self.session.user() == user {
return Ok(());
}
#[cfg(feature = "network")]
if self.network.is_some() {
return Err(Error::UserChangeWhileConnected);
}
let closing = self.session.set_user(user, peer)?;
self.persist_staged(&closing)?;
self.persist_session_state()
}

/// Edit the cached manifest and persist it. Always JSON, synchronous.
pub fn update_manifest(&mut self, edit: impl FnOnce(&mut Manifest)) -> Result<(), Error> {
edit(&mut self.manifest);
Expand Down
4 changes: 2 additions & 2 deletions document/format/tests/deterministic_save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use document_container::AnyContainer;
use document_container::backends::memory::MemoryBackend;
use document_format::{ExportFormat, ExportOptions, GddV1, GddV1Layout};
use document_graph_storage::{Implementation, Network, NetworkId, Node, NodeId, PeerId, Priority, RegistryDelta, ResourceId, SourceKey, TimeStamp, to_value};
use document_graph_storage::{Implementation, Network, NetworkId, Node, NodeId, PeerId, Priority, RegistryDelta, ResourceId, SourceKey, TimeStamp, UserId, to_value};
use graph_craft::application_io::resource::{HashMapResourceStorage, ResourceStorage};
use graphene_resource::DataSource;
use std::collections::BTreeMap;
Expand All @@ -16,7 +16,7 @@ const LATEST: TimeStamp = TimeStamp {
};

fn document(peer: u64) -> GddV1 {
GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(peer), 1, "editor".into(), "stdlib".into()).expect("create")
GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(peer), UserId(peer), 1, "editor".into(), "stdlib".into()).expect("create")
}

/// Networks and nodes, some of each removed again, plus link-only resources whose bytes sit in the byte store.
Expand Down
2 changes: 1 addition & 1 deletion document/format/tests/history_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const NOW_MS: f64 = 1_700_000_000_000.;
const ADA: Subject = Subject::User(UserId(10));

fn document(peer: u64) -> GddV1 {
GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(peer), 1, "editor".into(), "stdlib".into()).expect("create")
GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(peer), UserId(peer), 1, "editor".into(), "stdlib".into()).expect("create")
}

/// Let the room settle through greetings, the sync handshake and whatever was broadcast.
Expand Down
6 changes: 3 additions & 3 deletions document/format/tests/migration_costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use document_container::AnyContainer;
use document_container::backends::memory::MemoryBackend;
use document_format::{GddV1, GddV1Layout};
use document_graph_storage::{EncodedDeclaration, Implementation, NoMetadata, PeerId, Registry};
use document_graph_storage::{EncodedDeclaration, Implementation, NoMetadata, PeerId, Registry, UserId};
use futures::executor::block_on;
use graph_craft::application_io::resource::HashMapResourceStorage;
use graph_craft::document::{DocumentNode, DocumentNodeImplementation, NodeId, NodeNetwork};
Expand Down Expand Up @@ -91,7 +91,7 @@ fn stored(nodes: &[(u64, &'static str)]) -> AnyContainer {

/// A document whose declaration bytes went to `store`, as the editor sends them to its global cache.
fn stored_with_bytes_in(nodes: &[(u64, &'static str)], store: &HashMapResourceStorage) -> AnyContainer {
let mut gdd = GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(7), 1, "editor".into(), "std".into()).unwrap();
let mut gdd = GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(7), UserId(7), 1, "editor".into(), "std".into()).unwrap();
gdd.commit_from_runtime(&network_of(nodes), &NoMetadata, &ResourceRegistry::new(), store).unwrap();
gdd.into_storage().0
}
Expand Down Expand Up @@ -143,7 +143,7 @@ fn declarations_come_from_the_container_when_the_cache_lacks_them() {
let nodes = [(1, "test_nodes::CopyNode"), (2, "test_nodes::OtherNode")];

// An archive carries its declarations in the container, which a device's cache hasn't seen yet
let mut gdd = GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(7), 1, "editor".into(), "std".into()).unwrap();
let mut gdd = GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(7), UserId(7), 1, "editor".into(), "std".into()).unwrap();
gdd.commit_from_runtime(&network_of(&nodes), &NoMetadata, &ResourceRegistry::new(), &HashMapResourceStorage::new())
.unwrap();
for (_, identifier) in nodes {
Expand Down
4 changes: 2 additions & 2 deletions document/format/tests/migration_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn network_of(nodes: &[(u64, &'static str)]) -> NodeNetwork {
}

fn create() -> GddV1 {
GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(7), 1, "editor".into(), "std".into()).unwrap()
GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(7), UserId(7), 1, "editor".into(), "std".into()).unwrap()
}

fn commit(gdd: &mut GddV1, nodes: &[(u64, &'static str)], store: &HashMapResourceStorage) {
Expand Down Expand Up @@ -79,7 +79,7 @@ fn a_migration_on_disk_leaves_no_staging_behind() {
block_on(async {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("document");
let mut gdd = GddV1::create(&path, PeerId(7), 1, "editor".into(), "std".into()).unwrap();
let mut gdd = GddV1::create(&path, PeerId(7), UserId(7), 1, "editor".into(), "std".into()).unwrap();
commit(&mut gdd, &[(1, FADE)], &HashMapResourceStorage::new());
drop(gdd);

Expand Down
4 changes: 2 additions & 2 deletions document/format/tests/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use document_container::AnyContainer;
use document_container::backends::memory::MemoryBackend;
use document_format::{Error, GddV1, GddV1Layout, manifest};
use document_graph_storage::{EncodedDeclaration, Implementation, NoMetadata, PeerId};
use document_graph_storage::{EncodedDeclaration, Implementation, NoMetadata, PeerId, UserId};
use futures::executor::block_on;
use graph_craft::application_io::resource::HashMapResourceStorage;
use graph_craft::document::{DocumentNode, DocumentNodeImplementation, NodeId, NodeNetwork};
Expand All @@ -28,7 +28,7 @@ fn network_of(nodes: &[(u64, &'static str)]) -> NodeNetwork {
}

fn create() -> GddV1 {
GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(7), 1, "editor".into(), "std".into()).unwrap()
GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, PeerId(7), UserId(7), 1, "editor".into(), "std".into()).unwrap()
}

fn implementations(gdd: &GddV1) -> Vec<Implementation> {
Expand Down
Loading
Loading