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

View file

@ -2,6 +2,9 @@ import { UIEventSource } from "../UIEventSource"
import UserDetails, { OsmConnection } from "./OsmConnection" import UserDetails, { OsmConnection } from "./OsmConnection"
import { Utils } from "../../Utils" import { Utils } from "../../Utils"
import { LocalStorageSource } from "../Web/LocalStorageSource" import { LocalStorageSource } from "../Web/LocalStorageSource"
// @ts-ignore
import { osmAuth } from "osm-auth"
import OSMAuthInstance = OSMAuth.OSMAuthInstance
export class OsmPreferences { export class OsmPreferences {
/** /**
@ -17,16 +20,17 @@ export class OsmPreferences {
* @private * @private
*/ */
private readonly preferenceSources = new Map<string, UIEventSource<string>>() private readonly preferenceSources = new Map<string, UIEventSource<string>>()
private auth: any private readonly auth: OSMAuthInstance
private userDetails: UIEventSource<UserDetails> private userDetails: UIEventSource<UserDetails>
private longPreferences = {} private longPreferences = {}
private readonly _fakeUser: boolean
constructor(auth, osmConnection: OsmConnection) { constructor(auth: OSMAuthInstance, osmConnection: OsmConnection, fakeUser: boolean = false) {
this.auth = auth this.auth = auth
this._fakeUser = fakeUser
this.userDetails = osmConnection.userDetails this.userDetails = osmConnection.userDetails
const self = this
osmConnection.OnLoggedIn(() => { osmConnection.OnLoggedIn(() => {
self.UpdatePreferences(true) this.UpdatePreferences(true)
return 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) { private UpdatePreferences(forceUpdate?: boolean) {
const self = this const self = this
if (this._fakeUser) {
return
}
this.auth.xhr( this.auth.xhr(
{ {
method: "GET", method: "GET",
@ -272,13 +289,15 @@ export class OsmPreferences {
} }
const self = this const self = this
console.debug("Updating preference", k, " to ", Utils.EllipsesAfter(v, 15)) console.debug("Updating preference", k, " to ", Utils.EllipsesAfter(v, 15))
if (this._fakeUser) {
return
}
if (v === undefined || v === "") { if (v === undefined || v === "") {
this.auth.xhr( this.auth.xhr(
{ {
method: "DELETE", method: "DELETE",
path: "/api/0.6/user/preferences/" + encodeURIComponent(k), path: "/api/0.6/user/preferences/" + encodeURIComponent(k),
options: { header: { "Content-Type": "text/plain" } }, headers: { "Content-Type": "text/plain" },
}, },
function (error) { function (error) {
if (error) { if (error) {
@ -297,7 +316,7 @@ export class OsmPreferences {
{ {
method: "PUT", method: "PUT",
path: "/api/0.6/user/preferences/" + encodeURIComponent(k), path: "/api/0.6/user/preferences/" + encodeURIComponent(k),
options: { header: { "Content-Type": "text/plain" } }, headers: { "Content-Type": "text/plain" },
content: v, content: v,
}, },
function (error) { 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}> <If condition={state.featureSwitchIsTesting}>
<div class="alert w-fit">Testmode</div> <div class="alert w-fit">Testmode</div>
</If> </If>
<If condition={state.featureSwitches.featureSwitchFakeUser}>
<div class="alert w-fit">Faking a user (Testmode)</div>
</If>
</div> </div>
<div class="flex w-full flex-col items-center justify-center"> <div class="flex w-full flex-col items-center justify-center">
<!-- Flex and w-full are needed for the positioning --> <!-- Flex and w-full are needed for the positioning -->