Repair 'fake user' functionality

This commit is contained in:
Pieter Vander Vennet 2024-03-11 00:01:44 +01:00
parent 39c079db45
commit e36e594b89
3 changed files with 34 additions and 24 deletions

View file

@ -91,9 +91,11 @@ export class OsmConnection {
ud.name = "Fake user"
ud.totalMessages = 42
ud.languages = ["en"]
this.loadingStatus.setData("logged-in")
}
const self = this
this.UpdateCapabilities()
this.isLoggedIn = this.userDetails.map(
(user) =>
user.loggedIn &&
@ -112,10 +114,7 @@ export class OsmConnection {
this.updateAuthObject()
this.preferencesHandler = new OsmPreferences(
this.auth,
<any /*This is needed to make the tests work*/>this
)
this.preferencesHandler = new OsmPreferences(this.auth, this, this.fakeUser)
if (options.oauth_token?.data !== undefined) {
console.log(options.oauth_token.data)
@ -554,13 +553,12 @@ export class OsmConnection {
}
private UpdateCapabilities(): void {
const self = this
if (this.fakeUser) {
return
}
this.FetchCapabilities().then(({ api, gpx }) => {
self.apiIsOnline.setData(api)
self.gpxServiceIsOnline.setData(gpx)
this.apiIsOnline.setData(api)
this.gpxServiceIsOnline.setData(gpx)
})
}

View file

@ -2,6 +2,9 @@ import { UIEventSource } from "../UIEventSource"
import UserDetails, { OsmConnection } from "./OsmConnection"
import { Utils } from "../../Utils"
import { LocalStorageSource } from "../Web/LocalStorageSource"
// @ts-ignore
import { osmAuth } from "osm-auth"
import OSMAuthInstance = OSMAuth.OSMAuthInstance
export class OsmPreferences {
/**
@ -17,16 +20,17 @@ export class OsmPreferences {
* @private
*/
private readonly preferenceSources = new Map<string, UIEventSource<string>>()
private auth: any
private readonly auth: OSMAuthInstance
private userDetails: UIEventSource<UserDetails>
private longPreferences = {}
private readonly _fakeUser: boolean
constructor(auth, osmConnection: OsmConnection) {
constructor(auth: OSMAuthInstance, osmConnection: OsmConnection, fakeUser: boolean = false) {
this.auth = auth
this._fakeUser = fakeUser
this.userDetails = osmConnection.userDetails
const self = this
osmConnection.OnLoggedIn(() => {
self.UpdatePreferences(true)
this.UpdatePreferences(true)
return true
})
}
@ -212,8 +216,21 @@ export class OsmPreferences {
})
}
removeAllWithPrefix(prefix: string) {
for (const key in this.preferences.data) {
if (key.startsWith(prefix)) {
this.GetPreference(key, "", { prefix: "" }).setData(undefined)
console.log("Clearing preference", key)
}
}
this.preferences.ping()
}
private UpdatePreferences(forceUpdate?: boolean) {
const self = this
if (this._fakeUser) {
return
}
this.auth.xhr(
{
method: "GET",
@ -272,13 +289,15 @@ export class OsmPreferences {
}
const self = this
console.debug("Updating preference", k, " to ", Utils.EllipsesAfter(v, 15))
if (this._fakeUser) {
return
}
if (v === undefined || v === "") {
this.auth.xhr(
{
method: "DELETE",
path: "/api/0.6/user/preferences/" + encodeURIComponent(k),
options: { header: { "Content-Type": "text/plain" } },
headers: { "Content-Type": "text/plain" },
},
function (error) {
if (error) {
@ -297,7 +316,7 @@ export class OsmPreferences {
{
method: "PUT",
path: "/api/0.6/user/preferences/" + encodeURIComponent(k),
options: { header: { "Content-Type": "text/plain" } },
headers: { "Content-Type": "text/plain" },
content: v,
},
function (error) {
@ -311,14 +330,4 @@ export class OsmPreferences {
}
)
}
removeAllWithPrefix(prefix: string) {
for (const key in this.preferences.data) {
if (key.startsWith(prefix)) {
this.GetPreference(key, "", { prefix: "" }).setData(undefined)
console.log("Clearing preference", key)
}
}
this.preferences.ping()
}
}

View file

@ -257,6 +257,9 @@
<If condition={state.featureSwitchIsTesting}>
<div class="alert w-fit">Testmode</div>
</If>
<If condition={state.featureSwitches.featureSwitchFakeUser}>
<div class="alert w-fit">Faking a user (Testmode)</div>
</If>
</div>
<div class="flex w-full flex-col items-center justify-center">
<!-- Flex and w-full are needed for the positioning -->