Skip to content

Commit

Permalink
New: Auto tag movies based on tags present/absent on movies
Browse files Browse the repository at this point in the history
(cherry picked from commit f4c19a384bd9bb4e35c9fa0ca5d9a448c04e409e)

Closes #9916
  • Loading branch information
markus101 authored and mynameisbogdan committed Apr 10, 2024
1 parent 56be950 commit f7ca0b8
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 8 deletions.
5 changes: 5 additions & 0 deletions frontend/src/Components/Form/FormInputGroup.js
Expand Up @@ -17,6 +17,7 @@ import IndexerSelectInputConnector from './IndexerSelectInputConnector';
import KeyValueListInput from './KeyValueListInput';
import LanguageSelectInputConnector from './LanguageSelectInputConnector';
import MovieMonitoredSelectInput from './MovieMonitoredSelectInput';
import MovieTagInput from './MovieTagInput';
import NumberInput from './NumberInput';
import OAuthInputConnector from './OAuthInputConnector';
import PasswordInput from './PasswordInput';
Expand Down Expand Up @@ -89,6 +90,10 @@ function getComponent(type) {

case inputTypes.DYNAMIC_SELECT:
return EnhancedSelectInputConnector;

case inputTypes.MOVIE_TAG:
return MovieTagInput;

case inputTypes.TAG:
return TagInputConnector;

Expand Down
53 changes: 53 additions & 0 deletions frontend/src/Components/Form/MovieTagInput.tsx
@@ -0,0 +1,53 @@
import React, { useCallback } from 'react';
import TagInputConnector from './TagInputConnector';

interface MovieTagInputProps {
name: string;
value: number | number[];
onChange: ({
name,
value,
}: {
name: string;
value: number | number[];
}) => void;
}

export default function MovieTagInput(props: MovieTagInputProps) {
const { value, onChange, ...otherProps } = props;
const isArray = Array.isArray(value);

const handleChange = useCallback(
({ name, value: newValue }: { name: string; value: number[] }) => {
if (isArray) {
onChange({ name, value: newValue });
} else {
onChange({
name,
value: newValue.length ? newValue[newValue.length - 1] : 0,
});
}
},
[isArray, onChange]
);

let finalValue: number[] = [];

if (isArray) {
finalValue = value;
} else if (value === 0) {
finalValue = [];
} else {
finalValue = [value];
}

return (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore 2786 'TagInputConnector' isn't typed yet
<TagInputConnector
{...otherProps}
value={finalValue}
onChange={handleChange}
/>
);
}
2 changes: 2 additions & 0 deletions frontend/src/Components/Form/ProviderFieldFormGroup.js
Expand Up @@ -27,6 +27,8 @@ function getType({ type, selectOptionsProviderAction }) {
return inputTypes.DYNAMIC_SELECT;
}
return inputTypes.SELECT;
case 'movieTag':
return inputTypes.MOVIE_TAG;
case 'tag':
return inputTypes.TEXT_TAG;
case 'tagSelect':
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/Helpers/Props/inputTypes.js
Expand Up @@ -17,6 +17,7 @@ export const INDEXER_FLAGS_SELECT = 'indexerFlagsSelect';
export const LANGUAGE_SELECT = 'languageSelect';
export const DOWNLOAD_CLIENT_SELECT = 'downloadClientSelect';
export const SELECT = 'select';
export const MOVIE_TAG = 'movieTag';
export const DYNAMIC_SELECT = 'dynamicSelect';
export const TAG = 'tag';
export const TEXT = 'text';
Expand Down Expand Up @@ -45,6 +46,7 @@ export const all = [
INDEXER_FLAGS_SELECT,
LANGUAGE_SELECT,
SELECT,
MOVIE_TAG,
DYNAMIC_SELECT,
TAG,
TEXT,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/Settings/Tags/TagInUse.js
Expand Up @@ -12,7 +12,7 @@ export default function TagInUse(props) {
return null;
}

if (count > 1 && labelPlural ) {
if (count > 1 && labelPlural) {
return (
<div>
{count} {labelPlural.toLowerCase()}
Expand Down
@@ -1,6 +1,9 @@
using System.Collections.Generic;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.AutoTagging;
using NzbDrone.Core.AutoTagging.Specifications;
using NzbDrone.Core.Housekeeping.Housekeepers;
using NzbDrone.Core.Profiles.Releases;
using NzbDrone.Core.Tags;
Expand Down Expand Up @@ -43,5 +46,35 @@ public void should_not_delete_used_tags()
Subject.Clean();
AllStoredModels.Should().HaveCount(1);
}

[Test]
public void should_not_delete_used_auto_tagging_tag_specification_tags()
{
var tags = Builder<Tag>
.CreateListOfSize(2)
.All()
.With(x => x.Id = 0)
.BuildList();
Db.InsertMany(tags);

var autoTags = Builder<AutoTag>.CreateListOfSize(1)
.All()
.With(x => x.Id = 0)
.With(x => x.Specifications = new List<IAutoTaggingSpecification>
{
new TagSpecification
{
Name = "Test",
Value = tags[0].Id
}
})
.BuildList();

Mocker.GetMock<IAutoTaggingRepository>().Setup(s => s.All())
.Returns(autoTags);

Subject.Clean();
AllStoredModels.Should().HaveCount(1);
}
}
}
3 changes: 2 additions & 1 deletion src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs
Expand Up @@ -84,7 +84,8 @@ public enum FieldType
Device,
TagSelect,
RootFolder,
QualityProfile
QualityProfile,
MovieTag
}

public enum HiddenType
Expand Down
36 changes: 36 additions & 0 deletions src/NzbDrone.Core/AutoTagging/Specifications/TagSpecification.cs
@@ -0,0 +1,36 @@
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.Movies;
using NzbDrone.Core.Validation;

namespace NzbDrone.Core.AutoTagging.Specifications
{
public class TagSpecificationValidator : AbstractValidator<TagSpecification>
{
public TagSpecificationValidator()
{
RuleFor(c => c.Value).GreaterThan(0);
}
}

public class TagSpecification : AutoTaggingSpecificationBase
{
private static readonly TagSpecificationValidator Validator = new ();

public override int Order => 1;
public override string ImplementationName => "Tag";

[FieldDefinition(1, Label = "AutoTaggingSpecificationTag", Type = FieldType.MovieTag)]
public int Value { get; set; }

protected override bool IsSatisfiedByWithoutNegate(Movie movie)
{
return movie.Tags.Contains(Value);
}

public override NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}
36 changes: 33 additions & 3 deletions src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupUnusedTags.cs
Expand Up @@ -3,24 +3,33 @@
using System.Linq;
using Dapper;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.AutoTagging;
using NzbDrone.Core.AutoTagging.Specifications;
using NzbDrone.Core.Datastore;

namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupUnusedTags : IHousekeepingTask
{
private readonly IMainDatabase _database;
private readonly IAutoTaggingRepository _autoTaggingRepository;

public CleanupUnusedTags(IMainDatabase database)
public CleanupUnusedTags(IMainDatabase database, IAutoTaggingRepository autoTaggingRepository)
{
_database = database;
_autoTaggingRepository = autoTaggingRepository;
}

public void Clean()
{
using var mapper = _database.OpenConnection();
var usedTags = new[] { "Movies", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers", "AutoTagging", "DownloadClients" }
var usedTags = new[]
{
"Movies", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers",
"AutoTagging", "DownloadClients"
}
.SelectMany(v => GetUsedTags(v, mapper))
.Concat(GetAutoTaggingTagSpecificationTags(mapper))
.Distinct()
.ToList();

Expand All @@ -45,10 +54,31 @@ public void Clean()

private int[] GetUsedTags(string table, IDbConnection mapper)
{
return mapper.Query<List<int>>($"SELECT DISTINCT \"Tags\" FROM \"{table}\" WHERE NOT \"Tags\" = '[]' AND NOT \"Tags\" IS NULL")
return mapper
.Query<List<int>>(
$"SELECT DISTINCT \"Tags\" FROM \"{table}\" WHERE NOT \"Tags\" = '[]' AND NOT \"Tags\" IS NULL")
.SelectMany(x => x)
.Distinct()
.ToArray();
}

private List<int> GetAutoTaggingTagSpecificationTags(IDbConnection mapper)
{
var tags = new List<int>();
var autoTags = _autoTaggingRepository.All();

foreach (var autoTag in autoTags)
{
foreach (var specification in autoTag.Specifications)
{
if (specification is TagSpecification tagSpec)
{
tags.Add(tagSpec.Value);
}
}
}

return tags;
}
}
}
3 changes: 2 additions & 1 deletion src/NzbDrone.Core/Localization/Core/en.json
Expand Up @@ -113,6 +113,7 @@
"AutoTaggingLoadError": "Unable to load auto tagging",
"AutoTaggingNegateHelpText": "If checked, the auto tagging rule will not apply if this {implementationName} condition matches.",
"AutoTaggingRequiredHelpText": "This {implementationName} condition must match for the auto tagging rule to apply. Otherwise a single {implementationName} match is sufficient.",
"AutoTaggingSpecificationTag": "Tag",
"AutoUnmonitorPreviouslyDownloadedMoviesHelpText": "Movies deleted from the disk are automatically unmonitored in {appName}",
"Automatic": "Automatic",
"AutomaticAdd": "Automatic Add",
Expand Down Expand Up @@ -257,8 +258,8 @@
"CustomFormats": "Custom Formats",
"CustomFormatsLoadError": "Unable to load Custom Formats",
"CustomFormatsSettings": "Custom Formats Settings",
"CustomFormatsSettingsTriggerInfo": "A Custom Format will be applied to a release or file when it matches at least one of each of the different condition types chosen.",
"CustomFormatsSettingsSummary": "Custom Formats and Settings",
"CustomFormatsSettingsTriggerInfo": "A Custom Format will be applied to a release or file when it matches at least one of each of the different condition types chosen.",
"CustomFormatsSpecificationFlag": "Flag",
"CustomFormatsSpecificationRegularExpression": "Regular Expression",
"CustomFormatsSpecificationRegularExpressionHelpText": "Custom Format RegEx is Case Insensitive",
Expand Down
23 changes: 21 additions & 2 deletions src/NzbDrone.Core/Tags/TagService.cs
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Core.AutoTagging;
using NzbDrone.Core.AutoTagging.Specifications;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Download;
using NzbDrone.Core.ImportLists;
Expand Down Expand Up @@ -120,7 +121,7 @@ public List<TagDetails> Details()
var releaseProfiles = _releaseProfileService.All();
var movies = _movieService.AllMovieTags();
var indexers = _indexerService.All();
var autotags = _autoTaggingService.All();
var autoTags = _autoTaggingService.All();
var downloadClients = _downloadClientFactory.All();

var details = new List<TagDetails>();
Expand All @@ -137,7 +138,7 @@ public List<TagDetails> Details()
ReleaseProfileIds = releaseProfiles.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
MovieIds = movies.Where(c => c.Value.Contains(tag.Id)).Select(c => c.Key).ToList(),
IndexerIds = indexers.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
AutoTagIds = autotags.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
AutoTagIds = GetAutoTagIds(tag, autoTags),
DownloadClientIds = downloadClients.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
});
}
Expand Down Expand Up @@ -188,5 +189,23 @@ public void Delete(int tagId)
_repo.Delete(tagId);
_eventAggregator.PublishEvent(new TagsUpdatedEvent());
}

private List<int> GetAutoTagIds(Tag tag, List<AutoTag> autoTags)
{
var autoTagIds = autoTags.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList();

foreach (var autoTag in autoTags)
{
foreach (var specification in autoTag.Specifications)
{
if (specification is TagSpecification)
{
autoTagIds.Add(autoTag.Id);
}
}
}

return autoTagIds.Distinct().ToList();
}
}
}

0 comments on commit f7ca0b8

Please sign in to comment.