mapcomplete/Logic/Osm/OsmConnection.ts

361 lines
12 KiB
TypeScript
Raw Normal View History

2020-06-24 00:35:19 +02:00
// @ts-ignore
import osmAuth from "osm-auth";
2020-07-30 00:59:08 +02:00
import {UIEventSource} from "../../UI/UIEventSource";
2020-07-31 04:58:58 +02:00
import {CustomLayersState} from "../CustomLayersState";
2020-06-24 00:35:19 +02:00
export class UserDetails {
public loggedIn = false;
public name = "Not logged in";
public csCount = 0;
public img: string;
public unreadMessages = 0;
public totalMessages = 0;
2020-06-29 03:12:44 +02:00
public dryRun: boolean;
home: { lon: number; lat: number };
2020-06-24 00:35:19 +02:00
}
export class OsmConnection {
public auth;
2020-06-24 00:35:19 +02:00
public userDetails: UIEventSource<UserDetails>;
private _dryRun: boolean;
2020-08-08 21:17:17 +02:00
constructor(dryRun: boolean, oauth_token: UIEventSource<string>, singlePage: boolean = true) {
let pwaStandAloneMode = false;
try {
if (window.matchMedia('(display-mode: standalone)').matches || window.matchMedia('(display-mode: fullscreen)').matches) {
pwaStandAloneMode = true;
}
} catch (e) {
console.warn("Detecting standalone mode failed", e, ". Assuming in browser and not worrying furhter")
}
2020-07-31 18:38:14 +02:00
const iframeMode = window !== window.top;
2020-08-08 21:17:17 +02:00
if ( iframeMode || !singlePage) {
// In standalone mode, we DON'T use single page login, as 'redirecting' opens a new window anyway...
2020-07-31 18:38:14 +02:00
// Same for an iframe...
this.auth = new osmAuth({
oauth_consumer_key: 'hivV7ec2o49Two8g9h8Is1VIiVOgxQ1iYexCbvem',
oauth_secret: 'wDBRTCem0vxD7txrg1y6p5r8nvmz8tAhET7zDASI',
singlepage: false,
auto: true
});
} else {
this.auth = new osmAuth({
oauth_consumer_key: 'hivV7ec2o49Two8g9h8Is1VIiVOgxQ1iYexCbvem',
oauth_secret: 'wDBRTCem0vxD7txrg1y6p5r8nvmz8tAhET7zDASI',
singlepage: true,
landing: window.location.href,
auto: true
});
}
2020-06-24 00:35:19 +02:00
this.userDetails = new UIEventSource<UserDetails>(new UserDetails());
this.userDetails.data.dryRun = dryRun;
2020-06-24 00:35:19 +02:00
this._dryRun = dryRun;
if (oauth_token.data !== undefined) {
console.log(oauth_token.data)
const self = this;
this.auth.bootstrapToken(oauth_token.data,
(x) => {
console.log("Called back: ", x)
self.AttemptLogin();
}, this.auth);
oauth_token.setData(undefined);
}
2020-06-24 00:35:19 +02:00
if (this.auth.authenticated()) {
this.AttemptLogin(); // Also updates the user badge
} else {
2020-06-24 00:35:19 +02:00
console.log("Not authenticated");
}
if (dryRun) {
2020-06-24 00:35:19 +02:00
console.log("DRYRUN ENABLED");
}
2020-06-24 00:35:19 +02:00
}
public LogOut() {
this.auth.logout();
2020-07-01 17:51:55 +02:00
this.userDetails.data.loggedIn = false;
this.userDetails.ping();
console.log("Logged out")
2020-06-24 00:35:19 +02:00
}
public AttemptLogin() {
const self = this;
this.auth.xhr({
method: 'GET',
path: '/api/0.6/user/details'
}, function (err, details) {
if(err != null){
console.log(err);
self.auth.logout();
self.userDetails.data.loggedIn = false;
self.userDetails.ping();
}
if (details == null) {
2020-06-24 00:35:19 +02:00
return;
}
2020-06-29 03:12:44 +02:00
self.UpdatePreferences();
self.CheckForMessagesContinuously();
2020-06-24 00:35:19 +02:00
// details is an XML DOM of user details
let userInfo = details.getElementsByTagName("user")[0];
// let moreDetails = new DOMParser().parseFromString(userInfo.innerHTML, "text/xml");
2020-06-24 00:35:19 +02:00
let data = self.userDetails.data;
data.loggedIn = true;
2020-07-01 21:21:29 +02:00
console.log("Login completed, userinfo is ", userInfo);
2020-06-24 00:35:19 +02:00
data.name = userInfo.getAttribute('display_name');
data.csCount = userInfo.getElementsByTagName("changesets")[0].getAttribute("count");
2020-06-28 23:33:48 +02:00
data.img = undefined;
const imgEl = userInfo.getElementsByTagName("img");
if (imgEl !== undefined && imgEl[0] !== undefined) {
data.img = imgEl[0].getAttribute("href");
}
data.img = data.img ?? "./assets/osm-logo.svg";
2020-06-29 03:12:44 +02:00
const homeEl = userInfo.getElementsByTagName("home");
if (homeEl !== undefined && homeEl[0] !== undefined) {
const lat = parseFloat(homeEl[0].getAttribute("lat"));
const lon = parseFloat(homeEl[0].getAttribute("lon"));
data.home = {lat: lat, lon: lon};
}
const messages = userInfo.getElementsByTagName("messages")[0].getElementsByTagName("received")[0];
data.unreadMessages = parseInt(messages.getAttribute("unread"));
data.totalMessages = parseInt(messages.getAttribute("count"));
2020-06-24 00:35:19 +02:00
self.userDetails.ping();
2020-06-24 00:35:19 +02:00
});
}
private CheckForMessagesContinuously() {
const self = this;
window.setTimeout(() => {
if (self.userDetails.data.loggedIn) {
console.log("Checking for messages")
this.AttemptLogin();
}
}, 5 * 60 * 1000);
}
2020-06-29 03:12:44 +02:00
public preferences = new UIEventSource<any>({});
public preferenceSources : any = {}
2020-08-07 16:01:18 +02:00
public GetPreference(key: string, prefix : string = "mapcomplete-") : UIEventSource<string>{
key = prefix+key;
if (this.preferenceSources[key] !== undefined) {
return this.preferenceSources[key];
}
2020-08-07 16:01:18 +02:00
if (this.userDetails.data.loggedIn && this.preferences.data[key] === undefined) {
this.UpdatePreferences();
}
2020-07-15 10:53:00 +02:00
const pref = new UIEventSource<string>(this.preferences.data[key]);
pref.addCallback((v) => {
this.SetPreference(key, v);
});
this.preferences.addCallback((prefs) => {
if (prefs[key] !== undefined) {
pref.setData(prefs[key]);
}
});
this.preferenceSources[key] = pref;
return pref;
}
2020-06-29 03:12:44 +02:00
private UpdatePreferences() {
const self = this;
this.auth.xhr({
method: 'GET',
path: '/api/0.6/user/preferences'
}, function (error, value: XMLDocument) {
if(error){
console.log("Could not load preferences", error);
return;
}
const prefs = value.getElementsByTagName("preference");
for (let i = 0; i < prefs.length; i++) {
const pref = prefs[i];
const k = pref.getAttribute("k");
const v = pref.getAttribute("v");
self.preferences.data[k] = v;
}
self.preferences.ping();
});
}
private SetPreference(k:string, v:string) {
2020-07-05 18:59:47 +02:00
if(!this.userDetails.data.loggedIn){
console.log("Not saving preference: user not logged in");
return;
}
2020-07-01 21:21:29 +02:00
if (this.preferences.data[k] === v) {
2020-07-15 10:47:01 +02:00
console.log("Not updating preference", k, " to ", v, "not changed");
2020-07-01 21:21:29 +02:00
return;
}
console.log("Updating preference", k, " to ", v);
2020-06-29 03:12:44 +02:00
this.preferences.data[k] = v;
this.preferences.ping();
2020-07-31 16:17:16 +02:00
if(v === ""){
this.auth.xhr({
method: 'DELETE',
path: '/api/0.6/user/preferences/' + k,
options: {header: {'Content-Type': 'text/plain'}},
}, function (error, result) {
if (error) {
console.log("Could not remove preference", error);
return;
}
console.log("Preference removed!", result == "" ? "OK" : result);
});
}
2020-06-29 03:12:44 +02:00
this.auth.xhr({
method: 'PUT',
2020-07-01 21:21:29 +02:00
path: '/api/0.6/user/preferences/' + k,
options: {header: {'Content-Type': 'text/plain'}},
2020-06-29 03:12:44 +02:00
content: v
2020-07-01 21:21:29 +02:00
}, function (error, result) {
if (error) {
2020-06-29 03:12:44 +02:00
console.log("Could not set preference", error);
return;
}
2020-07-15 10:53:00 +02:00
console.log("Preference written!", result == "" ? "OK" : result);
2020-06-29 03:12:44 +02:00
});
}
2020-06-24 00:35:19 +02:00
private static parseUploadChangesetResponse(response: XMLDocument) {
const nodes = response.getElementsByTagName("node");
const mapping = {};
// @ts-ignore
for (const node of nodes) {
const oldId = parseInt(node.attributes.old_id.value);
const newId = parseInt(node.attributes.new_id.value);
if (oldId !== undefined && newId !== undefined &&
!isNaN(oldId) && !isNaN(newId)) {
mapping["node/"+oldId] = "node/"+newId;
}
}
return mapping;
}
public UploadChangeset(comment: string, generateChangeXML: ((csid: string) => string),
handleMapping: ((idMapping: any) => void),
continuation: (() => void)) {
if (this._dryRun) {
console.log("NOT UPLOADING as dryrun is true");
var changesetXML = generateChangeXML("123456");
console.log(changesetXML);
continuation();
2020-06-24 00:35:19 +02:00
return;
}
const self = this;
this.OpenChangeset(comment,
function (csId) {
var changesetXML = generateChangeXML(csId);
self.AddChange(csId, changesetXML,
function (csId, mapping) {
self.CloseChangeset(csId, continuation);
handleMapping(mapping);
}
);
}
);
this.userDetails.data.csCount++;
this.userDetails.ping();
}
private OpenChangeset(comment: string, continuation: ((changesetId: string) => void)) {
this.auth.xhr({
method: 'PUT',
path: '/api/0.6/changeset/create',
options: { header: { 'Content-Type': 'text/xml' } },
content: '<osm><changeset>' +
'<tag k="created_by" v="MapComplete 0.0.0" />' +
'<tag k="comment" v="' + comment + '"/>' +
'</changeset></osm>'
}, function (err, response) {
if (response === undefined) {
console.log("err", err);
return;
} else {
continuation(response);
}
});
}
private AddChange(changesetId: string,
changesetXML: string,
continuation: ((changesetId: string, idMapping: any) => void)){
this.auth.xhr({
method: 'POST',
options: { header: { 'Content-Type': 'text/xml' } },
path: '/api/0.6/changeset/'+changesetId+'/upload',
content: changesetXML
}, function (err, response) {
if (response == null) {
console.log("err", err);
return;
}
const mapping = OsmConnection.parseUploadChangesetResponse(response);
2020-08-06 23:49:35 +02:00
console.log("Uploaded changeset ", changesetId);
2020-06-24 00:35:19 +02:00
continuation(changesetId, mapping);
});
}
private CloseChangeset(changesetId: string, continuation : (() => void)) {
console.log("closing");
this.auth.xhr({
method: 'PUT',
path: '/api/0.6/changeset/'+changesetId+'/close',
}, function (err, response) {
if (response == null) {
console.log("err", err);
}
console.log("Closed changeset ", changesetId);
if(continuation !== undefined){
continuation();
}
});
}
}