Barazo AppView backend
barazo.forum
1// ---------------------------------------------------------------------------
2// Content maturity filtering helpers
3// ---------------------------------------------------------------------------
4// Determines the maximum maturity level a user is allowed to see based on
5// authentication status, declared age, age threshold, and maturity preference.
6// ---------------------------------------------------------------------------
7
8import { isMaturityAtMost, ratingsAtMost } from './maturity.js'
9import type { MaturityRating } from './maturity.js'
10
11/** Minimal user shape needed for maturity resolution. */
12export interface MaturityUser {
13 declaredAge: number | null | undefined
14 maturityPref: string
15}
16
17/** Default age threshold (GDPR Art. 8 strictest: 16). */
18const DEFAULT_AGE_THRESHOLD = 16
19
20/**
21 * Resolve the maximum maturity rating a user is allowed to view.
22 *
23 * Rules:
24 * - Unauthenticated (user is undefined): "safe" only
25 * - No age declared (null): "safe" only
26 * - "Rather not say" (0): "safe" only
27 * - Declared age below community threshold: "safe" only
28 * - Declared age meets threshold: use their maturityPref
29 */
30export function resolveMaxMaturity(
31 user: MaturityUser | undefined,
32 ageThreshold: number = DEFAULT_AGE_THRESHOLD
33): MaturityRating {
34 if (!user) return 'safe'
35 if (user.declaredAge === null || user.declaredAge === undefined) return 'safe'
36 if (user.declaredAge === 0) return 'safe'
37 if (user.declaredAge < ageThreshold) return 'safe'
38 const pref = user.maturityPref as MaturityRating
39 return pref
40}
41
42/**
43 * Check whether content with the given maturity rating is visible
44 * at the specified maximum allowed maturity level.
45 *
46 * A content rating is allowed if it is <= maxAllowed in the hierarchy:
47 * safe (0) <= mature (1) <= adult (2)
48 */
49export function maturityAllows(maxAllowed: MaturityRating, contentRating: MaturityRating): boolean {
50 return isMaturityAtMost(contentRating, maxAllowed)
51}
52
53/**
54 * Return the list of maturity ratings that are visible at the given max level.
55 * Useful for building SQL IN clauses.
56 */
57export function allowedRatings(maxAllowed: MaturityRating): MaturityRating[] {
58 return ratingsAtMost(maxAllowed)
59}