|
| 1 | +/** |
| 2 | + * Content Rating Constants for Parental Controls |
| 3 | + * |
| 4 | + * Single source of truth for US content rating hierarchies and filtering logic. |
| 5 | + * Lower index = more restrictive (suitable for younger audiences). |
| 6 | + */ |
| 7 | + |
| 8 | +// MPAA Movie Ratings (US) |
| 9 | +export const MOVIE_RATINGS = ['G', 'PG', 'PG-13', 'R', 'NC-17'] as const; |
| 10 | +export type MovieRating = (typeof MOVIE_RATINGS)[number]; |
| 11 | + |
| 12 | +// TV Parental Guidelines Ratings (US) |
| 13 | +export const TV_RATINGS = [ |
| 14 | + 'TV-Y', |
| 15 | + 'TV-Y7', |
| 16 | + 'TV-G', |
| 17 | + 'TV-PG', |
| 18 | + 'TV-14', |
| 19 | + 'TV-MA', |
| 20 | +] as const; |
| 21 | +export type TvRating = (typeof TV_RATINGS)[number]; |
| 22 | + |
| 23 | +// Values that indicate content has no rating |
| 24 | +export const UNRATED_VALUES = ['NR', 'UR', 'Unrated', 'Not Rated', '']; |
| 25 | + |
| 26 | +/** Per-user content rating limits set by admins */ |
| 27 | +export interface UserContentRatingLimits { |
| 28 | + maxMovieRating?: string; |
| 29 | + maxTvRating?: string; |
| 30 | + blockUnrated?: boolean; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Check if a movie should be filtered out based on rating. |
| 35 | + * Returns true if the movie should be BLOCKED. |
| 36 | + * |
| 37 | + * Uses fail-closed approach: unknown/missing ratings are blocked |
| 38 | + * when blockUnrated is true. |
| 39 | + */ |
| 40 | +export function shouldFilterMovie( |
| 41 | + rating: string | undefined | null, |
| 42 | + maxRating: string | undefined, |
| 43 | + blockUnrated = false |
| 44 | +): boolean { |
| 45 | + if (!maxRating && !blockUnrated) return false; |
| 46 | + |
| 47 | + if (!rating || UNRATED_VALUES.includes(rating)) { |
| 48 | + return blockUnrated; |
| 49 | + } |
| 50 | + |
| 51 | + if (!maxRating) return false; |
| 52 | + |
| 53 | + const ratingIndex = MOVIE_RATINGS.indexOf(rating as MovieRating); |
| 54 | + const maxIndex = MOVIE_RATINGS.indexOf(maxRating as MovieRating); |
| 55 | + |
| 56 | + // Unknown rating not in our hierarchy — treat as unrated |
| 57 | + if (ratingIndex === -1) return blockUnrated; |
| 58 | + if (maxIndex === -1) return false; |
| 59 | + |
| 60 | + return ratingIndex > maxIndex; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Check if a TV show should be filtered out based on rating. |
| 65 | + * Returns true if the show should be BLOCKED. |
| 66 | + * |
| 67 | + * Uses fail-closed approach: unknown/missing ratings are blocked |
| 68 | + * when blockUnrated is true. |
| 69 | + */ |
| 70 | +export function shouldFilterTv( |
| 71 | + rating: string | undefined | null, |
| 72 | + maxRating: string | undefined, |
| 73 | + blockUnrated = false |
| 74 | +): boolean { |
| 75 | + if (!maxRating && !blockUnrated) return false; |
| 76 | + |
| 77 | + if (!rating || UNRATED_VALUES.includes(rating)) { |
| 78 | + return blockUnrated; |
| 79 | + } |
| 80 | + |
| 81 | + if (!maxRating) return false; |
| 82 | + |
| 83 | + const ratingIndex = TV_RATINGS.indexOf(rating as TvRating); |
| 84 | + const maxIndex = TV_RATINGS.indexOf(maxRating as TvRating); |
| 85 | + |
| 86 | + if (ratingIndex === -1) return blockUnrated; |
| 87 | + if (maxIndex === -1) return false; |
| 88 | + |
| 89 | + return ratingIndex > maxIndex; |
| 90 | +} |
| 91 | + |
| 92 | +/** Display options for movie rating dropdown (admin UI) */ |
| 93 | +export function getMovieRatingOptions(): { value: string; label: string }[] { |
| 94 | + return [ |
| 95 | + { value: '', label: 'No Restriction' }, |
| 96 | + { value: 'G', label: 'G - General Audiences' }, |
| 97 | + { value: 'PG', label: 'PG - Parental Guidance Suggested' }, |
| 98 | + { value: 'PG-13', label: 'PG-13 - Parents Strongly Cautioned' }, |
| 99 | + { value: 'R', label: 'R - Restricted' }, |
| 100 | + { value: 'NC-17', label: 'NC-17 - Adults Only' }, |
| 101 | + ]; |
| 102 | +} |
| 103 | + |
| 104 | +/** Display options for TV rating dropdown (admin UI) */ |
| 105 | +export function getTvRatingOptions(): { value: string; label: string }[] { |
| 106 | + return [ |
| 107 | + { value: '', label: 'No Restriction' }, |
| 108 | + { value: 'TV-Y', label: 'TV-Y - All Children' }, |
| 109 | + { value: 'TV-Y7', label: 'TV-Y7 - Directed to Older Children' }, |
| 110 | + { value: 'TV-G', label: 'TV-G - General Audience' }, |
| 111 | + { value: 'TV-PG', label: 'TV-PG - Parental Guidance Suggested' }, |
| 112 | + { value: 'TV-14', label: 'TV-14 - Parents Strongly Cautioned' }, |
| 113 | + { value: 'TV-MA', label: 'TV-MA - Mature Audience Only' }, |
| 114 | + ]; |
| 115 | +} |
0 commit comments