Repository navigation
feat(parental-controls): rating limits and blocked detail pages #3621
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
Open
ProgenyAlpha
wants to merge
1
commit into
seerr-team:develop
Choose a base branch
from
ProgenyAlpha:parental-controls/1-rating-limits
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { | ||
| shouldFilterMovie, | ||
| shouldFilterTv, | ||
| } from '@server/constants/contentRatings'; | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| describe('shouldFilterMovie', () => { | ||
| it('allows a rating within the cap', () => { | ||
| assert.equal(shouldFilterMovie('PG', 'PG-13'), false); | ||
| }); | ||
|
|
||
| it('blocks a rating above the cap', () => { | ||
| assert.equal(shouldFilterMovie('R', 'PG-13'), true); | ||
| }); | ||
|
|
||
| it('allows unrated content when blockUnrated is false', () => { | ||
| assert.equal(shouldFilterMovie('NR', 'PG-13', false), false); | ||
| }); | ||
|
|
||
| it('blocks unrated content when blockUnrated is true', () => { | ||
| assert.equal(shouldFilterMovie('NR', 'PG-13', true), true); | ||
| assert.equal(shouldFilterMovie(undefined, 'PG-13', true), true); | ||
| }); | ||
|
|
||
| it('fails closed on an invalid maxRating', () => { | ||
| assert.equal(shouldFilterMovie('G', 'NOT-A-RATING', false), true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('shouldFilterTv', () => { | ||
| it('allows a rating within the cap', () => { | ||
| assert.equal(shouldFilterTv('TV-PG', 'TV-14'), false); | ||
| }); | ||
|
|
||
| it('blocks a rating above the cap', () => { | ||
| assert.equal(shouldFilterTv('TV-MA', 'TV-14'), true); | ||
| }); | ||
|
|
||
| it('allows unrated content when blockUnrated is false', () => { | ||
| assert.equal(shouldFilterTv('Unrated', 'TV-14', false), false); | ||
| }); | ||
|
|
||
| it('blocks unrated content when blockUnrated is true', () => { | ||
| assert.equal(shouldFilterTv('Unrated', 'TV-14', true), true); | ||
| assert.equal(shouldFilterTv(null, 'TV-14', true), true); | ||
| }); | ||
|
|
||
| it('fails closed on an invalid maxRating', () => { | ||
| assert.equal(shouldFilterTv('TV-G', 'NOT-A-RATING', false), true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| export const RATING_COUNTRY = 'US'; | ||
|
|
||
| export const MOVIE_RATINGS = ['G', 'PG', 'PG-13', 'R', 'NC-17'] as const; | ||
| export type MovieRating = (typeof MOVIE_RATINGS)[number]; | ||
|
|
||
| export const TV_RATINGS = [ | ||
| 'TV-Y', | ||
| 'TV-Y7', | ||
| 'TV-G', | ||
| 'TV-PG', | ||
| 'TV-14', | ||
| 'TV-MA', | ||
| ] as const; | ||
| export type TvRating = (typeof TV_RATINGS)[number]; | ||
|
|
||
| export const UNRATED_VALUES = ['NR', 'UR', 'Unrated', 'Not Rated', '']; | ||
|
|
||
| export interface UserContentRatingLimits { | ||
| maxMovieRating?: string; | ||
| maxTvRating?: string; | ||
| blockUnrated?: boolean; | ||
| } | ||
|
|
||
| function shouldFilter( | ||
| ratings: readonly string[], | ||
| rating: string | undefined | null, | ||
| maxRating: string | undefined, | ||
| blockUnrated: boolean | ||
| ): boolean { | ||
| if (!rating || UNRATED_VALUES.includes(rating)) { | ||
| return blockUnrated; | ||
| } | ||
|
|
||
| if (!maxRating) return false; | ||
|
|
||
| const ratingIndex = ratings.indexOf(rating); | ||
| const maxIndex = ratings.indexOf(maxRating); | ||
|
|
||
| if (ratingIndex === -1) return blockUnrated; | ||
| if (maxIndex === -1) return true; | ||
|
|
||
| return ratingIndex > maxIndex; | ||
| } | ||
|
|
||
| export const shouldFilterMovie = ( | ||
| rating: string | undefined | null, | ||
| maxRating: string | undefined, | ||
| blockUnrated = false | ||
| ) => shouldFilter(MOVIE_RATINGS, rating, maxRating, blockUnrated); | ||
|
|
||
| export const shouldFilterTv = ( | ||
| rating: string | undefined | null, | ||
| maxRating: string | undefined, | ||
| blockUnrated = false | ||
| ) => shouldFilter(TV_RATINGS, rating, maxRating, blockUnrated); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { | ||
| getMovieCertification, | ||
| getTvCertification, | ||
| } from '@server/lib/contentRating'; | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| describe('getMovieCertification', () => { | ||
| const movie = (iso_3166_1: string, ...certifications: string[]) => ({ | ||
| release_dates: { | ||
| results: [ | ||
| { | ||
| iso_3166_1, | ||
| rating: '', | ||
| release_dates: certifications.map((certification) => ({ | ||
| certification, | ||
| release_date: '', | ||
| type: 3, | ||
| })), | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| it('returns a single US certification', () => { | ||
| assert.equal(getMovieCertification(movie('US', 'PG-13')), 'PG-13'); | ||
| }); | ||
|
|
||
| it('picks the most restrictive of multiple US certifications', () => { | ||
| assert.equal(getMovieCertification(movie('US', 'PG-13', 'R')), 'R'); | ||
| }); | ||
|
|
||
| it('ignores an unrated release when a real certification also exists', () => { | ||
| assert.equal(getMovieCertification(movie('US', 'NR', 'PG')), 'PG'); | ||
| }); | ||
|
|
||
| it('returns undefined when there is no US entry', () => { | ||
| assert.equal(getMovieCertification(movie('GB', '15')), undefined); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getTvCertification', () => { | ||
| const show = (...entries: [string, string][]) => ({ | ||
| content_ratings: { | ||
| results: entries.map(([iso_3166_1, rating]) => ({ iso_3166_1, rating })), | ||
| }, | ||
| }); | ||
|
|
||
| it('returns the US content rating', () => { | ||
| assert.equal(getTvCertification(show(['US', 'TV-14'])), 'TV-14'); | ||
| }); | ||
|
|
||
| it('picks the most restrictive of multiple US entries', () => { | ||
| assert.equal( | ||
| getTvCertification(show(['US', 'TV-Y7'], ['US', 'TV-14'])), | ||
| 'TV-14' | ||
| ); | ||
| }); | ||
|
|
||
| it('ignores an unrated entry when a real rating also exists', () => { | ||
| assert.equal( | ||
| getTvCertification(show(['US', 'NR'], ['US', 'TV-PG'])), | ||
| 'TV-PG' | ||
| ); | ||
| }); | ||
|
|
||
| it('returns undefined for an unrated US entry', () => { | ||
| assert.equal(getTvCertification(show(['US', 'NR'])), undefined); | ||
| }); | ||
|
|
||
| it('returns undefined when there is no US entry', () => { | ||
| assert.equal(getTvCertification(show(['GB', '12'])), undefined); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import type { | ||
| TmdbMovieDetails, | ||
| TmdbTvDetails, | ||
| } from '@server/api/themoviedb/interfaces'; | ||
| import { | ||
| MOVIE_RATINGS, | ||
| RATING_COUNTRY, | ||
| TV_RATINGS, | ||
| UNRATED_VALUES, | ||
| shouldFilterMovie, | ||
| shouldFilterTv, | ||
| type UserContentRatingLimits, | ||
| } from '@server/constants/contentRatings'; | ||
| import type { User } from '@server/entity/User'; | ||
|
|
||
| export function getUserContentRatingLimits( | ||
| user?: User | ||
| ): UserContentRatingLimits | undefined { | ||
| const maxMovieRating = user?.settings?.maxMovieRating ?? undefined; | ||
| const maxTvRating = user?.settings?.maxTvRating ?? undefined; | ||
| const blockUnrated = user?.settings?.blockUnrated ?? false; | ||
|
|
||
| if (!maxMovieRating && !maxTvRating && !blockUnrated) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { maxMovieRating, maxTvRating, blockUnrated }; | ||
| } | ||
|
|
||
| const mostRestrictive = (ratings: readonly string[], certs?: string[]) => | ||
| certs?.reduce<string | undefined>( | ||
| (worst, cert) => | ||
| worst === undefined || ratings.indexOf(cert) > ratings.indexOf(worst) | ||
| ? cert | ||
| : worst, | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| undefined | ||
| ); | ||
|
|
||
| // Most restrictive US certification. Unrated releases are ignored, so an | ||
| // unrated cut doesn't override the theatrical rating. | ||
| export function getMovieCertification( | ||
| details: Pick<TmdbMovieDetails, 'release_dates'> | ||
| ): string | undefined { | ||
| return mostRestrictive( | ||
| MOVIE_RATINGS, | ||
| details.release_dates?.results | ||
| .filter((r) => r.iso_3166_1 === RATING_COUNTRY) | ||
| .flatMap((r) => r.release_dates.map((rd) => rd.certification)) | ||
| .filter((cert) => !UNRATED_VALUES.includes(cert)) | ||
| ); | ||
| } | ||
|
|
||
| // A show can carry multiple US ratings (e.g. different seasons or networks); | ||
| // the most restrictive one wins. | ||
| export function getTvCertification( | ||
| details: Pick<TmdbTvDetails, 'content_ratings'> | ||
| ): string | undefined { | ||
| return mostRestrictive( | ||
| TV_RATINGS, | ||
| details.content_ratings?.results | ||
| .filter((r) => r.iso_3166_1 === RATING_COUNTRY) | ||
| .map((r) => r.rating) | ||
| .filter((rating) => rating && !UNRATED_VALUES.includes(rating)) | ||
| ); | ||
| } | ||
|
|
||
| export function isMovieRestricted( | ||
| user: User | undefined, | ||
| details: Pick<TmdbMovieDetails, 'release_dates'> | ||
| ): boolean { | ||
| const limits = getUserContentRatingLimits(user); | ||
| return ( | ||
| !!limits && | ||
| shouldFilterMovie( | ||
| getMovieCertification(details), | ||
| limits.maxMovieRating, | ||
| limits.blockUnrated | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| export function isTvRestricted( | ||
| user: User | undefined, | ||
| details: Pick<TmdbTvDetails, 'content_ratings'> | ||
| ): boolean { | ||
| const limits = getUserContentRatingLimits(user); | ||
| return ( | ||
| !!limits && | ||
| shouldFilterTv( | ||
| getTvCertification(details), | ||
| limits.maxTvRating, | ||
| limits.blockUnrated | ||
| ) | ||
| ); | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
server/migration/postgres/1792267428951-AddParentalControlColumns.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
|
||
| export class AddParentalControlColumns1792267428951 implements MigrationInterface { | ||
| name = 'AddParentalControlColumns1792267428951'; | ||
|
|
||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| const table = await queryRunner.getTable('user_settings'); | ||
| const columns = [ | ||
| ['maxMovieRating', 'character varying'], | ||
| ['maxTvRating', 'character varying'], | ||
| ['blockUnrated', 'boolean NOT NULL DEFAULT false'], | ||
| ]; | ||
| for (const [name, type] of columns) { | ||
| if (!table?.findColumnByName(name)) { | ||
| await queryRunner.query( | ||
| `ALTER TABLE "user_settings" ADD "${name}" ${type}` | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| `ALTER TABLE "user_settings" DROP COLUMN "blockUnrated"` | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "user_settings" DROP COLUMN "maxTvRating"` | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "user_settings" DROP COLUMN "maxMovieRating"` | ||
| ); | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.