-
-
Notifications
You must be signed in to change notification settings - Fork 489
feat(overriderules): apply override rules to advanced requests #2164
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
gauthier-th
wants to merge
2
commits into
develop
Choose a base branch
from
advanced-override-rules
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.
+269
−117
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,153 @@ | ||
| import { ANIME_KEYWORD_ID } from '@server/api/themoviedb/constants'; | ||
| import type { | ||
| TmdbKeyword, | ||
| TmdbMovieDetails, | ||
| TmdbTvDetails, | ||
| } from '@server/api/themoviedb/interfaces'; | ||
| import { MediaType } from '@server/constants/media'; | ||
| import { getRepository } from '@server/datasource'; | ||
| import OverrideRule from '@server/entity/OverrideRule'; | ||
| import type { User } from '@server/entity/User'; | ||
| import { getSettings } from '@server/lib/settings'; | ||
| import logger from '@server/logger'; | ||
|
|
||
| export type OverrideRulesResult = { | ||
| rootFolder: string | null; | ||
| profileId: number | null; | ||
| tags: number[] | null; | ||
| }; | ||
|
|
||
| async function overrideRules({ | ||
| mediaType, | ||
| is4k, | ||
| tmdbMedia, | ||
| requestUser, | ||
| }: { | ||
| mediaType: MediaType; | ||
| is4k: boolean; | ||
| tmdbMedia: TmdbMovieDetails | TmdbTvDetails; | ||
| requestUser: User; | ||
| }): Promise<OverrideRulesResult> { | ||
| const settings = getSettings(); | ||
|
|
||
| let rootFolder: string | null = null; | ||
| let profileId: number | null = null; | ||
| let tags: number[] | null = null; | ||
|
|
||
| const defaultRadarrId = is4k | ||
| ? settings.radarr.findIndex((r) => r.is4k && r.isDefault) | ||
| : settings.radarr.findIndex((r) => !r.is4k && r.isDefault); | ||
| const defaultSonarrId = is4k | ||
| ? settings.sonarr.findIndex((s) => s.is4k && s.isDefault) | ||
| : settings.sonarr.findIndex((s) => !s.is4k && s.isDefault); | ||
|
|
||
| const overrideRuleRepository = getRepository(OverrideRule); | ||
| const overrideRules = await overrideRuleRepository.find({ | ||
| where: | ||
| mediaType === MediaType.MOVIE | ||
| ? { radarrServiceId: defaultRadarrId } | ||
| : { sonarrServiceId: defaultSonarrId }, | ||
| }); | ||
|
|
||
| const appliedOverrideRules = overrideRules.filter((rule) => { | ||
| const hasAnimeKeyword = | ||
| 'results' in tmdbMedia.keywords && | ||
| tmdbMedia.keywords.results.some( | ||
| (keyword: TmdbKeyword) => keyword.id === ANIME_KEYWORD_ID | ||
| ); | ||
|
|
||
| // Skip override rules if the media is an anime TV show as anime TV | ||
| // is handled by default and override rules do not explicitly include | ||
| // the anime keyword | ||
| if ( | ||
| mediaType === MediaType.TV && | ||
| hasAnimeKeyword && | ||
| (!rule.keywords || | ||
| !rule.keywords.split(',').map(Number).includes(ANIME_KEYWORD_ID)) | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( | ||
| rule.users && | ||
| !rule.users.split(',').some((userId) => Number(userId) === requestUser.id) | ||
| ) { | ||
| return false; | ||
| } | ||
| if ( | ||
| rule.genre && | ||
| !rule.genre | ||
| .split(',') | ||
| .some((genreId) => | ||
| tmdbMedia.genres.map((genre) => genre.id).includes(Number(genreId)) | ||
| ) | ||
| ) { | ||
| return false; | ||
| } | ||
| if ( | ||
| rule.language && | ||
| !rule.language | ||
| .split('|') | ||
| .some((languageId) => languageId === tmdbMedia.original_language) | ||
| ) { | ||
| return false; | ||
| } | ||
| if ( | ||
| rule.keywords && | ||
| !rule.keywords.split(',').some((keywordId) => { | ||
| let keywordList: TmdbKeyword[] = []; | ||
|
|
||
| if ('keywords' in tmdbMedia.keywords) { | ||
| keywordList = tmdbMedia.keywords.keywords; | ||
| } else if ('results' in tmdbMedia.keywords) { | ||
| keywordList = tmdbMedia.keywords.results; | ||
| } | ||
|
|
||
| return keywordList | ||
| .map((keyword: TmdbKeyword) => keyword.id) | ||
| .includes(Number(keywordId)); | ||
| }) | ||
| ) { | ||
| return false; | ||
| } | ||
| return true; | ||
| }); | ||
|
|
||
| // hacky way to prioritize rules | ||
| // TODO: make this better | ||
| const prioritizedRule = appliedOverrideRules.sort((a, b) => { | ||
| const keys: (keyof OverrideRule)[] = ['genre', 'language', 'keywords']; | ||
|
|
||
| const aSpecificity = keys.filter((key) => a[key] !== null).length; | ||
| const bSpecificity = keys.filter((key) => b[key] !== null).length; | ||
|
|
||
| // Take the rule with the most specific condition first | ||
| return bSpecificity - aSpecificity; | ||
| })[0]; | ||
|
|
||
| if (prioritizedRule) { | ||
| if (prioritizedRule.rootFolder) { | ||
| rootFolder = prioritizedRule.rootFolder; | ||
| } | ||
| if (prioritizedRule.profileId) { | ||
| profileId = prioritizedRule.profileId; | ||
| } | ||
| if (prioritizedRule.tags) { | ||
| tags = [ | ||
| ...new Set([ | ||
| ...(tags || []), | ||
| ...prioritizedRule.tags.split(',').map((tag) => Number(tag)), | ||
| ]), | ||
| ]; | ||
| } | ||
|
|
||
| logger.debug('Override rule applied.', { | ||
| label: 'Media Request', | ||
| overrides: prioritizedRule, | ||
| }); | ||
| } | ||
|
|
||
| return { rootFolder, profileId, tags }; | ||
| } | ||
|
|
||
| export default overrideRules; | ||
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
Oops, something went wrong.
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.
Check warning
Code scanning / CodeQL
Useless conditional Warning
Copilot Autofix
AI 3 days ago
To fix the problem, simply remove the unnecessary conditional
(tags || [])on line 138 and replace it with justtags. This maintains existing functionality and clarifies the code by removing an always-true/always-false (useless) conditional. Only modify line 138 within the fileserver/lib/overrideRules.ts. No additional imports or definitions are needed.