Update OSM data when opening saved places
All checks were successful
CI / Lint (pull_request) Successful in 49s
CI / Test (pull_request) Successful in 57s
Release Drafter / Update release notes draft (pull_request) Successful in 19s

This commit is contained in:
2026-03-18 14:33:27 +04:00
parent f7c40095d5
commit bdd5db157c
4 changed files with 354 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
import Service from '@ember/service';
import Service, { service } from '@ember/service';
import RemoteStorage from 'remotestoragejs';
import Places from '@remotestorage/module-places';
import Widget from 'remotestorage-widget';
@@ -7,8 +7,10 @@ import { getGeohashPrefixesInBbox } from '../utils/geohash-coverage';
import { action } from '@ember/object';
import { debounceTask } from 'ember-lifeline';
import Geohash from 'latlon-geohash';
import { getLocalizedName } from '../utils/osm';
export default class StorageService extends Service {
@service osm;
rs;
widget;
@tracked placesInView = [];
@@ -366,6 +368,82 @@ export default class StorageService extends Service {
}
}
async refreshPlace(place) {
if (!place || !place.id || !place.osmId || !place.osmType) {
return null;
}
try {
console.debug(`Checking for updates for ${place.title} (${place.osmId})`);
const freshData = await this.osm.fetchOsmObject(
place.osmId,
place.osmType
);
if (!freshData) {
console.warn('Could not fetch fresh data for', place.osmId);
return null;
}
// Check for changes
let hasChanges = false;
const changes = {};
// 1. Check Coordinates (allow tiny drift < ~1m)
const latDiff = Math.abs(place.lat - freshData.lat);
const lonDiff = Math.abs(place.lon - freshData.lon);
if (latDiff > 0.00001 || lonDiff > 0.00001) {
hasChanges = true;
changes.lat = freshData.lat;
changes.lon = freshData.lon;
}
// 2. Check Tags
const oldTags = place.osmTags || {};
const newTags = freshData.osmTags || {};
const allKeys = new Set([
...Object.keys(oldTags),
...Object.keys(newTags),
]);
for (const key of allKeys) {
if (oldTags[key] !== newTags[key]) {
hasChanges = true;
changes.osmTags = newTags;
break;
}
}
if (!hasChanges) {
console.debug('No changes detected for', place.title);
return null;
}
console.debug('Changes detected:', changes);
// 3. Prepare Update
const updatedPlace = {
...place,
...changes,
};
// If the current title matches the old localized name, update it to the
// new localized name. If the user renamed it (custom title), keep it.
const oldDefaultName = getLocalizedName(oldTags);
const newDefaultName = getLocalizedName(newTags);
if (place.title === oldDefaultName && oldDefaultName !== newDefaultName) {
updatedPlace.title = newDefaultName;
}
// 4. Save
return await this.updatePlace(updatedPlace);
} catch (e) {
console.error('Failed to refresh place:', e);
return null;
}
}
@action
connect() {
this.isWidgetOpen = true;