diff --git a/src/Logic/Web/MangroveReviews.ts b/src/Logic/Web/MangroveReviews.ts index 0493e3afd..17c46e0a2 100644 --- a/src/Logic/Web/MangroveReviews.ts +++ b/src/Logic/Web/MangroveReviews.ts @@ -6,32 +6,30 @@ import { GeoOperations } from "../GeoOperations" import ScriptUtils from "../../../scripts/ScriptUtils" export class MangroveIdentity { - private readonly keypair: Store + private readonly keypair: UIEventSource = new UIEventSource(undefined) /** * Same as the one in the user settings */ public readonly mangroveIdentity: UIEventSource - private readonly key_id: Store + private readonly key_id: UIEventSource = new UIEventSource(undefined) private readonly _mangroveIdentityCreationDate: UIEventSource constructor(mangroveIdentity: UIEventSource, mangroveIdentityCreationDate: UIEventSource) { this.mangroveIdentity = mangroveIdentity this._mangroveIdentityCreationDate = mangroveIdentityCreationDate - const key_id = new UIEventSource(undefined) - this.key_id = key_id - const keypairEventSource = new UIEventSource(undefined) - this.keypair = keypairEventSource mangroveIdentity.addCallbackAndRunD(async (data) => { - if (!data) { - return - } - const keypair = await MangroveReviews.jwkToKeypair(JSON.parse(data)) - keypairEventSource.setData(keypair) - const pem = await MangroveReviews.publicToPem(keypair.publicKey) - key_id.setData(pem) + await this.setKeypair(data) }) } + private async setKeypair(data: string){ + console.log("Setting keypair from",data) + const keypair = await MangroveReviews.jwkToKeypair(JSON.parse(data)) + this.keypair.setData(keypair) + const pem = await MangroveReviews.publicToPem(keypair.publicKey) + this.key_id.setData(pem) + } + /** * Creates an identity if none exists already. * Is written into the UIEventsource, which was passed into the constructor @@ -44,7 +42,9 @@ export class MangroveIdentity { // Identity has been loaded via osmPreferences by now - we don't overwrite return } - console.log("Creating a new Mangrove identity!") + this.keypair.setData(keypair) + const pem = await MangroveReviews.publicToPem(keypair.publicKey) + this.key_id.setData(pem) this.mangroveIdentity.setData(JSON.stringify(jwk)) this._mangroveIdentityCreationDate.setData(new Date().toISOString()) } @@ -53,7 +53,7 @@ export class MangroveIdentity { * Only called to create a review. */ async getKeypair(): Promise { - if (this.keypair.data ?? "" === "") { + if (this.keypair.data === undefined) { // We want to create a review, but it seems like no key has been setup at this moment // We create the key try { @@ -79,10 +79,10 @@ export class MangroveIdentity { const all = this.getAllReviews() this.geoReviewsById = this.getAllReviews().mapD(reviews => reviews.filter( review => { - try{ - const subjectUrl = new URL(review.sub) - return subjectUrl.protocol === "geo:" - }catch (e) { + try { + const subjectUrl = new URL(review.sub) + return subjectUrl.protocol === "geo:" + } catch (e) { return false } } @@ -146,6 +146,7 @@ export default class FeatureReviews { private readonly _uncertainty: number private readonly _name: Store private readonly _identity: MangroveIdentity + private readonly _testmode: Store private constructor( feature: Feature, @@ -155,11 +156,13 @@ export default class FeatureReviews { nameKey?: "name" | string fallbackName?: string uncertaintyRadius?: number - } + }, + testmode?: Store ) { const centerLonLat = GeoOperations.centerpointCoordinates(feature) ;[this._lon, this._lat] = centerLonLat this._identity = mangroveIdentity + this._testmode = testmode ?? new ImmutableStore(false) const nameKey = options?.nameKey ?? "name" if (feature.geometry.type === "Point") { @@ -231,19 +234,20 @@ export default class FeatureReviews { public static construct( feature: Feature, tagsSource: UIEventSource>, - mangroveIdentity?: MangroveIdentity, - options?: { + mangroveIdentity: MangroveIdentity, + options: { nameKey?: "name" | string fallbackName?: string uncertaintyRadius?: number - } - ) { + }, + testmode: Store + ): FeatureReviews { const key = feature.properties.id const cached = FeatureReviews._featureReviewsCache[key] if (cached !== undefined) { return cached } - const featureReviews = new FeatureReviews(feature, tagsSource, mangroveIdentity, options) + const featureReviews = new FeatureReviews(feature, tagsSource, mangroveIdentity, options,testmode ) FeatureReviews._featureReviewsCache[key] = featureReviews return featureReviews } @@ -269,7 +273,12 @@ export default class FeatureReviews { const keypair: CryptoKeyPair = await this._identity.getKeypair() const jwt = await MangroveReviews.signReview(keypair, r) const kid = await MangroveReviews.publicToPem(keypair.publicKey) - await MangroveReviews.submitReview(jwt) + if (!this._testmode.data) { + await MangroveReviews.submitReview(jwt) + } else { + console.log("Testmode enabled - not uploading review") + await Utils.waitFor(1000) + } const reviewWithKid = { ...r, kid, diff --git a/src/UI/Reviews/ReviewForm.svelte b/src/UI/Reviews/ReviewForm.svelte index fbf73a82d..e01b4df55 100644 --- a/src/UI/Reviews/ReviewForm.svelte +++ b/src/UI/Reviews/ReviewForm.svelte @@ -61,17 +61,12 @@ opinion: opinion.data, metadata: { nickname, is_affiliated: isAffiliated.data }, } - if (state.featureSwitchIsTesting?.data ?? true) { - console.log("Testing - not actually saving review", review) - await Utils.waitFor(1000) - } else { try { await reviews.createReview(review) } catch (e) { console.error("Could not create review due to", e) uploadFailed = "" + e } - } _state = "done" } diff --git a/src/UI/SpecialVisualizations.ts b/src/UI/SpecialVisualizations.ts index c7ee4d3aa..754086cf1 100644 --- a/src/UI/SpecialVisualizations.ts +++ b/src/UI/SpecialVisualizations.ts @@ -667,7 +667,8 @@ export default class SpecialVisualizations { { nameKey: nameKey, fallbackName, - } + }, + state.featureSwitchIsTesting ) return new SvelteUIElement(StarsBarIcon, { score: reviews.average, @@ -700,7 +701,8 @@ export default class SpecialVisualizations { { nameKey: nameKey, fallbackName, - } + }, + state.featureSwitchIsTesting ) return new SvelteUIElement(ReviewForm, { reviews, state, tags, feature, layer }) }, @@ -732,7 +734,8 @@ export default class SpecialVisualizations { { nameKey: nameKey, fallbackName, - } + }, + state.featureSwitchIsTesting ) return new SvelteUIElement(AllReviews, { reviews, state, tags, feature, layer }) },