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-26 15:36:04 +02:00
|
|
|
import {OsmPreferences} from "./OsmPreferences";
|
|
|
|
import {ChangesetHandler} from "./ChangesetHandler";
|
2020-08-30 01:13:18 +02:00
|
|
|
import {ElementStorage} from "../ElementStorage";
|
2020-11-06 04:02:53 +01:00
|
|
|
import Svg from "../../Svg";
|
2020-11-11 16:23:49 +01:00
|
|
|
import LayoutConfig from "../../Customizations/JSON/LayoutConfig";
|
2021-01-03 00:19:42 +01:00
|
|
|
import Img from "../../UI/Base/Img";
|
2021-06-08 16:52:31 +02:00
|
|
|
import {Utils} from "../../Utils";
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2021-01-04 04:06:21 +01:00
|
|
|
export default class UserDetails {
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
public loggedIn = false;
|
|
|
|
public name = "Not logged in";
|
2021-06-28 18:06:54 +02:00
|
|
|
public uid: number;
|
2020-06-24 00:35:19 +02:00
|
|
|
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 {
|
2021-01-04 04:06:21 +01:00
|
|
|
|
2021-06-08 16:52:31 +02:00
|
|
|
public static readonly _oauth_configs = {
|
|
|
|
"osm": {
|
|
|
|
oauth_consumer_key: 'hivV7ec2o49Two8g9h8Is1VIiVOgxQ1iYexCbvem',
|
|
|
|
oauth_secret: 'wDBRTCem0vxD7txrg1y6p5r8nvmz8tAhET7zDASI',
|
2021-06-08 17:13:25 +02:00
|
|
|
url: "https://www.openstreetmap.org"
|
2021-06-08 16:52:31 +02:00
|
|
|
},
|
|
|
|
"osm-test": {
|
|
|
|
oauth_consumer_key: 'Zgr7EoKb93uwPv2EOFkIlf3n9NLwj5wbyfjZMhz2',
|
|
|
|
oauth_secret: '3am1i1sykHDMZ66SGq4wI2Z7cJMKgzneCHp3nctn',
|
|
|
|
url: "https://master.apis.dev.openstreetmap.org"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2020-07-29 21:32:51 +02:00
|
|
|
public auth;
|
2020-06-24 00:35:19 +02:00
|
|
|
public userDetails: UIEventSource<UserDetails>;
|
2021-06-12 02:58:32 +02:00
|
|
|
public isLoggedIn: UIEventSource<boolean>
|
2020-12-08 23:44:34 +01:00
|
|
|
_dryRun: boolean;
|
2020-08-27 00:08:00 +02:00
|
|
|
public preferencesHandler: OsmPreferences;
|
|
|
|
public changesetHandler: ChangesetHandler;
|
2021-01-04 04:06:21 +01:00
|
|
|
private _onLoggedIn: ((userDetails: UserDetails) => void)[] = [];
|
2021-05-13 13:16:22 +02:00
|
|
|
private readonly _iframeMode: Boolean | boolean;
|
|
|
|
private readonly _singlePage: boolean;
|
2021-06-08 16:52:31 +02:00
|
|
|
private readonly _oauth_config: {
|
|
|
|
oauth_consumer_key: string,
|
|
|
|
oauth_secret: string,
|
|
|
|
url: string
|
|
|
|
};
|
2020-08-26 15:36:04 +02:00
|
|
|
|
2020-08-27 18:44:16 +02:00
|
|
|
constructor(dryRun: boolean, oauth_token: UIEventSource<string>,
|
|
|
|
// Used to keep multiple changesets open and to write to the correct changeset
|
|
|
|
layoutName: string,
|
2021-06-08 16:52:31 +02:00
|
|
|
singlePage: boolean = true,
|
|
|
|
osmConfiguration: "osm" | "osm-test" = 'osm'
|
|
|
|
) {
|
2021-05-13 13:16:22 +02:00
|
|
|
this._singlePage = singlePage;
|
2021-06-08 16:52:31 +02:00
|
|
|
this._oauth_config = OsmConnection._oauth_configs[osmConfiguration] ?? OsmConnection._oauth_configs.osm;
|
|
|
|
console.debug("Using backend", this._oauth_config.url)
|
|
|
|
this._iframeMode = Utils.runningFromConsole ? false : window !== window.top;
|
2020-07-29 21:32:51 +02:00
|
|
|
|
2021-05-13 13:16:22 +02:00
|
|
|
this.userDetails = new UIEventSource<UserDetails>(new UserDetails(), "userDetails");
|
|
|
|
this.userDetails.data.dryRun = dryRun;
|
2021-06-14 02:39:23 +02:00
|
|
|
const self =this;
|
|
|
|
this.isLoggedIn = this.userDetails.map(user => user.loggedIn).addCallback(isLoggedIn => {
|
2021-06-14 17:42:26 +02:00
|
|
|
if(self.userDetails.data.loggedIn == false && isLoggedIn == true){
|
2021-06-14 02:39:23 +02:00
|
|
|
// We have an inconsistency: the userdetails say we _didn't_ log in, but this actor says we do
|
|
|
|
// This means someone attempted to toggle this; so we attempt to login!
|
|
|
|
self.AttemptLogin()
|
|
|
|
}
|
|
|
|
});
|
2021-05-13 13:16:22 +02:00
|
|
|
this._dryRun = dryRun;
|
2021-06-08 16:52:31 +02:00
|
|
|
|
2021-05-13 13:16:22 +02:00
|
|
|
this.updateAuthObject();
|
|
|
|
|
|
|
|
this.preferencesHandler = new OsmPreferences(this.auth, this);
|
|
|
|
|
|
|
|
this.changesetHandler = new ChangesetHandler(layoutName, dryRun, this, this.auth);
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
if (this.auth.authenticated()) {
|
|
|
|
this.AttemptLogin(); // Also updates the user badge
|
|
|
|
} else {
|
|
|
|
console.log("Not authenticated");
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 21:32:51 +02:00
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
public UploadChangeset(
|
2020-11-11 16:23:49 +01:00
|
|
|
layout: LayoutConfig,
|
2020-08-30 01:13:18 +02:00
|
|
|
allElements: ElementStorage,
|
|
|
|
generateChangeXML: (csid: string) => string,
|
2021-01-04 04:06:21 +01:00
|
|
|
continuation: () => void = () => {
|
|
|
|
}) {
|
2020-08-30 01:13:18 +02:00
|
|
|
this.changesetHandler.UploadChangeset(layout, allElements, generateChangeXML, continuation);
|
2020-08-26 15:36:04 +02:00
|
|
|
}
|
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> {
|
2020-08-27 00:08:00 +02:00
|
|
|
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> {
|
2020-08-27 00:08:00 +02:00
|
|
|
return this.preferencesHandler.GetLongPreference(key, prefix);
|
2020-08-26 15:36:04 +02:00
|
|
|
}
|
|
|
|
|
2021-01-04 04:06:21 +01:00
|
|
|
public OnLoggedIn(action: (userDetails: UserDetails) => void) {
|
2020-08-26 15:36:04 +02:00
|
|
|
this._onLoggedIn.push(action);
|
|
|
|
}
|
2021-01-04 04:06:21 +01:00
|
|
|
|
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;
|
2020-09-05 01:40:43 +02:00
|
|
|
this.userDetails.data.csCount = 0;
|
|
|
|
this.userDetails.data.name = "";
|
2020-07-01 17:51:55 +02:00
|
|
|
this.userDetails.ping();
|
|
|
|
console.log("Logged out")
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public AttemptLogin() {
|
|
|
|
const self = this;
|
2020-08-27 18:44:16 +02:00
|
|
|
console.log("Trying to log in...");
|
2021-05-13 13:16:22 +02:00
|
|
|
this.updateAuthObject();
|
2020-06-24 00:35:19 +02:00
|
|
|
this.auth.xhr({
|
|
|
|
method: 'GET',
|
|
|
|
path: '/api/0.6/user/details'
|
|
|
|
}, function (err, details) {
|
2021-01-04 04:06:21 +01:00
|
|
|
if (err != null) {
|
2020-06-24 00:35:19 +02:00
|
|
|
console.log(err);
|
2021-04-23 15:20:24 +02:00
|
|
|
if (err.status == 401) {
|
|
|
|
console.log("Clearing tokens...")
|
|
|
|
// Not authorized - our token probably got revoked
|
|
|
|
// Reset all the tokens
|
|
|
|
const tokens = [
|
2021-06-08 16:52:31 +02:00
|
|
|
"https://www.openstreetmap.orgoauth_request_token_secret",
|
2021-04-23 15:20:24 +02:00
|
|
|
"https://www.openstreetmap.orgoauth_token",
|
|
|
|
"https://www.openstreetmap.orgoauth_token_secret"]
|
|
|
|
tokens.forEach(token => localStorage.removeItem(token))
|
|
|
|
}
|
2020-08-28 03:16:21 +02:00
|
|
|
return;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 03:06:51 +02:00
|
|
|
if (details == null) {
|
2020-06-24 00:35:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-01-04 04:06:21 +01:00
|
|
|
|
2020-07-30 16:34:06 +02:00
|
|
|
self.CheckForMessagesContinuously();
|
2021-01-04 04:06:21 +01:00
|
|
|
|
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');
|
2021-06-28 18:06:54 +02:00
|
|
|
data.uid= Number(userInfo.getAttribute("id"))
|
2020-06-24 00:35:19 +02:00
|
|
|
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");
|
|
|
|
}
|
2020-11-06 04:02:53 +01:00
|
|
|
data.img = data.img ?? Img.AsData(Svg.osm_logo);
|
2020-06-28 23:33:48 +02:00
|
|
|
|
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
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
self.userDetails.ping();
|
2020-08-26 15:36:04 +02:00
|
|
|
for (const action of self._onLoggedIn) {
|
|
|
|
action(self.userDetails.data);
|
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
self._onLoggedIn = [];
|
2021-01-04 04:06:21 +01:00
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-08 16:52:31 +02:00
|
|
|
private updateAuthObject() {
|
|
|
|
let pwaStandAloneMode = false;
|
|
|
|
try {
|
|
|
|
if (Utils.runningFromConsole) {
|
|
|
|
pwaStandAloneMode = true
|
|
|
|
} else 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")
|
|
|
|
}
|
|
|
|
const standalone = this._iframeMode || pwaStandAloneMode || !this._singlePage;
|
|
|
|
|
|
|
|
// In standalone mode, we DON'T use single page login, as 'redirecting' opens a new window anyway...
|
|
|
|
// Same for an iframe...
|
|
|
|
|
|
|
|
|
|
|
|
this.auth = new osmAuth({
|
|
|
|
oauth_consumer_key: this._oauth_config.oauth_consumer_key,
|
|
|
|
oauth_secret: this._oauth_config.oauth_secret,
|
|
|
|
url: this._oauth_config.url,
|
|
|
|
landing: standalone ? undefined : window.location.href,
|
|
|
|
singlepage: !standalone,
|
|
|
|
auto: true,
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2020-07-30 16:34:06 +02:00
|
|
|
|
2021-06-14 02:39:23 +02:00
|
|
|
private CheckForMessagesContinuously(){
|
|
|
|
const self =this;
|
|
|
|
UIEventSource.Chronic(5 * 60 * 1000).addCallback(_ => {
|
|
|
|
if (self.isLoggedIn .data) {
|
|
|
|
console.log("Checking for messages")
|
|
|
|
self.AttemptLogin();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-30 16:34:06 +02:00
|
|
|
}
|
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
}
|