Reviews: add additional heuristic to hide reviews if the name is different

This commit is contained in:
Pieter Vander Vennet 2024-06-19 00:09:35 +02:00
parent 3f8c85cf8c
commit 8bcbad5628

View file

@ -77,6 +77,9 @@ export class MangroveIdentity {
private geoReviewsById: Store<(Review & { kid: string; signature: string })[]> = undefined private geoReviewsById: Store<(Review & { kid: string; signature: string })[]> = undefined
/**
* Subset of `getAllReviews()` which contains all reviews which use the `geo:` as URL
*/
public getGeoReviews(): Store<(Review & { kid: string; signature: string })[] | undefined> { public getGeoReviews(): Store<(Review & { kid: string; signature: string })[] | undefined> {
if (!this.geoReviewsById) { if (!this.geoReviewsById) {
this.geoReviewsById = this.getAllReviews().mapD((reviews) => this.geoReviewsById = this.getAllReviews().mapD((reviews) =>
@ -205,19 +208,21 @@ export default class FeatureReviews {
this.subjectUri.addCallbackAndRunD(async (sub) => { this.subjectUri.addCallbackAndRunD(async (sub) => {
const reviews = await MangroveReviews.getReviews({ sub }) const reviews = await MangroveReviews.getReviews({ sub })
this.addReviews(reviews.reviews) console.log("Got reviews for", feature, reviews, sub)
}) this.addReviews(reviews.reviews, this._name.data)
}, [this._name])
/* We also construct all subject queries _without_ encoding the name to work around a previous bug /* We also construct all subject queries _without_ encoding the name to work around a previous bug
* See https://github.com/giggls/opencampsitemap/issues/30 * See https://github.com/giggls/opencampsitemap/issues/30
*/ */
this.ConstructSubjectUri(true).addCallbackAndRunD(async (sub) => { this.ConstructSubjectUri(true).mapD(async (sub) => {
try { try {
const reviews = await MangroveReviews.getReviews({ sub }) const reviews = await MangroveReviews.getReviews({ sub })
this.addReviews(reviews.reviews) console.log("Got reviews (no-encode) for", feature, reviews, sub)
this.addReviews(reviews.reviews, this._name.data)
} catch (e) { } catch (e) {
console.log("Could not fetch reviews for partially incorrect query ", sub) console.log("Could not fetch reviews for partially incorrect query ", sub)
} }
}) }, [this._name])
this.average = this._reviews.map((reviews) => { this.average = this._reviews.map((reviews) => {
if (!reviews) { if (!reviews) {
return null return null
@ -311,11 +316,12 @@ export default class FeatureReviews {
} }
/** /**
* Adds given reviews to the 'reviews'-UI-eventsource * Adds given reviews to the 'reviews'-UI-eventsource, if they match close enough.
* We assume only geo-reviews are passed in (as they should be queried using the 'geo'-part)
* @param reviews * @param reviews
* @private * @private
*/ */
private addReviews(reviews: { payload: Review; kid: string; signature: string }[]) { private addReviews(reviews: { payload: Review; kid: string; signature: string }[], expectedName: string) {
const alreadyKnown = new Set(this._reviews.data.map((r) => r.rating + " " + r.opinion)) const alreadyKnown = new Set(this._reviews.data.map((r) => r.rating + " " + r.opinion))
let hasNew = false let hasNew = false
@ -324,22 +330,33 @@ export default class FeatureReviews {
try { try {
const url = new URL(review.sub) const url = new URL(review.sub)
if (url.protocol === "geo:") { if (url.protocol !== "geo:") {
const coordinate = <[number, number]>( continue
url.pathname.split(",").map((n) => Number(n)) }
) const coordinate = <[number, number]>(
const distance = GeoOperations.distanceBetween( url.pathname.split(",").map((n) => Number(n))
[this._lat, this._lon], )
coordinate const distance = GeoOperations.distanceBetween(
) [this._lat, this._lon],
if (distance > this._uncertainty) { coordinate
continue )
} if (distance > this._uncertainty) {
continue
}
const nameUrl = url.searchParams.get("q")
const distanceName = Utils.levenshteinDistance(nameUrl.toLowerCase(), expectedName.toLowerCase()) / expectedName.length
if(distanceName > 0.25){
// Then name is wildly different
continue
} }
} catch (e) { } catch (e) {
console.warn(e) console.warn(e)
// Not a valid URL, ignore this review
continue
} }
const key = review.rating + " " + review.opinion const key = review.rating + " " + review.opinion
if (alreadyKnown.has(key)) { if (alreadyKnown.has(key)) {
continue continue