mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-30 02:04:54 +01:00
Merge pull request #2719 from mikaelweiss/fix/server-kept-no-save
M9: a file joined on a server that keeps it asks for no save
This commit is contained in:
7 files changed
+249
-4
No files matched your search
@@ -606,6 +606,8 @@ mod selection_behaviour_tests;
|
||||
#[cfg(test)]
|
||||
mod selection_depth_tests;
|
||||
#[cfg(test)]
|
||||
mod server_kept_tests;
|
||||
#[cfg(test)]
|
||||
mod shape_handles_tests;
|
||||
#[cfg(test)]
|
||||
mod shapebuilder_strokes_tests;
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
//! A file joined on a server that keeps it has nothing to save on this computer once the server
|
||||
//! confirmed all of it: no "Unsaved changes" question when its tab closes or the app quits.
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use figcraft_collab::message::{ClientMsg, HostInfo, ServerMsg};
|
||||
use figcraft_collab::{Client, Limits, Outgoing, Server};
|
||||
use figcraft_doc::Document;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
use crate::live::{Event, Role, Status};
|
||||
use crate::live_tests::{Script, info, peer, push, relay, session};
|
||||
|
||||
/// You joined "Team file" on a server as a guest: the session, its server and your client id.
|
||||
fn joined() -> (Session, Arc<Mutex<Script>>, Server, u32) {
|
||||
let (mut s, script) = session();
|
||||
let mut doc = Document::new("Team file");
|
||||
doc.repair();
|
||||
let mut server = Server::new(doc, "", Limits::default());
|
||||
let ClientMsg::Hello(h) = Client::hello("f", "", "Me", "") else { panic!("no hello") };
|
||||
let (me, outs) = server.join(h).unwrap();
|
||||
let Some(Outgoing::To(_, w)) = outs.into_iter().find(|o| matches!(o, Outgoing::To(_, ServerMsg::Welcome { .. }))) else { panic!("no welcome") };
|
||||
s.execute("collab.join", &json!({"link": "figcraft://join?host=a:1&file=f&token=t"})).unwrap();
|
||||
push(&script, vec![Event::Welcome { info: info(Role::Guest, me), peers: vec![peer(1, "Hana")] }, Event::Sync(w)]);
|
||||
s.live_poll();
|
||||
assert_eq!(s.doc().unwrap().name, "Team file");
|
||||
(s, script, server, me)
|
||||
}
|
||||
|
||||
/// Draw a rectangle, confirm it stays dirty across a poll while pending, then let the server confirm it.
|
||||
fn edit_and_confirm(s: &mut Session, script: &Arc<Mutex<Script>>, server: &mut Server, me: u32) {
|
||||
s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
|
||||
s.live_sync();
|
||||
// An unrelated event while the batch is still pending must not clear the unsaved mark.
|
||||
push(script, vec![Event::PeerJoined(peer(1, "Hana"))]);
|
||||
s.live_poll();
|
||||
assert!(s.ds().unwrap().is_dirty(), "an edit the server hasn't confirmed is unsaved");
|
||||
push(script, relay(script, server, me));
|
||||
s.live_poll();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_the_server_keeps_has_nothing_to_save_once_confirmed() {
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
||||
s.live_poll();
|
||||
assert!(!s.ds().unwrap().is_dirty(), "joining a kept file asks for no save");
|
||||
edit_and_confirm(&mut s, &script, &mut server, me);
|
||||
assert!(!s.ds().unwrap().is_dirty(), "the server has every edit");
|
||||
// Someone else's edit is on the server too.
|
||||
let ClientMsg::Hello(h) = Client::hello("f", "", "Ada", "") else { panic!("no hello") };
|
||||
let (ada, outs) = server.join(h).unwrap();
|
||||
let Some(Outgoing::To(_, w)) = outs.into_iter().find(|o| matches!(o, Outgoing::To(_, ServerMsg::Welcome { .. }))) else { panic!("no welcome") };
|
||||
let mut other = Client::join(w).unwrap();
|
||||
let mut doc = other.document().clone();
|
||||
doc.name = "Renamed".into();
|
||||
let batch = other.edit(&doc).unwrap().unwrap();
|
||||
let remote: Vec<Event> =
|
||||
server.receive(ada, batch).into_iter().filter_map(|o| if let Outgoing::All(m) = o { Some(Event::Sync(m)) } else { None }).collect();
|
||||
push(&script, remote);
|
||||
s.live_poll();
|
||||
assert_eq!(s.doc().unwrap().name, "Renamed");
|
||||
assert!(!s.ds().unwrap().is_dirty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_session_an_app_hosts_still_asks() {
|
||||
// No hosting info: an app hosts the session, and nothing keeps the file once it leaves.
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
edit_and_confirm(&mut s, &script, &mut server, me);
|
||||
assert!(s.ds().unwrap().is_dirty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_with_an_expiry_time_still_asks() {
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
push(&script, vec![Event::Hosting(HostInfo { open: true, expires: Some(4_102_444_800), ..HostInfo::default() })]);
|
||||
s.live_poll();
|
||||
edit_and_confirm(&mut s, &script, &mut server, me);
|
||||
assert!(s.ds().unwrap().is_dirty(), "a server that expires the file keeps no backups");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tab_with_a_file_of_its_own_still_asks() {
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
||||
s.live_poll();
|
||||
s.ds_mut().unwrap().path = Some("team.figcraft".into());
|
||||
edit_and_confirm(&mut s, &script, &mut server, me);
|
||||
assert!(s.ds().unwrap().is_dirty(), "the file on this computer is behind");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tab_with_changed_versions_still_asks() {
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
||||
s.live_poll();
|
||||
s.execute("file.saveVersion", &json!({"name": "Local snapshot"})).unwrap();
|
||||
edit_and_confirm(&mut s, &script, &mut server, me);
|
||||
assert!(s.ds().unwrap().is_dirty(), "versions live only in the local file");
|
||||
assert!(s.ds().unwrap().versions_changed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_branch_tab_still_asks() {
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
||||
s.live_poll();
|
||||
s.ds_mut().unwrap().branch_of = Some((999, "b1".into()));
|
||||
edit_and_confirm(&mut s, &script, &mut server, me);
|
||||
assert!(s.ds().unwrap().is_dirty(), "branch tabs are excluded from live_kept");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unsynced_local_edit_stays_unsaved_when_polling() {
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
// Confirm an edit before hosting arrives so saved_doc differs from synced.
|
||||
edit_and_confirm(&mut s, &script, &mut server, me);
|
||||
let saved_before = s.ds().unwrap().saved_doc.clone();
|
||||
// Mutate the document directly without running live_edit(): pending() stays 0, but document_itself() != synced.
|
||||
s.ds_mut().unwrap().doc_mut().name = "Local draft".into();
|
||||
assert_eq!(s.live().client().unwrap().pending(), 0);
|
||||
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
||||
s.live_poll();
|
||||
assert!(s.ds().unwrap().is_dirty(), "local edit not yet sent to the sync client must stay unsaved");
|
||||
assert!(Arc::ptr_eq(&s.ds().unwrap().saved_doc, &saved_before), "unsynced local edit must not advance saved_doc");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirming_during_a_version_preview_marks_the_document_saved() {
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
s.execute("file.saveVersion", &json!({"name": "Earlier"})).unwrap();
|
||||
// Simulate a version already saved before this edit so only the preview baseline is tested.
|
||||
s.ds_mut().unwrap().versions_changed = false;
|
||||
// Confirm the edit before hosting info arrives so the tab is still dirty entering the preview.
|
||||
edit_and_confirm(&mut s, &script, &mut server, me);
|
||||
assert!(s.ds().unwrap().is_dirty());
|
||||
s.execute("version.preview", &json!({"index": 0})).unwrap();
|
||||
assert!(s.ds().unwrap().preview.is_some());
|
||||
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
||||
s.live_poll();
|
||||
assert!(!s.ds().unwrap().is_dirty(), "confirmed underlying document is saved during a version preview");
|
||||
s.execute("version.exitPreview", &json!({})).unwrap();
|
||||
assert!(!s.ds().unwrap().is_dirty(), "document stays saved after ending a version preview");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirming_during_a_text_preview_marks_the_document_saved() {
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
let text = s.execute("text.create", &json!({"text": "Preview"})).unwrap()["id"].clone();
|
||||
s.live_sync();
|
||||
push(&script, relay(&script, &mut server, me));
|
||||
s.live_poll();
|
||||
assert!(s.ds().unwrap().is_dirty());
|
||||
s.execute("text.preview", &json!({"command": "text.style", "params": {"ids": [text], "fontSize": 32}})).unwrap();
|
||||
assert!(s.ds().unwrap().text_preview.is_some());
|
||||
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
||||
s.live_poll();
|
||||
assert!(!s.ds().unwrap().is_dirty(), "confirmed underlying document is saved during a text preview");
|
||||
s.execute("text.endPreview", &json!({})).unwrap();
|
||||
assert!(!s.ds().unwrap().is_dirty(), "document stays saved after ending a text preview");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_deferred_confirmation_stays_unsaved_until_applied_after_preview() {
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
||||
s.live_poll();
|
||||
let text = s.execute("text.create", &json!({"text": "Deferred"})).unwrap()["id"].clone();
|
||||
s.live_sync();
|
||||
// Start a text preview before the server echoes the batch so the confirmation is deferred.
|
||||
s.execute("text.preview", &json!({"command": "text.style", "params": {"ids": [text], "fontSize": 28}})).unwrap();
|
||||
push(&script, relay(&script, &mut server, me));
|
||||
s.live_poll();
|
||||
assert!(s.ds().unwrap().is_dirty(), "deferred confirmation must not clear the unsaved mark yet");
|
||||
// Ending the preview lets live_sync() apply the deferred batch with no further network events.
|
||||
s.execute("text.endPreview", &json!({})).unwrap();
|
||||
s.live_sync();
|
||||
assert!(!s.ds().unwrap().is_dirty(), "applying the deferred confirmation clears the unsaved mark");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_held_session_stays_unsaved_until_released() {
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
let uid = s.ds().unwrap().uid;
|
||||
edit_and_confirm(&mut s, &script, &mut server, me);
|
||||
assert!(s.ds().unwrap().is_dirty());
|
||||
s.live_hold(uid, true);
|
||||
push(&script, vec![Event::Hosting(HostInfo::default())]);
|
||||
s.live_poll();
|
||||
assert!(s.ds().unwrap().is_dirty(), "a held session must stay unsaved");
|
||||
s.live_hold(uid, false);
|
||||
s.live_sync();
|
||||
assert!(!s.ds().unwrap().is_dirty(), "releasing the hold allows the confirmed tab to become saved");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_disconnected_session_stays_unsaved_even_with_hosting_info() {
|
||||
let (mut s, script, mut server, me) = joined();
|
||||
edit_and_confirm(&mut s, &script, &mut server, me);
|
||||
assert!(s.ds().unwrap().is_dirty());
|
||||
// Hosting info arrives while reconnecting (not Status::Live).
|
||||
push(&script, vec![Event::Status(Status::Reconnecting { attempt: 1 }), Event::Hosting(HostInfo::default())]);
|
||||
s.live_poll();
|
||||
assert!(s.ds().unwrap().is_dirty(), "a non-live session must stay unsaved");
|
||||
}
|
||||
@@ -237,9 +237,10 @@ impl Session {
|
||||
let Some((bdoc, _, _, uid)) = before else { return };
|
||||
let deletes = spec.id == "edit.delete" || (spec.id == "edit.repeat" && self.last_command.as_ref().is_some_and(|(id, _)| id == "edit.delete"));
|
||||
let Some(ds) = self.docs.iter_mut().find(|d| d.uid == *uid) else { return };
|
||||
// Saving may adopt the chosen filename as the document title. The encoded snapshot is
|
||||
// already final; normalising it again would detach it from the saved baseline.
|
||||
if !Arc::ptr_eq(&ds.doc, bdoc) && !matches!(spec.id, "file.save" | "file.saveAs") {
|
||||
// Saving may adopt the chosen filename as the document title, and exiting a version
|
||||
// preview only restores the already-normalised document; normalising either again would
|
||||
// detach it from the saved baseline.
|
||||
if !Arc::ptr_eq(&ds.doc, bdoc) && !matches!(spec.id, "file.save" | "file.saveAs" | "version.exitPreview") {
|
||||
normalize(ds.doc_mut(), bdoc);
|
||||
if deletes {
|
||||
cmd::edit::report_orphaned_instances(result, bdoc, &ds.doc);
|
||||
|
||||
@@ -154,6 +154,7 @@ impl Session {
|
||||
for id in self.live.sessions.keys().copied().collect::<Vec<_>>() {
|
||||
self.live_deferred(id);
|
||||
self.live_follow_apply(id);
|
||||
self.live_kept(id);
|
||||
}
|
||||
self.online_recoveries();
|
||||
true
|
||||
|
||||
@@ -251,6 +251,28 @@ impl Session {
|
||||
live.followed = Some(FollowedView { page: ds.page, cx: ds.view.cx, cy: ds.view.cy, zoom: ds.view.zoom });
|
||||
}
|
||||
|
||||
/// Session `id`'s file is kept by a server that never expires it, and the server confirmed all
|
||||
/// of it: a tab with no file of its own on this computer then has nothing to save, so closing
|
||||
/// it or quitting doesn't ask. Not for a session an app hosts (nothing keeps it once the host
|
||||
/// leaves), a file with an expiry time (a public server keeps no backups), a branch tab,
|
||||
/// versions or a local file the tab has (they stay on this computer), or edits that are
|
||||
/// unconfirmed, deferred or held.
|
||||
pub(super) fn live_kept(&mut self, id: SessionId) {
|
||||
let Some(live) = self.live.get(id) else { return };
|
||||
let kept = live.status == Status::Live && live.hosting.as_ref().is_some_and(|h| h.expires.is_none());
|
||||
let confirmed = live.client.as_ref().is_some_and(|c| c.pending() == 0) && live.deferred.is_empty() && live.hold == 0;
|
||||
let (Some(uid), Some(synced)) = (live.doc_uid, live.synced.clone()) else { return };
|
||||
if !kept || !confirmed {
|
||||
return;
|
||||
}
|
||||
if let Some(ds) = self.docs.iter_mut().find(|d| d.uid == uid && d.path.is_none() && d.branch_of.is_none() && !d.versions_changed)
|
||||
&& Arc::ptr_eq(ds.document_itself(), &synced)
|
||||
&& ds.is_dirty()
|
||||
{
|
||||
ds.saved_doc = synced;
|
||||
}
|
||||
}
|
||||
|
||||
/// Sessions whose tab was closed end (hosts stop hosting, guests leave); the others go on. A
|
||||
/// join still on its way from a closed tab shows on the one in front.
|
||||
pub(super) fn live_closed(&mut self) {
|
||||
@@ -297,6 +319,7 @@ impl Session {
|
||||
self.live_branches_sync(uid);
|
||||
}
|
||||
self.live_present(id);
|
||||
self.live_kept(id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Collaboration parity
|
||||
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-28 · **Change:** minor (`wss://` connections also trust the authorities in the operating system's certificate store, so a private authority added there works without `FIGCRAFT_CA_FILE` (#2342); legacy welcomes without a reconnect secret are accepted with their explicit access level, and reconnects re-authenticate with the share link (#2365); a shared file's opening page can be set (`page.setOpening`, Set as opening page in the pages list) and syncs to everyone who joins afterwards (#1817); shared branches sync across live sessions and survive `figcraft-server` restarts, capability-gated on `branches`; numbers unchanged) · **Target:** Figma Design (desktop app 126.10.7); M9: a joined live session reconnects until you leave and says when it is disconnected, and joining it again merges what you did while disconnected; ; a file shared on the local network can be shared to look at only: a view-only code next to the edit code, picked in the Share popover before or while sharing)
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-28 · **Change:** minor (a file joined on a server that keeps it no longer shows an unsaved mark or asks to be saved once every edit is confirmed; `wss://` connections also trust the authorities in the operating system's certificate store, so a private authority added there works without `FIGCRAFT_CA_FILE` (#2342); legacy welcomes without a reconnect secret are accepted with their explicit access level, and reconnects re-authenticate with the share link (#2365); a shared file's opening page can be set (`page.setOpening`, Set as opening page in the pages list) and syncs to everyone who joins afterwards (#1817); shared branches sync across live sessions and survive `figcraft-server` restarts, capability-gated on `branches`; numbers unchanged) · **Target:** Figma Design (desktop app 126.10.7); M9: a joined live session reconnects until you leave and says when it is disconnected, and joining it again merges what you did while disconnected; ; a file shared on the local network can be shared to look at only: a view-only code next to the edit code, picked in the Share popover before or while sharing)
|
||||
|
||||
Figma is multiplayer first; FigCraft is local first. **Ready ~13% (estimated):** comments, version
|
||||
history and branches (review and merge) work inside one file; the real-time collaboration core landed in #103 (sync model,
|
||||
@@ -27,6 +27,7 @@ cursors, selections, text carets, cursor chat and spotlights shown live ([`colla
|
||||
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-11-28 | minor | M9: a file joined on a server that keeps it (hosting info reports no expiry) has nothing to save on this computer once the server has confirmed every edit and no batch is deferred or held: closing its tab or quitting no longer asks to save it, and the tab shows no unsaved mark. Recording `synced` as the saved baseline preserves the clean state across version and text previews, and `live_sync_in` also rechecks after applying deferred batches or releasing holds. Still asks while an edit is unconfirmed, deferred or held, for a file with an expiry time, on servers older than hosting info, for branch tabs, for a tab saved to a local file or with changed versions, and in an app-hosted session. Tests: `engine::server_kept_tests`. Numbers unchanged. |
|
||||
| 2026-11-28 | minor | #2342: the desktop app and `figcraft-cli` checked a `wss://` server's certificate against the compiled-in public roots and `FIGCRAFT_CA_FILE` only, so a server behind a proxy with a private authority needed that variable on every computer, which a signed desktop app started from the dock or the start menu doesn't see. They now also trust the authorities in the operating system's certificate store, read once per process, so adding the private authority to the computer's trusted certificates is enough. `FIGCRAFT_NO_SYSTEM_ROOTS=1` leaves the store out. Certificates are still always verified; an unknown authority's error names both ways to trust it. Not tested against a real private authority in the store here. |
|
||||
| 2026-11-28 | patch | M9, #2495: Stop sharing failed on a private server for a file shared with the server key typed under Advanced ("that owner token doesn't manage a file of that id here"). Such a file has no owner token, and the key went only with the publish. `collab.share {key}` now keeps the key with the share in this session, in the address as `collab.share {server: "…?key=…"}` would give it, so Stop sharing and sharing again ask as the server's operator. The key is never in a command result. Test: `live_share_tests::stopping_a_file_shared_with_a_separate_key_asks_with_that_key`. Numbers unchanged |
|
||||
| 2026-11-26 | minor | M10, gap 17: shared branches sync across live sessions. Capability-gated on `branches` in `Hello::client_version` (`ServerMsg::Branches` / `Branch`, `ClientMsg::Branch` with `Create`, `Edit`, `UpdateFromMain`, `SetState`), persisted in `SessionState::branches` and the server journal (with fallback decoding for pre-branch `figcraft-server` snapshots). Any editor can open, co-edit, update from main, review, archive and merge another peer's branch (remote merges/archives close open branch tabs on peers and switch back to main); view and comment links are refused; node/entry id spaces, geometry and image entries are validated on every branch mutation; older clients and servers stay compatible (local-only branches). Tests: `collab::branch_tests`, `engine::branch_live_tests`, `figcraft-server::server_tests::shared_branches_sync_and_survive_a_server_restart`, `figcraft-server::disk::tests::decode_snapshot_reads_pre_branch_v1_snapshots`. Numbers unchanged. |
|
||||
|
||||
@@ -222,6 +222,16 @@ comment link's batch only when every change in it touches comment threads. An ap
|
||||
comment links joins with one as a viewer. **Leave session** keeps the file open as your own copy,
|
||||
editable again.
|
||||
|
||||
The server keeps the file, so a tab joined there has nothing to save on your computer once the
|
||||
server has confirmed every edit in it and no batch is deferred or held (during a preview, gesture
|
||||
or command group): closing the tab or quitting doesn't ask to save it, and the tab shows no unsaved
|
||||
mark. It still asks while an edit of yours hasn't reached the server (offline, say) or a batch is
|
||||
deferred or held, for a file with an expiry time (a [public server](#running-a-public-server) keeps
|
||||
no backups: keep a copy of your own), for a server older than hosting info (it never reports
|
||||
whether the file expires), for a tab saved to a file on this computer (that file would be behind),
|
||||
for branch tabs, for versions you made in the tab (they live only on this computer), and in a
|
||||
session a desktop app hosts on the local network (nothing keeps that file once the host leaves).
|
||||
|
||||
## Team libraries
|
||||
|
||||
A file on the server can hold a **team library**: its published components, styles and variables,
|
||||
|
||||
Reference in new issue
Block a user