2020-06-24 00:35:19 +02:00
|
|
|
import osmAuth from "osm-auth"
|
2022-06-06 19:37:22 +02:00
|
|
|
import { Store, Stores, 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"
|
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"
|
2021-06-30 18:48:23 +02:00
|
|
|
import { OsmObject } from "./OsmObject"
|
2021-09-26 23:35:26 +02:00
|
|
|
import { Changes } from "./Changes"
|
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
|
|
|
home: { lon: number; lat: number }
|
2021-06-30 18:48:23 +02:00
|
|
|
public backend: string
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2021-06-30 18:48:23 +02:00
|
|
|
constructor(backend: string) {
|
|
|
|
this.backend = backend
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class OsmConnection {
|
2021-07-10 13:47:53 +02:00
|
|
|
public static readonly oauth_configs = {
|
2022-11-02 14:44:06 +01:00
|
|
|
osm: {
|
|
|
|
oauth_consumer_key: "hivV7ec2o49Two8g9h8Is1VIiVOgxQ1iYexCbvem",
|
|
|
|
oauth_secret: "wDBRTCem0vxD7txrg1y6p5r8nvmz8tAhET7zDASI",
|
|
|
|
url: "https://www.openstreetmap.org",
|
2022-02-16 00:56:48 +01:00
|
|
|
// OAUTH 1.0 application
|
|
|
|
// https://www.openstreetmap.org/user/Pieter%20Vander%20Vennet/oauth_clients/7404
|
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>
|
2022-06-06 19:37:22 +02:00
|
|
|
public isLoggedIn: Store<boolean>
|
2022-02-15 15:42:09 +01:00
|
|
|
public loadingStatus = new UIEventSource<"not-attempted" | "loading" | "error" | "logged-in">(
|
|
|
|
"not-attempted"
|
|
|
|
)
|
2020-08-27 00:08:00 +02:00
|
|
|
public preferencesHandler: OsmPreferences
|
2021-09-26 23:35:26 +02:00
|
|
|
public readonly _oauth_config: {
|
2021-06-08 16:52:31 +02:00
|
|
|
oauth_consumer_key: string
|
|
|
|
oauth_secret: string
|
|
|
|
url: string
|
|
|
|
}
|
2022-01-21 01:57:16 +01:00
|
|
|
private readonly _dryRun: UIEventSource<boolean>
|
2021-11-07 16:34:51 +01:00
|
|
|
private fakeUser: boolean
|
|
|
|
private _onLoggedIn: ((userDetails: UserDetails) => void)[] = []
|
|
|
|
private readonly _iframeMode: Boolean | boolean
|
|
|
|
private readonly _singlePage: boolean
|
2021-09-09 00:05:51 +02:00
|
|
|
private isChecking = false
|
2020-08-26 15:36:04 +02:00
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
constructor(options: {
|
2022-01-21 01:57:16 +01:00
|
|
|
dryRun?: UIEventSource<boolean>
|
2021-11-07 16:34:51 +01:00
|
|
|
fakeUser?: false | boolean
|
|
|
|
oauth_token?: UIEventSource<string>
|
|
|
|
// Used to keep multiple changesets open and to write to the correct changeset
|
|
|
|
singlePage?: boolean
|
2021-12-13 02:05:34 +01:00
|
|
|
osmConfiguration?: "osm" | "osm-test"
|
|
|
|
attemptLogin?: true | boolean
|
2021-06-08 16:52:31 +02:00
|
|
|
}) {
|
2021-09-28 17:30:48 +02:00
|
|
|
this.fakeUser = options.fakeUser ?? false
|
|
|
|
this._singlePage = options.singlePage ?? true
|
|
|
|
this._oauth_config =
|
|
|
|
OsmConnection.oauth_configs[options.osmConfiguration ?? "osm"] ??
|
|
|
|
OsmConnection.oauth_configs.osm
|
2021-06-08 16:52:31 +02:00
|
|
|
console.debug("Using backend", this._oauth_config.url)
|
2021-06-30 18:48:23 +02:00
|
|
|
OsmObject.SetBackendUrl(this._oauth_config.url + "/")
|
2021-06-08 16:52:31 +02:00
|
|
|
this._iframeMode = Utils.runningFromConsole ? false : window !== window.top
|
2020-07-29 21:32:51 +02:00
|
|
|
|
2021-06-30 18:48:23 +02:00
|
|
|
this.userDetails = new UIEventSource<UserDetails>(
|
|
|
|
new UserDetails(this._oauth_config.url),
|
|
|
|
"userDetails"
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2021-09-28 17:30:48 +02:00
|
|
|
if (options.fakeUser) {
|
2021-07-16 02:06:33 +02:00
|
|
|
const ud = this.userDetails.data
|
|
|
|
ud.csCount = 5678
|
2021-09-09 00:05:51 +02:00
|
|
|
ud.loggedIn = true
|
2021-07-16 02:06:33 +02:00
|
|
|
ud.unreadMessages = 0
|
|
|
|
ud.name = "Fake user"
|
|
|
|
ud.totalMessages = 42
|
|
|
|
}
|
2021-09-09 00:05:51 +02:00
|
|
|
const self = this
|
2022-06-06 19:37:22 +02:00
|
|
|
this.isLoggedIn = this.userDetails.map((user) => user.loggedIn)
|
|
|
|
this.isLoggedIn.addCallback((isLoggedIn) => {
|
2021-09-09 00:05:51 +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()
|
|
|
|
}
|
|
|
|
})
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2022-01-21 01:57:16 +01:00
|
|
|
this._dryRun = options.dryRun ?? new UIEventSource<boolean>(false)
|
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)
|
|
|
|
|
2021-09-28 17:30:48 +02:00
|
|
|
if (options.oauth_token?.data !== undefined) {
|
|
|
|
console.log(options.oauth_token.data)
|
2021-05-13 13:16:22 +02:00
|
|
|
const self = this
|
2021-09-28 17:30:48 +02:00
|
|
|
this.auth.bootstrapToken(
|
|
|
|
options.oauth_token.data,
|
2021-05-13 13:16:22 +02:00
|
|
|
(x) => {
|
|
|
|
console.log("Called back: ", x)
|
|
|
|
self.AttemptLogin()
|
|
|
|
},
|
|
|
|
this.auth
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2021-05-13 13:16:22 +02:00
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
options.oauth_token.setData(undefined)
|
2021-05-13 13:16:22 +02:00
|
|
|
}
|
2021-12-13 02:05:34 +01:00
|
|
|
if (this.auth.authenticated() && options.attemptLogin !== false) {
|
2021-05-13 13:16:22 +02:00
|
|
|
this.AttemptLogin() // Also updates the user badge
|
|
|
|
} else {
|
|
|
|
console.log("Not authenticated")
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2022-02-15 23:44:16 +01:00
|
|
|
public CreateChangesetHandler(allElements: ElementStorage, changes: Changes) {
|
|
|
|
return new ChangesetHandler(this._dryRun, this, allElements, changes, this.auth)
|
|
|
|
}
|
2020-07-29 21:32:51 +02:00
|
|
|
|
2022-06-03 01:33:41 +02:00
|
|
|
public GetPreference(
|
|
|
|
key: string,
|
|
|
|
defaultValue: string = undefined,
|
|
|
|
prefix: string = "mapcomplete-"
|
|
|
|
): UIEventSource<string> {
|
|
|
|
return this.preferencesHandler.GetPreference(key, defaultValue, 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")
|
2022-02-15 15:42:09 +01:00
|
|
|
this.loadingStatus.setData("not-attempted")
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2022-06-21 22:57:13 +02:00
|
|
|
public Backend(): string {
|
|
|
|
return this._oauth_config.url
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
public AttemptLogin() {
|
2022-02-15 15:42:09 +01:00
|
|
|
this.loadingStatus.setData("loading")
|
2021-09-09 00:05:51 +02:00
|
|
|
if (this.fakeUser) {
|
2022-02-15 15:42:09 +01:00
|
|
|
this.loadingStatus.setData("logged-in")
|
2021-07-16 02:06:33 +02:00
|
|
|
console.log("AttemptLogin called, but ignored as fakeUser is set")
|
|
|
|
return
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
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)
|
2022-02-15 15:42:09 +01:00
|
|
|
self.loadingStatus.setData("error")
|
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))
|
2022-09-08 21:40:48 +02:00
|
|
|
}
|
2021-04-23 15:20:24 +02:00
|
|
|
return
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2020-06-27 03:06:51 +02:00
|
|
|
if (details == null) {
|
2022-02-15 15:42:09 +01:00
|
|
|
self.loadingStatus.setData("error")
|
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-09-09 00:05:51 +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 }
|
|
|
|
}
|
|
|
|
|
2022-02-15 15:42:09 +01:00
|
|
|
self.loadingStatus.setData("logged-in")
|
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)
|
2022-09-08 21:40:48 +02:00
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
self._onLoggedIn = []
|
2020-08-26 15:36:04 +02:00
|
|
|
}
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 00:29:51 +02:00
|
|
|
public closeNote(id: number | string, text?: string): Promise<void> {
|
2022-01-08 04:22:50 +01:00
|
|
|
let textSuffix = ""
|
2022-01-08 14:08:04 +01:00
|
|
|
if ((text ?? "") !== "") {
|
|
|
|
textSuffix = "?text=" + encodeURIComponent(text)
|
2022-01-08 04:22:50 +01:00
|
|
|
}
|
2022-01-21 01:57:16 +01:00
|
|
|
if (this._dryRun.data) {
|
2022-01-12 02:31:51 +01:00
|
|
|
console.warn("Dryrun enabled - not actually closing note ", id, " with text ", text)
|
2022-06-05 02:24:14 +02:00
|
|
|
return new Promise((ok) => {
|
2022-01-12 02:31:51 +01:00
|
|
|
ok()
|
|
|
|
})
|
|
|
|
}
|
2022-01-08 04:22:50 +01:00
|
|
|
return new Promise((ok, error) => {
|
|
|
|
this.auth.xhr(
|
|
|
|
{
|
|
|
|
method: "POST",
|
2022-01-14 01:41:19 +01:00
|
|
|
path: `/api/0.6/notes/${id}/close${textSuffix}`,
|
2022-06-05 02:24:14 +02:00
|
|
|
},
|
|
|
|
function (err, _) {
|
2022-01-08 04:22:50 +01:00
|
|
|
if (err !== null) {
|
|
|
|
error(err)
|
|
|
|
} else {
|
|
|
|
ok()
|
2022-09-08 21:40:48 +02:00
|
|
|
}
|
2022-01-08 04:22:50 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-28 00:29:51 +02:00
|
|
|
public reopenNote(id: number | string, text?: string): Promise<void> {
|
2022-01-21 01:57:16 +01:00
|
|
|
if (this._dryRun.data) {
|
2022-01-12 02:31:51 +01:00
|
|
|
console.warn("Dryrun enabled - not actually reopening note ", id, " with text ", text)
|
2022-06-05 02:24:14 +02:00
|
|
|
return new Promise((ok) => {
|
2022-01-12 02:31:51 +01:00
|
|
|
ok()
|
|
|
|
})
|
|
|
|
}
|
2022-01-08 04:22:50 +01:00
|
|
|
let textSuffix = ""
|
2022-01-08 14:08:04 +01:00
|
|
|
if ((text ?? "") !== "") {
|
|
|
|
textSuffix = "?text=" + encodeURIComponent(text)
|
2022-01-08 04:22:50 +01:00
|
|
|
}
|
|
|
|
return new Promise((ok, error) => {
|
|
|
|
this.auth.xhr(
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
path: `/api/0.6/notes/${id}/reopen${textSuffix}`,
|
2022-06-05 02:24:14 +02:00
|
|
|
},
|
|
|
|
function (err, _) {
|
2022-01-08 04:22:50 +01:00
|
|
|
if (err !== null) {
|
|
|
|
error(err)
|
|
|
|
} else {
|
|
|
|
ok()
|
2022-09-08 21:40:48 +02:00
|
|
|
}
|
2022-01-08 04:22:50 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-14 01:41:19 +01:00
|
|
|
public openNote(lat: number, lon: number, text: string): Promise<{ id: number }> {
|
2022-01-21 01:57:16 +01:00
|
|
|
if (this._dryRun.data) {
|
2022-01-14 01:41:19 +01:00
|
|
|
console.warn("Dryrun enabled - not actually opening note with text ", text)
|
2022-06-05 02:24:14 +02:00
|
|
|
return new Promise<{ id: number }>((ok) => {
|
2022-01-21 01:57:16 +01:00
|
|
|
window.setTimeout(
|
|
|
|
() => ok({ id: Math.floor(Math.random() * 1000) }),
|
|
|
|
Math.random() * 5000
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-01-14 01:41:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
const auth = this.auth
|
|
|
|
const content = { lat, lon, text }
|
|
|
|
return new Promise((ok, error) => {
|
|
|
|
auth.xhr(
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
path: `/api/0.6/notes.json`,
|
2022-01-21 01:57:16 +01:00
|
|
|
options: {
|
|
|
|
header: { "Content-Type": "application/json" },
|
2022-09-08 21:40:48 +02:00
|
|
|
},
|
2022-01-14 01:41:19 +01:00
|
|
|
content: JSON.stringify(content),
|
2022-01-21 01:57:16 +01:00
|
|
|
},
|
2022-01-25 21:55:51 +01:00
|
|
|
function (err, response: string) {
|
|
|
|
console.log("RESPONSE IS", response)
|
2022-01-14 01:41:19 +01:00
|
|
|
if (err !== null) {
|
|
|
|
error(err)
|
|
|
|
} else {
|
2022-01-25 21:55:51 +01:00
|
|
|
const parsed = JSON.parse(response)
|
|
|
|
const id = parsed.properties.id
|
2022-01-14 01:41:19 +01:00
|
|
|
console.log("OPENED NOTE", id)
|
|
|
|
ok({ id })
|
2022-09-08 21:40:48 +02:00
|
|
|
}
|
2022-01-14 01:41:19 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2022-11-02 14:44:06 +01:00
|
|
|
public async uploadGpxTrack(
|
|
|
|
gpx: string,
|
|
|
|
options: {
|
|
|
|
description: string
|
|
|
|
visibility: "private" | "public" | "trackable" | "identifiable"
|
|
|
|
filename?: string
|
|
|
|
/**
|
|
|
|
* Some words to give some properties;
|
|
|
|
*
|
|
|
|
* Note: these are called 'tags' on the wiki, but I opted to name them 'labels' instead as they aren't "key=value" tags, but just words.
|
|
|
|
*/
|
|
|
|
labels: string[]
|
|
|
|
}
|
|
|
|
): Promise<{ id: number }> {
|
2022-02-16 00:56:48 +01:00
|
|
|
if (this._dryRun.data) {
|
|
|
|
console.warn("Dryrun enabled - not actually uploading GPX ", gpx)
|
|
|
|
return new Promise<{ id: number }>((ok, error) => {
|
2022-11-02 14:44:06 +01:00
|
|
|
window.setTimeout(
|
|
|
|
() => ok({ id: Math.floor(Math.random() * 1000) }),
|
|
|
|
Math.random() * 5000
|
|
|
|
)
|
|
|
|
})
|
2022-02-16 00:56:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const contents = {
|
2022-11-02 14:44:06 +01:00
|
|
|
file: gpx,
|
|
|
|
description: options.description ?? "",
|
|
|
|
tags: options.labels?.join(",") ?? "",
|
|
|
|
visibility: options.visibility,
|
2022-02-16 00:56:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const extras = {
|
2022-11-02 14:44:06 +01:00
|
|
|
file:
|
|
|
|
'; filename="' +
|
|
|
|
(options.filename ?? "gpx_track_mapcomplete_" + new Date().toISOString()) +
|
|
|
|
'"\r\nContent-Type: application/gpx+xml',
|
2022-02-16 00:56:48 +01:00
|
|
|
}
|
2022-10-28 04:33:05 +02:00
|
|
|
|
2022-11-02 14:44:06 +01:00
|
|
|
const auth = this.auth
|
|
|
|
const boundary = "987654"
|
2022-02-16 00:56:48 +01:00
|
|
|
|
2022-08-05 12:39:02 +02:00
|
|
|
let body = ""
|
|
|
|
for (const key in contents) {
|
2022-02-16 00:56:48 +01:00
|
|
|
body += "--" + boundary + "\r\n"
|
2022-11-02 14:44:06 +01:00
|
|
|
body += 'Content-Disposition: form-data; name="' + key + '"'
|
|
|
|
if (extras[key] !== undefined) {
|
2022-02-16 00:56:48 +01:00
|
|
|
body += extras[key]
|
|
|
|
}
|
|
|
|
body += "\r\n\r\n"
|
|
|
|
body += contents[key] + "\r\n"
|
|
|
|
}
|
|
|
|
body += "--" + boundary + "--\r\n"
|
|
|
|
|
|
|
|
return new Promise((ok, error) => {
|
2022-11-02 14:44:06 +01:00
|
|
|
auth.xhr(
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
path: `/api/0.6/gpx/create`,
|
|
|
|
options: {
|
|
|
|
header: {
|
2022-02-16 00:56:48 +01:00
|
|
|
"Content-Type": "multipart/form-data; boundary=" + boundary,
|
2022-11-02 14:44:06 +01:00
|
|
|
"Content-Length": body.length,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
content: body,
|
2022-02-16 00:56:48 +01:00
|
|
|
},
|
2022-11-02 14:44:06 +01:00
|
|
|
function (err, response: string) {
|
|
|
|
console.log("RESPONSE IS", response)
|
|
|
|
if (err !== null) {
|
|
|
|
error(err)
|
|
|
|
} else {
|
|
|
|
const parsed = JSON.parse(response)
|
|
|
|
console.log("Uploaded GPX track", parsed)
|
|
|
|
ok({ id: parsed })
|
|
|
|
}
|
2022-02-16 00:56:48 +01:00
|
|
|
}
|
2022-11-02 14:44:06 +01:00
|
|
|
)
|
2022-02-16 00:56:48 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-20 11:26:55 +02:00
|
|
|
public addCommentToNote(id: number | string, text: string): Promise<void> {
|
2022-01-21 01:57:16 +01:00
|
|
|
if (this._dryRun.data) {
|
2022-01-14 01:41:19 +01:00
|
|
|
console.warn("Dryrun enabled - not actually adding comment ", text, "to note ", id)
|
2022-06-05 02:24:14 +02:00
|
|
|
return new Promise((ok) => {
|
2022-01-12 02:31:51 +01:00
|
|
|
ok()
|
|
|
|
})
|
|
|
|
}
|
2022-01-08 04:22:50 +01:00
|
|
|
if ((text ?? "") === "") {
|
|
|
|
throw "Invalid text!"
|
|
|
|
}
|
|
|
|
|
2022-01-07 04:14:53 +01:00
|
|
|
return new Promise((ok, error) => {
|
|
|
|
this.auth.xhr(
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
|
2022-01-31 13:14:07 +01:00
|
|
|
path: `/api/0.6/notes/${id}/comment?text=${encodeURIComponent(text)}`,
|
2022-09-08 21:40:48 +02:00
|
|
|
},
|
2022-06-05 02:24:14 +02:00
|
|
|
function (err, _) {
|
2022-01-07 04:14:53 +01:00
|
|
|
if (err !== null) {
|
|
|
|
error(err)
|
2022-09-08 21:40:48 +02:00
|
|
|
} else {
|
|
|
|
ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2022-01-07 04:14:53 +01: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-09-09 00:05:51 +02:00
|
|
|
private CheckForMessagesContinuously() {
|
|
|
|
const self = this
|
|
|
|
if (this.isChecking) {
|
2021-07-27 19:35:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
this.isChecking = true
|
2022-06-05 02:24:14 +02:00
|
|
|
Stores.Chronic(5 * 60 * 1000).addCallback((_) => {
|
2021-09-09 00:05:51 +02:00
|
|
|
if (self.isLoggedIn.data) {
|
|
|
|
console.log("Checking for messages")
|
|
|
|
self.AttemptLogin()
|
|
|
|
}
|
2021-06-14 02:39:23 +02:00
|
|
|
})
|
2020-07-30 16:34:06 +02:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|