forked from MapComplete/MapComplete
		
	Add awareness of 'api/capabilities', see #880
This commit is contained in:
		
							parent
							
								
									b504547fe3
								
							
						
					
					
						commit
						29b0b2871b
					
				
					 1 changed files with 44 additions and 1 deletions
				
			
		| 
						 | 
					@ -28,6 +28,8 @@ export default class UserDetails {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export type OsmServiceState = "online" | "readonly" | "offline" | "unknown" | "unreachable"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class OsmConnection {
 | 
					export class OsmConnection {
 | 
				
			||||||
    public static readonly oauth_configs = {
 | 
					    public static readonly oauth_configs = {
 | 
				
			||||||
        osm: {
 | 
					        osm: {
 | 
				
			||||||
| 
						 | 
					@ -46,6 +48,13 @@ export class OsmConnection {
 | 
				
			||||||
    public auth
 | 
					    public auth
 | 
				
			||||||
    public userDetails: UIEventSource<UserDetails>
 | 
					    public userDetails: UIEventSource<UserDetails>
 | 
				
			||||||
    public isLoggedIn: Store<boolean>
 | 
					    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">(
 | 
					    public loadingStatus = new UIEventSource<"not-attempted" | "loading" | "error" | "logged-in">(
 | 
				
			||||||
        "not-attempted"
 | 
					        "not-attempted"
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
| 
						 | 
					@ -94,7 +103,13 @@ export class OsmConnection {
 | 
				
			||||||
            ud.totalMessages = 42
 | 
					            ud.totalMessages = 42
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        const self = this
 | 
					        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) => {
 | 
					        this.isLoggedIn.addCallback((isLoggedIn) => {
 | 
				
			||||||
            if (self.userDetails.data.loggedIn == false && isLoggedIn == true) {
 | 
					            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
 | 
					                // 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")
 | 
					        this.loadingStatus.setData("not-attempted")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * The backend host, without path or trailing '/'
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * new OsmConnection().Backend() // => "https://www.openstreetmap.org"
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    public Backend(): string {
 | 
					    public Backend(): string {
 | 
				
			||||||
        return this._oauth_config.url
 | 
					        return this._oauth_config.url
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public AttemptLogin() {
 | 
					    public AttemptLogin() {
 | 
				
			||||||
 | 
					        this.UpdateCapabilities()
 | 
				
			||||||
        this.loadingStatus.setData("loading")
 | 
					        this.loadingStatus.setData("loading")
 | 
				
			||||||
        if (this.fakeUser) {
 | 
					        if (this.fakeUser) {
 | 
				
			||||||
            this.loadingStatus.setData("logged-in")
 | 
					            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 }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue