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
64 changes: 39 additions & 25 deletions crates/engine/src/cmd/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,14 +1002,21 @@ pub(crate) fn bring_back(s: &mut Session, p: &Value) -> Result<Value, String> {
));
}
let mut reference = Vec::new();
// A layer the area has no copy of (one added in the field) isn't the area's: matched by id
// alone, it could reach a home layer of the same id. Left out, and said.
let mut unowned = Vec::new();
edited.layers.retain(|l| {
if !l.is_raster() && !baseline.layers.iter().any(|b| b.id == l.id) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rasters are still dropped without a notice (Never silent). The new unowned path skips rasters, so a raster layer the field added that the area has no copy of is removed from edited.layers silently. The keep branch below keeps it, but sync_area never reads rasters, so the layer goes nowhere and nothing says so.

Packaging strips rasters (field.rs ~line 147), so this needs a raster to come back in a return, which is probably rare. Still, the same "not in the area, left in the field project" notice would cover it, and a test could check it.

Confidence: low to medium. I did not confirm that the field app can add a raster layer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a change from this PR. Rasters are kept in edited.layers here exactly as before, and sync_area has always passed them over (!l.is_raster()), because a field package carries no raster layer data to bring back (packaging strips rasters; its basemaps travel as tile archives). The new unowned path deliberately covers only vector layers, whose features could otherwise reach a home layer.

unowned.push(l.name.clone());
return false;
}
let keep = !baseline.layers.iter().any(|b| b.id == l.id && b.is_streamed());
if !keep {
reference.push(l.name.clone());
}
keep
});
let skipped_photos = if !reference.is_empty() && !photos.is_empty() {
let skipped_photos = if (!reference.is_empty() || !unowned.is_empty()) && !photos.is_empty() {
let before = photos.len();
// An entry may be shared by a kept feature and a dropped one. Only discard entries
// left without a reference among the layers passed to Sync.
Expand Down Expand Up @@ -1146,6 +1153,16 @@ pub(crate) fn bring_back(s: &mut Session, p: &Value) -> Result<Value, String> {
false,
);
}
if !unowned.is_empty() {
s.notify(
format!(
"Bring Back skipped {} layer(s) that aren't in offline area '{area}' (added in the field). Their features stay in the field project: {}",
unowned.len(),
unowned.join(", ")
),
false,
);
}
if skipped_photos > 0 {
s.notify(format!("Bring Back skipped {skipped_photos} photo(s) without a feature in the layers being brought back"), false);
}
Expand All @@ -1165,42 +1182,36 @@ pub(crate) fn bring_back(s: &mut Session, p: &Value) -> Result<Value, String> {
}

/// Adds to offline area `ai`'s source layers, and to the area's own copy of them (so features
/// aren't all taken as edited for lacking it), the fields `edited`'s layers have after theirs: a
/// field the field added. Only fields added after all of the layer's own, of names it hasn't;
/// fields that differ otherwise are left for the sync to refuse. Returns them as `layer: field`.
/// aren't all taken as edited for lacking it), the fields `edited`'s layers have that neither has:
/// a field the field added (a layer's photos field, made with its first photo). A field the area's
/// copy has and the source hasn't was removed at home, and isn't put back. Returns them as
/// `layer: field`.
fn take_new_fields(s: &mut Session, ai: usize, edited: &rustgis_map::Map) -> Vec<String> {
let Some(area) = s.project.offline_areas.get(ai) else { return Vec::new() };
let source = area.map.clone();
let same = |a: &[rustgis_data::Field], b: &[rustgis_data::Field]| {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x.name.eq_ignore_ascii_case(&y.name) && x.ty == y.ty)
};
let Some(si) = s.project.maps.iter().position(|m| m.name.eq_ignore_ascii_case(&source)) else { return Vec::new() };
let mut added = Vec::new();
for ol in edited.layers.iter().filter(|l| !l.is_raster()) {
let fields = &ol.dataset.fields;
let Some(si) = s.project.maps.iter().position(|m| m.name.eq_ignore_ascii_case(&source)) else { break };
let Some(sl) = s.project.maps.get(si).and_then(|m| m.layers.iter().find(|l| l.id == ol.id && !l.is_raster())) else { continue };
let had = sl.dataset.fields.len();
let base_same = s
.project
.offline_areas
.get(ai)
.and_then(|a| a.data.layers.iter().find(|l| l.id == ol.id))
.is_none_or(|b| same(&b.dataset.fields, &sl.dataset.fields));
let Some(extra) = fields.get(had..).filter(|e| !e.is_empty()) else { continue };
if !base_same || !fields.get(..had).is_some_and(|own| same(own, &sl.dataset.fields)) {
continue;
}
if extra.iter().any(|f| sl.dataset.field_index(&f.name).is_some()) {
let base = s.project.offline_areas.get(ai).and_then(|a| a.data.layers.iter().find(|l| l.id == ol.id));
let extra: Vec<rustgis_data::Field> = ol
.dataset
.fields
.iter()
.filter(|f| sl.dataset.field_index(&f.name).is_none() && base.is_none_or(|b| b.dataset.field_index(&f.name).is_none()))
.cloned()
.collect();
if extra.is_empty() {
continue;
}
let name = sl.name.clone();
if let Some(l) = s.project.maps.get_mut(si).and_then(|m| m.layers.iter_mut().find(|l| l.id == ol.id)) {
for f in extra {
for f in &extra {
l.dataset.ensure_field(&f.name, f.ty);
}
}
if let Some(b) = s.project.offline_areas.get_mut(ai).and_then(|a| a.data.layers.iter_mut().find(|l| l.id == ol.id)) {
for f in extra {
for f in &extra {
b.dataset.ensure_field(&f.name, f.ty);
}
}
Expand Down Expand Up @@ -2369,13 +2380,16 @@ mod tests {
field.run("edit.setValue", &json!({"layer": cities, "row": 0, "field": "photos", "value": photo.display().to_string()})).unwrap();
let (_, back) = package(&mut field, &Value::Null).unwrap();
std::fs::remove_file(&photo).unwrap();
// At home meanwhile, a field the field doesn't have: the sync is refused.
// At home meanwhile, a field the field has is removed: the sync is refused.
let home_cities = layer(&home, "Cities");
home.run("edit.addField", &json!({"layer": home_cities, "name": "extra", "type": "text"})).unwrap();
let gone = home.map().layer(home_cities).unwrap().dataset.fields[0].name.clone();
home.run("edit.deleteField", &json!({"layer": home_cities, "field": gone})).unwrap();
let path = temp.path().join("back.rgisfield");
std::fs::write(&path, &back).unwrap();
assert!(home.run("offline.bringBack", &json!({"path": path.display().to_string()})).is_err());
assert!(!temp.path().join("Back photos").exists());
// Nor the photos field it would have added.
assert!(home.map().layer(home_cities).unwrap().dataset.field_index("photos").is_none());
// A file holds bytes only as a file of their size: a folder, or another size, doesn't.
let file = temp.path().join("same.jpg");
std::fs::write(&file, b"abc").unwrap();
Expand Down
67 changes: 59 additions & 8 deletions crates/engine/src/cmd/field_audit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,25 +350,29 @@ fn audit_two_packages_same_feature_last_back_wins_and_is_said() {
assert!(said.iter().any(|t| t.contains("Cities #")), "the overwritten edit should be named: {said:?}");
}

/// The office adds a column to the layer while the crew is out. The crew's edits come back.
/// The office adds a column to the layer while the crew is out. The crew's edits come back, and
/// the office's values in its new column stay.
#[test]
#[ignore = "repro of a known field-sync bug (mobile offline audit); run with --ignored"]
fn audit_office_adds_a_field_while_crew_is_out() {
let temp = Temp::new("schema");
let mut home = home(&temp);
let (_, out) = package(&mut home, &json!({"name": "Day", "extent": AREA})).unwrap();
let mut a = phone(&temp, &out, "a");
let cities = layer(&a, "Cities");
let name = text_field(&a, cities);
let id = a.map().layer(cities).unwrap().dataset.features[0].id;
a.run("edit.setValue", &json!({"layer": cities, "row": 0, "field": name, "value": "Crew edit"})).unwrap();
add_point(&mut a, cities, &name, "Captured", [10.0, 5.0]);
let hc = layer(&home, "Cities");
home.run("edit.addField", &json!({"layer": hc, "name": "inspected", "type": "text"})).unwrap();
let (_, back) = package(&mut a, &Value::Null).unwrap();
let path = temp.0.join("back.rgisfield");
std::fs::write(&path, &back).unwrap();
let r = home.run("offline.bringBack", &json!({"path": path.display().to_string()}));
eprintln!("bring back after office schema change: {r:?}");
assert!(r.is_ok(), "the whole day's edits were refused: {r:?}");
let row = home.map().layer(hc).unwrap().dataset.features.iter().position(|f| f.id == id).unwrap();
home.run("edit.setValue", &json!({"layer": hc, "row": row, "field": "inspected", "value": "yes"})).unwrap();
let (done, said) = bring_back(&temp, &mut a, &mut home, "a");
assert_eq!((done["updated"].as_u64(), done["added"].as_u64(), done["conflicts"].as_u64()), (Some(1), Some(1), Some(0)), "{done}");
assert_eq!(value_of(&home, hc, id, &name).as_deref(), Some("Crew edit"));
assert_eq!(value_of(&home, hc, id, "inspected").as_deref(), Some("yes"));
assert_eq!(with_label(&home, hc, &name, "Captured"), 1);
assert!(!said.iter().any(|t| t.contains("replaced the map's changes")), "{said:?}");
}

/// The crew deletes a point on the phone that the office edited meanwhile, and the office
Expand Down Expand Up @@ -403,6 +407,53 @@ fn audit_delete_vs_edit_both_ways() {
assert!(said.iter().any(|t| t.contains("edits were dropped") && t.contains(&format!("Cities #{f1}"))), "{said:?}");
}

/// The crew deletes a feature the office has since given a value in a field it added: the
/// office's value is lost with the feature, so it is reported as a conflict.
#[test]
fn deleted_in_the_field_after_the_office_filled_its_new_field() {
let temp = Temp::new("delete-new-field");
let mut home = home(&temp);
let (_, out) = package(&mut home, &json!({"name": "Day", "extent": AREA})).unwrap();
let mut a = phone(&temp, &out, "a");
let cities = layer(&a, "Cities");
let id = a.map().layer(cities).unwrap().dataset.features[0].id;
a.run("select.rows", &json!({"layer": cities, "rows": [0]})).unwrap();
a.run("edit.delete", &Value::Null).unwrap();
let hc = layer(&home, "Cities");
home.run("edit.addField", &json!({"layer": hc, "name": "inspected", "type": "text"})).unwrap();
let row = home.map().layer(hc).unwrap().dataset.features.iter().position(|f| f.id == id).unwrap();
home.run("edit.setValue", &json!({"layer": hc, "row": row, "field": "inspected", "value": "flagged"})).unwrap();
let (done, said) = bring_back(&temp, &mut a, &mut home, "a");
assert_eq!((done["deleted"].as_u64(), done["conflicts"].as_u64()), (Some(1), Some(1)), "{done}");
assert!(said.iter().any(|t| t.contains("replaced the map's changes") && t.contains(&format!("Cities #{id}"))), "{said:?}");
}

/// A layer added in the field, whose id a layer added at home has taken meanwhile: it isn't the
/// area's, so none of its fields or features reach the home layer of that id, and it is said.
#[test]
fn a_layer_added_in_the_field_stays_out_of_home_layers() {
use base64::Engine;
let point = |label: &str| {
let text = format!(
r#"{{"type":"FeatureCollection","features":[{{"type":"Feature","properties":{{"crew_only":"{label}"}},"geometry":{{"type":"Point","coordinates":[10,5]}}}}]}}"#
);
base64::engine::general_purpose::STANDARD.encode(text)
};
let temp = Temp::new("field-layer");
let mut home = home(&temp);
let (_, out) = package(&mut home, &json!({"name": "Day", "extent": AREA})).unwrap();
let mut a = phone(&temp, &out, "a");
a.run("layer.addDataBytes", &json!({"fileName": "crew.geojson", "data": point("crew"), "name": "Crew layer"})).unwrap();
home.run("layer.addDataBytes", &json!({"fileName": "office.geojson", "data": point("office"), "name": "Office layer"})).unwrap();
let (crew, office) = (layer(&a, "Crew layer"), layer(&home, "Office layer"));
assert_eq!(crew, office, "both sides give their new layer the next id");
let (done, said) = bring_back(&temp, &mut a, &mut home, "a");
let ds = &home.map().layer(office).unwrap().dataset;
assert_eq!(ds.features.len(), 1, "ids {crew} / {office}: {done}");
assert_eq!(ds.fields.len(), 1, "ids {crew} / {office}");
assert!(said.iter().any(|t| t.contains("skipped 1 layer(s) that aren't in offline area") && t.contains("Crew layer")), "{said:?}");
}

#[cfg(not(target_arch = "wasm32"))]
fn roads_gpkg(dir: &Path) -> PathBuf {
let path = dir.join("roads.gpkg");
Expand Down
Loading
Loading