Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature flag for fake marking results as sensitive #862

Merged
merged 4 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions frontend/feat/feature-flags.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"features": {
"fake_sensitive": {
"status": {
"staging": "switchable",
"production": "disabled"
},
"description": "Mark 50% of results as mature to test content safety.",
"defaultState": "off"
},
"external_sources": {
"status": {
"staging": "switchable",
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/stores/media/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { initServices } from "~/stores/media/services"
import { isSearchTypeSupported, useSearchStore } from "~/stores/search"
import { useRelatedMediaStore } from "~/stores/media/related-media"
import { deepFreeze } from "~/utils/deep-freeze"
import { useFeatureFlagStore } from "~/stores/feature-flag"
import { markFakeSensitive } from "~/utils/content-safety"

export type MediaStoreResult = {
count: number
Expand Down Expand Up @@ -426,6 +428,13 @@ export const useMediaStore = defineStore("media", {
page = undefined
}
this._updateFetchState(mediaType, "end", errorMessage)

// Fake ~50% of results as mature. This leaves actual mature results unchanged.
const featureFlagStore = useFeatureFlagStore()
if (featureFlagStore.isOn("fake_sensitive")) {
Object.values(data.results).forEach(markFakeSensitive)
}

this.setMedia({
mediaType,
media: data.results,
Expand Down
12 changes: 11 additions & 1 deletion frontend/src/stores/media/single-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { initServices } from "~/stores/media/services"
import { useMediaStore } from "~/stores/media/index"
import { useRelatedMediaStore } from "~/stores/media/related-media"
import { useProviderStore } from "~/stores/provider"
import { useFeatureFlagStore } from "~/stores/feature-flag"
import { markFakeSensitive } from "~/utils/content-safety"

export type MediaItemState =
| {
Expand Down Expand Up @@ -115,7 +117,15 @@ export const useSingleResultStore = defineStore("single-result", {
this._updateFetchState("start")
const accessToken = this.$nuxt.$openverseApiToken
const service = initServices[type](accessToken)
this.mediaItem = this._addProviderName(await service.getMediaDetail(id))
const item = this._addProviderName(await service.getMediaDetail(id))

// Fake ~50% of results as mature. This leaves actual mature results unchanged.
const featureFlagStore = useFeatureFlagStore()
if (featureFlagStore.isOn("fake_sensitive")) {
markFakeSensitive(item)
}

this.mediaItem = item
this.mediaType = type

this._updateFetchState("end")
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/types/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export interface Media {

tags: Tag[]
fields_matched?: string[]

mature: boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I didn't know we didn't have this in the type! Great catch! Is it always available, or should we mark it as optional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always available.

}

export interface ImageDetail extends Media {
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/utils/content-safety.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Contains utilities related to content safety.
*/

import type { Media } from "~/types/media"
import { hash, rand as prng } from "~/utils/prng"
import { log } from "~/utils/console"

/**
* Marks the given item as mature based on a random number seeded with the
* item's UUID v4 identifier. The `frac` param controls the probability of an
* item being marked as mature.
*
* @param item - the item to mark as mature
* @param frac - the fraction of items to mark as mature
*/
export const markFakeSensitive = (item: Media, frac = 0.5) => {
const random = prng(hash(item.id))()
if (random < frac) {
item.mature = true
log("Fake mature", item.frontendMediaType, item.id)
}
}