Add awareness of 'api/capabilities', see #880

This commit is contained in:
Pieter Vander Vennet 2023-01-06 03:30:18 +01:00
parent b504547fe3
commit 29b0b2871b

View file

@ -28,6 +28,8 @@ export default class UserDetails {
}
}
export type OsmServiceState = "online" | "readonly" | "offline" | "unknown" | "unreachable"
export class OsmConnection {
public static readonly oauth_configs = {
osm: {
@ -46,6 +48,13 @@ export class OsmConnection {
public auth
public userDetails: UIEventSource<UserDetails>
public isLoggedIn: Store<boolean>
public gpxServiceIsOnline: UIEventSource<OsmServiceState> = new UIEventSource<OsmServiceState>(
"unknown"
)
public apiIsOnline: UIEventSource<OsmServiceState> = new UIEventSource<OsmServiceState>(
"unknown"
)
public loadingStatus = new UIEventSource<"not-attempted" | "loading" | "error" | "logged-in">(
"not-attempted"
)
@ -94,7 +103,13 @@ export class OsmConnection {
ud.totalMessages = 42
}
const self = this
this.isLoggedIn = this.userDetails.map((user) => user.loggedIn)
this.UpdateCapabilities()
this.isLoggedIn = this.userDetails.map(
(user) =>
user.loggedIn &&
(self.apiIsOnline.data === "unknown" || self.apiIsOnline.data === "online"),
[this.apiIsOnline]
)
this.isLoggedIn.addCallback((isLoggedIn) => {
if (self.userDetails.data.loggedIn == false && isLoggedIn == true) {
// We have an inconsistency: the userdetails say we _didn't_ log in, but this actor says we do
@ -160,11 +175,17 @@ export class OsmConnection {
this.loadingStatus.setData("not-attempted")
}
/**
* The backend host, without path or trailing '/'
*
* new OsmConnection().Backend() // => "https://www.openstreetmap.org"
*/
public Backend(): string {
return this._oauth_config.url
}
public AttemptLogin() {
this.UpdateCapabilities()
this.loadingStatus.setData("loading")
if (this.fakeUser) {
this.loadingStatus.setData("logged-in")
@ -504,4 +525,26 @@ export class OsmConnection {
}
})
}
private UpdateCapabilities(): void {
const self = this
this.FetchCapabilities().then(({ api, gpx }) => {
self.apiIsOnline.setData(api)
self.gpxServiceIsOnline.setData(gpx)
})
}
private async FetchCapabilities(): Promise<{ api: OsmServiceState; gpx: OsmServiceState }> {
const result = await Utils.downloadAdvanced(this.Backend() + "/api/0.6/capabilities")
if (result["content"] === undefined) {
console.log("Something went wrong:", result)
return { api: "unreachable", gpx: "unreachable" }
}
const xmlRaw = result["content"]
const parsed = new DOMParser().parseFromString(xmlRaw, "text/xml")
const statusEl = parsed.getElementsByTagName("status")[0]
const api = <OsmServiceState>statusEl.getAttribute("api")
const gpx = <OsmServiceState>statusEl.getAttribute("gpx")
return { api, gpx }
}
}