Skip to content

Commit

Permalink
Move sensitivity faking into decoding stage
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Jul 11, 2023
1 parent 813bde9 commit d7ce36f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 27 deletions.
8 changes: 0 additions & 8 deletions frontend/src/stores/media/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ 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 @@ -438,12 +436,6 @@ export const useMediaStore = defineStore("media", {
}
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
8 changes: 0 additions & 8 deletions frontend/src/stores/media/single-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ 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 @@ -119,12 +117,6 @@ export const useSingleResultStore = defineStore("single-result", {
const service = initServices[type](accessToken)
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

Expand Down
7 changes: 6 additions & 1 deletion frontend/src/types/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export interface Media {
fields_matched?: string[]

mature: boolean
sensitivity: string[]
isSensitive: boolean
}

export interface ImageDetail extends Media {
Expand Down Expand Up @@ -90,7 +92,10 @@ export type DetailFromMediaType<T extends SupportedMediaType> =
* being decoded in the `decodeMediaData` function.
*/
export interface ApiMedia
extends Omit<Media, "frontendMediaType" | "title" | "originalTitle"> {
extends Omit<
Media,
"frontendMediaType" | "title" | "originalTitle" | "isSensitive"
> {
title?: string
originalTitle?: string
}
Expand Down
29 changes: 22 additions & 7 deletions frontend/src/utils/content-safety.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* 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"

Expand All @@ -11,13 +10,29 @@ import { log } from "~/utils/console"
* 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 id - the ID of the item to mark as mature
* @param frac - the fraction of items to mark as mature
* @returns an array of strings representing the mature flags
*/
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)
export const markFakeSensitive = (id: string, frac = 0.5): string[] => {
const random = prng(hash(id))()

if (random > frac) {
return []
}

const sensitivityMask = Math.floor((random * 7) / frac) + 1
const sensitivity = []
if ((sensitivityMask & 4) !== 0) {
sensitivity.push("user_reported_sensitive")
}
if ((sensitivityMask & 2) !== 0) {
sensitivity.push("provider_supplied_sensitive")
}
if ((sensitivityMask & 1) !== 0) {
sensitivity.push("sensitive_text")
}

log("Fake mature", id, sensitivity)
return sensitivity
}
18 changes: 15 additions & 3 deletions frontend/src/utils/decode-media-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { decodeData as decodeString } from "~/utils/decode-data"
import type { ApiMedia, Media, Tag } from "~/types/media"
import type { MediaType } from "~/constants/media"
import { AUDIO, IMAGE, MODEL_3D, VIDEO } from "~/constants/media"
import { useFeatureFlagStore } from "~/stores/feature-flag"
import { markFakeSensitive } from "~/utils/content-safety"

const mediaTypeExtensions: Record<MediaType, string[]> = {
[IMAGE]: ["jpg", "jpeg", "png", "gif", "svg"],
Expand Down Expand Up @@ -83,8 +85,15 @@ const mediaTitle = (
export const decodeMediaData = <T extends Media>(
media: ApiMedia,
mediaType: T["frontendMediaType"]
): T =>
({
): T => {
// Fake ~50% of results as mature.
const featureFlagStore = useFeatureFlagStore()
const sensitivity = featureFlagStore.isOn("fake_sensitive")
? markFakeSensitive(media.id)
: []
const isSensitive = sensitivity.length > 0

return {
...media,
...mediaTitle(media, mediaType),
frontendMediaType: mediaType,
Expand All @@ -94,4 +103,7 @@ export const decodeMediaData = <T extends Media>(
...tag,
name: decodeString(tag.name),
})),
} as T)
sensitivity,
isSensitive,
} as T
}
9 changes: 9 additions & 0 deletions frontend/test/unit/specs/utils/decode-image-data.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createPinia, setActivePinia } from "~~/test/unit/test-utils/pinia"

import { decodeMediaData } from "~/utils/decode-media-data"
import { IMAGE } from "~/constants/media"

Expand All @@ -15,10 +17,17 @@ const requiredFields = {
detail_url: "url",
related_url: "url",

sensitivity: [],
isSensitive: false,

tags: [],
}

describe("decodeImageData", () => {
beforeEach(() => {
setActivePinia(createPinia())
})

it("decodes symbols correctly", () => {
const data = {
...requiredFields,
Expand Down

0 comments on commit d7ce36f

Please sign in to comment.