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"
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
*/
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>
constructor(mangroveIdentity: UIEventSource<string>, mangroveIdentityCreationDate: UIEventSource<string>) {
this.mangroveIdentity = mangroveIdentity
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) => {
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<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 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<string>
private readonly _identity: MangroveIdentity
private readonly _testmode: Store<boolean>
private constructor(
feature: Feature,
@ -155,11 +156,13 @@ export default class FeatureReviews {
nameKey?: "name" | string
fallbackName?: string
uncertaintyRadius?: number
}
},
testmode?: Store<boolean>
) {
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<Record<string, string>>,
mangroveIdentity?: MangroveIdentity,
options?: {
mangroveIdentity: MangroveIdentity,
options: {
nameKey?: "name" | string
fallbackName?: string
uncertaintyRadius?: number
}
) {
},
testmode: Store<boolean>
): 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,

View file

@ -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"
}
</script>

View file

@ -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 })
},