2020-06-24 00:35:19 +02:00
|
|
|
// @ts-ignore
|
|
|
|
import osmAuth from "osm-auth";
|
2020-08-17 17:23:15 +02:00
|
|
|
import {UIEventSource} from "../UIEventSource";
|
2020-08-22 02:12:46 +02:00
|
|
|
import {State} from "../../State";
|
2020-08-26 15:36:04 +02:00
|
|
|
import {All} from "../../Customizations/Layouts/All";
|
|
|
|
import {OsmPreferences} from "./OsmPreferences";
|
|
|
|
import {ChangesetHandler} from "./ChangesetHandler";
|
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;
|
2020-06-27 03:06:51 +02:00
|
|
|
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 {
|
|
|
|
|
2020-07-29 21:32:51 +02:00
|
|
|
public auth;
|
2020-06-24 00:35:19 +02:00
|
|
|
public userDetails: UIEventSource<UserDetails>;
|
|
|
|
private _dryRun: boolean;
|
|
|
|
|
2020-08-26 15:36:04 +02:00
|
|
|
public _preferencesHandler: OsmPreferences;
|
|
|
|
private _changesetHandler: ChangesetHandler;
|
|
|
|
|
|
|
|
private _onLoggedIn : ((userDetails: UserDetails) => void)[] = [];
|
|
|
|
|
2020-08-08 21:17:17 +02:00
|
|
|
constructor(dryRun: boolean, oauth_token: UIEventSource<string>, singlePage: boolean = true) {
|
2020-07-29 21:32:51 +02:00
|
|
|
|
2020-07-31 01:45:54 +02:00
|
|
|
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-07-31 01:45:54 +02:00
|
|
|
|
|
|
|
|
2020-08-25 00:10:48 +02:00
|
|
|
if ( iframeMode || pwaStandAloneMode || !singlePage) {
|
2020-07-31 01:45:54 +02:00
|
|
|
// 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...
|
2020-07-31 01:45:54 +02:00
|
|
|
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-07-29 21:32:51 +02:00
|
|
|
|
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
this.userDetails = new UIEventSource<UserDetails>(new UserDetails());
|
2020-06-27 03:06:51 +02:00
|
|
|
this.userDetails.data.dryRun = dryRun;
|
2020-06-24 00:35:19 +02:00
|
|
|
this._dryRun = dryRun;
|
|
|
|
|
2020-08-26 15:36:04 +02:00
|
|
|
this._preferencesHandler = new OsmPreferences(this.auth, this);
|
|
|
|
|
|
|
|
this._changesetHandler = new ChangesetHandler(dryRun, this.userDetails, this.auth);
|
2020-07-31 01:45:54 +02:00
|
|
|
if (oauth_token.data !== undefined) {
|
2020-07-29 21:32:51 +02:00
|
|
|
console.log(oauth_token.data)
|
|
|
|
const self = this;
|
2020-08-26 15:36:04 +02:00
|
|
|
this.auth.bootstrapToken(oauth_token.data,
|
2020-07-29 21:32:51 +02:00
|
|
|
(x) => {
|
|
|
|
console.log("Called back: ", x)
|
|
|
|
self.AttemptLogin();
|
|
|
|
}, this.auth);
|
2020-08-26 15:36:04 +02:00
|
|
|
|
2020-07-29 21:32:51 +02:00
|
|
|
oauth_token.setData(undefined);
|
|
|
|
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
if (this.auth.authenticated()) {
|
|
|
|
this.AttemptLogin(); // Also updates the user badge
|
2020-07-29 21:32:51 +02:00
|
|
|
} else {
|
2020-06-24 00:35:19 +02:00
|
|
|
console.log("Not authenticated");
|
|
|
|
}
|
2020-08-26 15:36:04 +02:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2020-07-29 21:32:51 +02:00
|
|
|
|
2020-08-26 15:36:04 +02:00
|
|
|
public UploadChangeset(generateChangeXML: (csid: string) => string,
|
|
|
|
handleMapping: (idMapping: any) => void,
|
|
|
|
continuation: () => void) {
|
|
|
|
this._changesetHandler.UploadChangeset(generateChangeXML, handleMapping, continuation);
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2020-08-26 15:36:04 +02:00
|
|
|
public GetPreference(key: string, prefix: string = "mapcomplete-"): UIEventSource<string> {
|
|
|
|
return this._preferencesHandler.GetPreference(key, prefix);
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2020-08-26 15:36:04 +02:00
|
|
|
public GetLongPreference(key: string, prefix: string = "mapcomplete-"): UIEventSource<string> {
|
|
|
|
return this._preferencesHandler.GetLongPreference(key, prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
public OnLoggedIn(action: (userDetails: UserDetails) => void){
|
|
|
|
this._onLoggedIn.push(action);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-06-27 03:06:51 +02:00
|
|
|
if (details == null) {
|
2020-06-24 00:35:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-06-29 03:12:44 +02:00
|
|
|
|
2020-07-30 16:34:06 +02:00
|
|
|
self.CheckForMessagesContinuously();
|
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
// details is an XML DOM of user details
|
|
|
|
let userInfo = details.getElementsByTagName("user")[0];
|
|
|
|
|
2020-06-27 03:06:51 +02:00
|
|
|
// 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};
|
|
|
|
}
|
|
|
|
|
2020-06-27 03:06:51 +02:00
|
|
|
const messages = userInfo.getElementsByTagName("messages")[0].getElementsByTagName("received")[0];
|
|
|
|
data.unreadMessages = parseInt(messages.getAttribute("unread"));
|
|
|
|
data.totalMessages = parseInt(messages.getAttribute("count"));
|
2020-08-26 15:36:04 +02:00
|
|
|
|
|
|
|
for (const action of self._onLoggedIn) {
|
|
|
|
action(self.userDetails.data);
|
|
|
|
}
|
2020-07-30 16:34:06 +02:00
|
|
|
|
2020-08-26 15:36:04 +02:00
|
|
|
self.userDetails.ping();
|
2020-06-24 00:35:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:34:06 +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-24 00:35:19 +02:00
|
|
|
|
|
|
|
}
|