This commit is contained in:
Pieter Vander Vennet 2024-08-24 12:15:06 +02:00
parent 90916cdd32
commit 8959f72e92

View file

@ -37,7 +37,7 @@ export class Changes {
public readonly isUploading = new UIEventSource(false) public readonly isUploading = new UIEventSource(false)
public readonly errors = new UIEventSource<string[]>([], "upload-errors") public readonly errors = new UIEventSource<string[]>([], "upload-errors")
private readonly historicalUserLocations?: FeatureSource private readonly historicalUserLocations?: FeatureSource
private _nextId: number = -1 // Newly assigned ID's are negative private _nextId: number = 0 // Newly assigned ID's are negative
private readonly previouslyCreated: OsmObject[] = [] private readonly previouslyCreated: OsmObject[] = []
private readonly _leftRightSensitive: boolean private readonly _leftRightSensitive: boolean
public readonly _changesetHandler: ChangesetHandler public readonly _changesetHandler: ChangesetHandler
@ -53,7 +53,7 @@ export class Changes {
featureSwitches?: FeatureSwitchState featureSwitches?: FeatureSwitchState
}, },
leftRightSensitive: boolean = false, leftRightSensitive: boolean = false,
reportError?: (string: string | Error, extramessage?: string) => void reportError?: (string: string | Error, extramessage?: string) => void,
) { ) {
this._leftRightSensitive = leftRightSensitive this._leftRightSensitive = leftRightSensitive
// We keep track of all changes just as well // We keep track of all changes just as well
@ -68,7 +68,7 @@ export class Changes {
state.osmConnection, state.osmConnection,
state.featurePropertiesStore, state.featurePropertiesStore,
this, this,
(e, extramessage: string) => this._reportError(e, extramessage) (e, extramessage: string) => this._reportError(e, extramessage),
) )
this.historicalUserLocations = state.historicalUserLocations this.historicalUserLocations = state.historicalUserLocations
@ -82,7 +82,7 @@ export class Changes {
modifiedObjects: OsmObject[] modifiedObjects: OsmObject[]
newObjects: OsmObject[] newObjects: OsmObject[]
deletedObjects: OsmObject[] deletedObjects: OsmObject[]
} },
): string { ): string {
const changedElements = allChanges.modifiedObjects ?? [] const changedElements = allChanges.modifiedObjects ?? []
const newElements = allChanges.newObjects ?? [] const newElements = allChanges.newObjects ?? []
@ -172,7 +172,7 @@ export class Changes {
docs: "The identifier of the used background layer, this will probably be an identifier from the [editor layer index](https://github.com/osmlab/editor-layer-index)", docs: "The identifier of the used background layer, this will probably be an identifier from the [editor layer index](https://github.com/osmlab/editor-layer-index)",
}, },
], ],
"default" "default",
), ),
...addSource(ChangeTagAction.metatags, "ChangeTag"), ...addSource(ChangeTagAction.metatags, "ChangeTag"),
...addSource(ChangeLocationAction.metatags, "ChangeLocation"), ...addSource(ChangeLocationAction.metatags, "ChangeLocation"),
@ -201,7 +201,7 @@ export class Changes {
: "", : "",
].join("\n"), ].join("\n"),
source, source,
]) ]),
), ),
].join("\n\n") ].join("\n\n")
} }
@ -214,7 +214,11 @@ export class Changes {
* Returns a new ID and updates the value for the next ID * Returns a new ID and updates the value for the next ID
*/ */
public getNewID() { public getNewID() {
return this._nextId-- // See #2082. We check for previous rewritings, as a remapping might be from a previous session
do {
this._nextId--
} while (this._changesetHandler._remappings.has("node/" + this._nextId) || this._changesetHandler._remappings.has("way/" + this._nextId) || this._changesetHandler._remappings.has("relation/" + this._nextId))
return this._nextId
} }
/** /**
@ -250,7 +254,7 @@ export class Changes {
const changeDescriptions = await action.Perform(this) const changeDescriptions = await action.Perform(this)
const remapped = ChangeDescriptionTools.rewriteAllIds( const remapped = ChangeDescriptionTools.rewriteAllIds(
changeDescriptions, changeDescriptions,
this._changesetHandler._remappings this._changesetHandler._remappings,
) )
remapped[0].meta.distanceToObject = this.calculateDistanceToChanges(action, remapped) remapped[0].meta.distanceToObject = this.calculateDistanceToChanges(action, remapped)
@ -266,7 +270,7 @@ export class Changes {
public CreateChangesetObjects( public CreateChangesetObjects(
changes: ChangeDescription[], changes: ChangeDescription[],
downloadedOsmObjects: OsmObject[] downloadedOsmObjects: OsmObject[],
): { ): {
newObjects: OsmObject[] newObjects: OsmObject[]
modifiedObjects: OsmObject[] modifiedObjects: OsmObject[]
@ -454,7 +458,7 @@ export class Changes {
result.modifiedObjects.length, result.modifiedObjects.length,
"modified;", "modified;",
result.deletedObjects.length, result.deletedObjects.length,
"deleted" "deleted",
) )
} }
return result return result
@ -462,7 +466,7 @@ export class Changes {
private calculateDistanceToChanges( private calculateDistanceToChanges(
change: OsmChangeAction, change: OsmChangeAction,
changeDescriptions: ChangeDescription[] changeDescriptions: ChangeDescription[],
) { ) {
const locations = this.historicalUserLocations?.features?.data const locations = this.historicalUserLocations?.features?.data
if (locations === undefined) { if (locations === undefined) {
@ -482,7 +486,7 @@ export class Changes {
.filter((feat) => feat.geometry.type === "Point") .filter((feat) => feat.geometry.type === "Point")
.filter((feat) => { .filter((feat) => {
const visitTime = new Date( const visitTime = new Date(
(<GeoLocationPointProperties>(<any>feat.properties)).date (<GeoLocationPointProperties>(<any>feat.properties)).date,
) )
// In seconds // In seconds
const diff = (now.getTime() - visitTime.getTime()) / 1000 const diff = (now.getTime() - visitTime.getTime()) / 1000
@ -529,9 +533,9 @@ export class Changes {
...recentLocationPoints.map((gpsPoint) => { ...recentLocationPoints.map((gpsPoint) => {
const otherCoor = GeoOperations.centerpointCoordinates(gpsPoint) const otherCoor = GeoOperations.centerpointCoordinates(gpsPoint)
return GeoOperations.distanceBetween(coor, otherCoor) return GeoOperations.distanceBetween(coor, otherCoor)
}) }),
) ),
) ),
) )
} }
@ -567,7 +571,7 @@ export class Changes {
public fragmentChanges( public fragmentChanges(
pending: ChangeDescription[], pending: ChangeDescription[],
objects: OsmObject[] objects: OsmObject[],
): { ): {
refused: ChangeDescription[] refused: ChangeDescription[]
toUpload: ChangeDescription[] toUpload: ChangeDescription[]
@ -577,7 +581,7 @@ export class Changes {
// All ids which have an 'update' // All ids which have an 'update'
const createdIds = new Set( const createdIds = new Set(
pending.filter((cd) => cd.changes !== undefined).map((cd) => cd.id) pending.filter((cd) => cd.changes !== undefined).map((cd) => cd.id),
) )
pending.forEach((c) => { pending.forEach((c) => {
if (c.id < 0) { if (c.id < 0) {
@ -586,7 +590,7 @@ export class Changes {
} else { } else {
this._reportError( this._reportError(
`Got an orphaned change. The 'creation'-change description for ${c.type}/${c.id} got lost. Permanently dropping this change:` + `Got an orphaned change. The 'creation'-change description for ${c.type}/${c.id} got lost. Permanently dropping this change:` +
JSON.stringify(c) JSON.stringify(c),
) )
} }
return return
@ -600,7 +604,7 @@ export class Changes {
c.type + c.type +
"/" + "/" +
c.id + c.id +
" as not in the objects. No internet?" " as not in the objects. No internet?",
) )
refused.push(c) refused.push(c)
} }
@ -615,7 +619,7 @@ export class Changes {
*/ */
private async flushSelectChanges( private async flushSelectChanges(
pending: ChangeDescription[], pending: ChangeDescription[],
openChangeset: UIEventSource<number> openChangeset: UIEventSource<number>,
): Promise<ChangeDescription[]> { ): Promise<ChangeDescription[]> {
const neededIds = Changes.GetNeededIds(pending) const neededIds = Changes.GetNeededIds(pending)
/* Download the latest version of the OSM-objects /* Download the latest version of the OSM-objects
@ -623,7 +627,7 @@ export class Changes {
*/ */
const downloader = new OsmObjectDownloader(this.backend, undefined) const downloader = new OsmObjectDownloader(this.backend, undefined)
const osmObjects = Utils.NoNull(await Promise.all<{ id: string; osmObj: OsmObject | "deleted" }>( const osmObjects = Utils.NoNull(await Promise.all<{ id: string; osmObj: OsmObject | "deleted" }>(
neededIds.map((id) => this.getOsmObject(id, downloader)) neededIds.map((id) => this.getOsmObject(id, downloader)),
)) ))
// Drop changes to deleted items // Drop changes to deleted items
@ -657,7 +661,7 @@ export class Changes {
(csId, remappings) => { (csId, remappings) => {
if (remappings.size > 0) { if (remappings.size > 0) {
toUpload = toUpload.map((ch) => toUpload = toUpload.map((ch) =>
ChangeDescriptionTools.rewriteIds(ch, remappings) ChangeDescriptionTools.rewriteIds(ch, remappings),
) )
} }
@ -670,7 +674,7 @@ export class Changes {
return Changes.buildChangesetXML("" + csId, changes) return Changes.buildChangesetXML("" + csId, changes)
}, },
metatags, metatags,
openChangeset openChangeset,
) )
console.log("Upload successful! Refused changes are", refused) console.log("Upload successful! Refused changes are", refused)
@ -780,14 +784,14 @@ export class Changes {
try { try {
const openChangeset = UIEventSource.asInt( const openChangeset = UIEventSource.asInt(
this.state.osmConnection.GetPreference( this.state.osmConnection.GetPreference(
"current-open-changeset-" + theme "current-open-changeset-" + theme,
) ),
) )
console.log( console.log(
"Using current-open-changeset-" + "Using current-open-changeset-" +
theme + theme +
" from the preferences, got " + " from the preferences, got " +
openChangeset.data openChangeset.data,
) )
const refused = await self.flushSelectChanges(pendingChanges, openChangeset) const refused = await self.flushSelectChanges(pendingChanges, openChangeset)
@ -802,7 +806,7 @@ export class Changes {
this.errors.ping() this.errors.ping()
return pendingChanges return pendingChanges
} }
}) }),
) )
// We keep all the refused changes to try them again // We keep all the refused changes to try them again
@ -810,7 +814,7 @@ export class Changes {
} catch (e) { } catch (e) {
console.error( console.error(
"Could not handle changes - probably an old, pending changeset in localstorage with an invalid format; erasing those", "Could not handle changes - probably an old, pending changeset in localstorage with an invalid format; erasing those",
e e,
) )
this.errors.data.push(e) this.errors.data.push(e)
this.errors.ping() this.errors.ping()