Fix: fix anonymous reviews; fix #1868

This commit is contained in:
Pieter Vander Vennet 2024-04-02 13:32:19 +02:00
parent 6950bda35e
commit 073cd692e4
3 changed files with 41 additions and 34 deletions

View file

@ -6,32 +6,30 @@ import { GeoOperations } from "../GeoOperations"
import ScriptUtils from "../../../scripts/ScriptUtils" import ScriptUtils from "../../../scripts/ScriptUtils"
export class MangroveIdentity { export class MangroveIdentity {
private readonly keypair: Store<CryptoKeyPair> private readonly keypair: UIEventSource<CryptoKeyPair> = new UIEventSource<CryptoKeyPair>(undefined)
/** /**
* Same as the one in the user settings * Same as the one in the user settings
*/ */
public readonly mangroveIdentity: UIEventSource<string> public readonly mangroveIdentity: UIEventSource<string>
private readonly key_id: Store<string> private readonly key_id: UIEventSource<string> = new UIEventSource<string>(undefined)
private readonly _mangroveIdentityCreationDate: UIEventSource<string> private readonly _mangroveIdentityCreationDate: UIEventSource<string>
constructor(mangroveIdentity: UIEventSource<string>, mangroveIdentityCreationDate: UIEventSource<string>) { constructor(mangroveIdentity: UIEventSource<string>, mangroveIdentityCreationDate: UIEventSource<string>) {
this.mangroveIdentity = mangroveIdentity this.mangroveIdentity = mangroveIdentity
this._mangroveIdentityCreationDate = mangroveIdentityCreationDate this._mangroveIdentityCreationDate = mangroveIdentityCreationDate
const key_id = new UIEventSource<string>(undefined)
this.key_id = key_id
const keypairEventSource = new UIEventSource<CryptoKeyPair>(undefined)
this.keypair = keypairEventSource
mangroveIdentity.addCallbackAndRunD(async (data) => { mangroveIdentity.addCallbackAndRunD(async (data) => {
if (!data) { await this.setKeypair(data)
return
}
const keypair = await MangroveReviews.jwkToKeypair(JSON.parse(data))
keypairEventSource.setData(keypair)
const pem = await MangroveReviews.publicToPem(keypair.publicKey)
key_id.setData(pem)
}) })
} }
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. * Creates an identity if none exists already.
* Is written into the UIEventsource, which was passed into the constructor * 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 // Identity has been loaded via osmPreferences by now - we don't overwrite
return 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.mangroveIdentity.setData(JSON.stringify(jwk))
this._mangroveIdentityCreationDate.setData(new Date().toISOString()) this._mangroveIdentityCreationDate.setData(new Date().toISOString())
} }
@ -53,7 +53,7 @@ export class MangroveIdentity {
* Only called to create a review. * Only called to create a review.
*/ */
async getKeypair(): Promise<CryptoKeyPair> { async getKeypair(): Promise<CryptoKeyPair> {
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 want to create a review, but it seems like no key has been setup at this moment
// We create the key // We create the key
try { try {
@ -79,10 +79,10 @@ export class MangroveIdentity {
const all = this.getAllReviews() const all = this.getAllReviews()
this.geoReviewsById = this.getAllReviews().mapD(reviews => reviews.filter( this.geoReviewsById = this.getAllReviews().mapD(reviews => reviews.filter(
review => { review => {
try{ try {
const subjectUrl = new URL(review.sub) const subjectUrl = new URL(review.sub)
return subjectUrl.protocol === "geo:" return subjectUrl.protocol === "geo:"
}catch (e) { } catch (e) {
return false return false
} }
} }
@ -146,6 +146,7 @@ export default class FeatureReviews {
private readonly _uncertainty: number private readonly _uncertainty: number
private readonly _name: Store<string> private readonly _name: Store<string>
private readonly _identity: MangroveIdentity private readonly _identity: MangroveIdentity
private readonly _testmode: Store<boolean>
private constructor( private constructor(
feature: Feature, feature: Feature,
@ -155,11 +156,13 @@ export default class FeatureReviews {
nameKey?: "name" | string nameKey?: "name" | string
fallbackName?: string fallbackName?: string
uncertaintyRadius?: number uncertaintyRadius?: number
} },
testmode?: Store<boolean>
) { ) {
const centerLonLat = GeoOperations.centerpointCoordinates(feature) const centerLonLat = GeoOperations.centerpointCoordinates(feature)
;[this._lon, this._lat] = centerLonLat ;[this._lon, this._lat] = centerLonLat
this._identity = mangroveIdentity this._identity = mangroveIdentity
this._testmode = testmode ?? new ImmutableStore(false)
const nameKey = options?.nameKey ?? "name" const nameKey = options?.nameKey ?? "name"
if (feature.geometry.type === "Point") { if (feature.geometry.type === "Point") {
@ -231,19 +234,20 @@ export default class FeatureReviews {
public static construct( public static construct(
feature: Feature, feature: Feature,
tagsSource: UIEventSource<Record<string, string>>, tagsSource: UIEventSource<Record<string, string>>,
mangroveIdentity?: MangroveIdentity, mangroveIdentity: MangroveIdentity,
options?: { options: {
nameKey?: "name" | string nameKey?: "name" | string
fallbackName?: string fallbackName?: string
uncertaintyRadius?: number uncertaintyRadius?: number
} },
) { testmode: Store<boolean>
): FeatureReviews {
const key = feature.properties.id const key = feature.properties.id
const cached = FeatureReviews._featureReviewsCache[key] const cached = FeatureReviews._featureReviewsCache[key]
if (cached !== undefined) { if (cached !== undefined) {
return cached return cached
} }
const featureReviews = new FeatureReviews(feature, tagsSource, mangroveIdentity, options) const featureReviews = new FeatureReviews(feature, tagsSource, mangroveIdentity, options,testmode )
FeatureReviews._featureReviewsCache[key] = featureReviews FeatureReviews._featureReviewsCache[key] = featureReviews
return featureReviews return featureReviews
} }
@ -269,7 +273,12 @@ export default class FeatureReviews {
const keypair: CryptoKeyPair = await this._identity.getKeypair() const keypair: CryptoKeyPair = await this._identity.getKeypair()
const jwt = await MangroveReviews.signReview(keypair, r) const jwt = await MangroveReviews.signReview(keypair, r)
const kid = await MangroveReviews.publicToPem(keypair.publicKey) 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 = { const reviewWithKid = {
...r, ...r,
kid, kid,

View file

@ -61,17 +61,12 @@
opinion: opinion.data, opinion: opinion.data,
metadata: { nickname, is_affiliated: isAffiliated.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 { try {
await reviews.createReview(review) await reviews.createReview(review)
} catch (e) { } catch (e) {
console.error("Could not create review due to", e) console.error("Could not create review due to", e)
uploadFailed = "" + e uploadFailed = "" + e
} }
}
_state = "done" _state = "done"
} }
</script> </script>

View file

@ -667,7 +667,8 @@ export default class SpecialVisualizations {
{ {
nameKey: nameKey, nameKey: nameKey,
fallbackName, fallbackName,
} },
state.featureSwitchIsTesting
) )
return new SvelteUIElement(StarsBarIcon, { return new SvelteUIElement(StarsBarIcon, {
score: reviews.average, score: reviews.average,
@ -700,7 +701,8 @@ export default class SpecialVisualizations {
{ {
nameKey: nameKey, nameKey: nameKey,
fallbackName, fallbackName,
} },
state.featureSwitchIsTesting
) )
return new SvelteUIElement(ReviewForm, { reviews, state, tags, feature, layer }) return new SvelteUIElement(ReviewForm, { reviews, state, tags, feature, layer })
}, },
@ -732,7 +734,8 @@ export default class SpecialVisualizations {
{ {
nameKey: nameKey, nameKey: nameKey,
fallbackName, fallbackName,
} },
state.featureSwitchIsTesting
) )
return new SvelteUIElement(AllReviews, { reviews, state, tags, feature, layer }) return new SvelteUIElement(AllReviews, { reviews, state, tags, feature, layer })
}, },