Attempting to add in backend to the element

This commit is contained in:
Pieter Vander Vennet 2021-07-10 13:47:53 +02:00
parent ba5cbe9b5c
commit e11a5ca17b
2 changed files with 29 additions and 25 deletions

View file

@ -6,31 +6,31 @@ import Constants from "../../Models/Constants";
import FeatureSource from "../FeatureSource/FeatureSource"; import FeatureSource from "../FeatureSource/FeatureSource";
import {TagsFilter} from "../Tags/TagsFilter"; import {TagsFilter} from "../Tags/TagsFilter";
import {Tag} from "../Tags/Tag"; import {Tag} from "../Tags/Tag";
import {OsmConnection} from "./OsmConnection";
/** /**
* Handles all changes made to OSM. * Handles all changes made to OSM.
* Needs an authenticator via OsmConnection * Needs an authenticator via OsmConnection
*/ */
export class Changes implements FeatureSource{ export class Changes implements FeatureSource {
private static _nextId = -1; // Newly assigned ID's are negative
public readonly name = "Newly added features" public readonly name = "Newly added features"
/** /**
* The newly created points, as a FeatureSource * The newly created points, as a FeatureSource
*/ */
public features = new UIEventSource<{feature: any, freshness: Date}[]>([]); public features = new UIEventSource<{ feature: any, freshness: Date }[]>([]);
private static _nextId = -1; // Newly assigned ID's are negative
/** /**
* All the pending changes * All the pending changes
*/ */
public readonly pending: UIEventSource<{ elementId: string, key: string, value: string }[]> = public readonly pending: UIEventSource<{ elementId: string, key: string, value: string }[]> =
new UIEventSource<{elementId: string; key: string; value: string}[]>([]); new UIEventSource<{ elementId: string; key: string; value: string }[]>([]);
/** /**
* Adds a change to the pending changes * Adds a change to the pending changes
*/ */
private static checkChange(kv: {k: string, v: string}): { k: string, v: string } { private static checkChange(kv: { k: string, v: string }): { k: string, v: string } {
const key = kv.k; const key = kv.k;
const value = kv.v; const value = kv.v;
if (key === undefined || key === null) { if (key === undefined || key === null) {
@ -49,8 +49,7 @@ export class Changes implements FeatureSource{
return {k: key.trim(), v: value.trim()}; return {k: key.trim(), v: value.trim()};
} }
addTag(elementId: string, tagsFilter: TagsFilter, addTag(elementId: string, tagsFilter: TagsFilter,
tags?: UIEventSource<any>) { tags?: UIEventSource<any>) {
const eventSource = tags ?? State.state?.allElements.getEventSourceById(elementId); const eventSource = tags ?? State.state?.allElements.getEventSourceById(elementId);
@ -59,7 +58,7 @@ export class Changes implements FeatureSource{
if (changes.length == 0) { if (changes.length == 0) {
return; return;
} }
for (const change of changes) { for (const change of changes) {
if (elementTags[change.k] !== change.v) { if (elementTags[change.k] !== change.v) {
elementTags[change.k] = change.v; elementTags[change.k] = change.v;
@ -76,16 +75,17 @@ export class Changes implements FeatureSource{
* Uploads all the pending changes in one go. * Uploads all the pending changes in one go.
* Triggered by the 'PendingChangeUploader'-actor in Actors * Triggered by the 'PendingChangeUploader'-actor in Actors
*/ */
public flushChanges(flushreason: string = undefined){ public flushChanges(flushreason: string = undefined) {
if(this.pending.data.length === 0){ if (this.pending.data.length === 0) {
return; return;
} }
if(flushreason !== undefined){ if (flushreason !== undefined) {
console.log(flushreason) console.log(flushreason)
} }
this.uploadAll([], this.pending.data); this.uploadAll([], this.pending.data);
this.pending.setData([]); this.pending.setData([]);
} }
/** /**
* Create a new node element at the given lat/long. * Create a new node element at the given lat/long.
* An internal OsmObject is created to upload later on, a geojson represention is returned. * An internal OsmObject is created to upload later on, a geojson represention is returned.
@ -118,33 +118,37 @@ export class Changes implements FeatureSource{
// The tags are not yet written into the OsmObject, but this is applied onto a // The tags are not yet written into the OsmObject, but this is applied onto a
const changes = []; const changes = [];
for (const kv of basicTags) { for (const kv of basicTags) {
properties[kv.key] = kv.value;
if (typeof kv.value !== "string") { if (typeof kv.value !== "string") {
throw "Invalid value: don't use a regex in a preset" throw "Invalid value: don't use a regex in a preset"
} }
properties[kv.key] = kv.value;
changes.push({elementId: id, key: kv.key, value: kv.value}) changes.push({elementId: id, key: kv.key, value: kv.value})
} }
console.log("New feature added and pinged") console.log("New feature added and pinged")
this.features.data.push({feature:geojson, freshness: new Date()}); this.features.data.push({feature: geojson, freshness: new Date()});
this.features.ping(); this.features.ping();
State.state.allElements.addOrGetElement(geojson).ping(); State.state.allElements.addOrGetElement(geojson).ping();
this.uploadAll([osmNode], changes); if (State.state.osmConnection.userDetails.data.backend !== OsmConnection.oauth_configs.osm.url) {
properties["_backend"] = State.state.osmConnection.userDetails.data.backend
}
// this.uploadAll([osmNode], changes);
return geojson; return geojson;
} }
private uploadChangesWithLatestVersions( private uploadChangesWithLatestVersions(
knownElements: OsmObject[], newElements: OsmObject[], pending: { elementId: string; key: string; value: string }[]) { knownElements: OsmObject[], newElements: OsmObject[], pending: { elementId: string; key: string; value: string }[]) {
const knownById = new Map<string, OsmObject>(); const knownById = new Map<string, OsmObject>();
knownElements.forEach(knownElement => { knownElements.forEach(knownElement => {
console.log("Setting ",knownElement.type + knownElement.id, knownElement) console.log("Setting ", knownElement.type + knownElement.id, knownElement)
knownById.set(knownElement.type + "/" + knownElement.id, knownElement) knownById.set(knownElement.type + "/" + knownElement.id, knownElement)
}) })
// Here, inside the continuation, we know that all 'neededIds' are loaded in 'knownElements', which maps the ids onto the elements // Here, inside the continuation, we know that all 'neededIds' are loaded in 'knownElements', which maps the ids onto the elements
// We apply the changes on them // We apply the changes on them
for (const change of pending) { for (const change of pending) {

View file

@ -30,7 +30,7 @@ export default class UserDetails {
export class OsmConnection { export class OsmConnection {
public static readonly _oauth_configs = { public static readonly oauth_configs = {
"osm": { "osm": {
oauth_consumer_key: 'hivV7ec2o49Two8g9h8Is1VIiVOgxQ1iYexCbvem', oauth_consumer_key: 'hivV7ec2o49Two8g9h8Is1VIiVOgxQ1iYexCbvem',
oauth_secret: 'wDBRTCem0vxD7txrg1y6p5r8nvmz8tAhET7zDASI', oauth_secret: 'wDBRTCem0vxD7txrg1y6p5r8nvmz8tAhET7zDASI',
@ -66,7 +66,7 @@ export class OsmConnection {
osmConfiguration: "osm" | "osm-test" = 'osm' osmConfiguration: "osm" | "osm-test" = 'osm'
) { ) {
this._singlePage = singlePage; this._singlePage = singlePage;
this._oauth_config = OsmConnection._oauth_configs[osmConfiguration] ?? OsmConnection._oauth_configs.osm; this._oauth_config = OsmConnection.oauth_configs[osmConfiguration] ?? OsmConnection.oauth_configs.osm;
console.debug("Using backend", this._oauth_config.url) console.debug("Using backend", this._oauth_config.url)
OsmObject.SetBackendUrl(this._oauth_config.url + "/") OsmObject.SetBackendUrl(this._oauth_config.url + "/")
this._iframeMode = Utils.runningFromConsole ? false : window !== window.top; this._iframeMode = Utils.runningFromConsole ? false : window !== window.top;