Do not allow already available movies to be imported via the watchlist

pull/4591/head
tidusjar 3 years ago
parent 0492e71ab7
commit 7ec8939cdf

@ -1,6 +1,7 @@
using MockQueryable.Moq;
using Moq;
using NUnit.Framework;
using Ombi.Core.Engine;
using Ombi.Core.Rule.Rules.Request;
using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests;
@ -176,6 +177,47 @@ namespace Ombi.Core.Tests.Rule.Request
Assert.That(result.Success, Is.True);
}
[Test]
public async Task RequestMovie_IsSuccessful()
{
SetupMockData();
var req = new MovieRequests
{
RequestType = RequestType.Movie,
TheMovieDbId = 123,
Id = 1,
};
var result = await Rule.Execute(req);
Assert.That(result.Success, Is.True);
}
[Test]
public async Task RequestMovie_IsAlreadyAvailable()
{
var content = new List<PlexServerContent> {
new PlexServerContent
{
TheMovieDbId = 123.ToString(),
}
};
PlexContentRepo.Setup(x => x.GetAll()).Returns(content.AsQueryable().BuildMock().Object);
var req = new MovieRequests
{
RequestType = RequestType.Movie,
TheMovieDbId = 123,
Id = 1,
};
var result = await Rule.Execute(req);
Assert.That(result.Success, Is.False);
Assert.That(result.ErrorCode, Is.EqualTo(ErrorCode.AlreadyRequested));
}
private void SetupMockData()
{
var childRequests = new List<PlexServerContent>

@ -54,6 +54,15 @@ namespace Ombi.Core.Rule.Rules.Request
// looks like we have a match on the TVDbID
return CheckExistingContent(tvRequest, anyMovieDbMatches);
}
if (obj.RequestType == RequestType.Movie)
{
var movie = (MovieRequests)obj;
var exists = _plexContent.GetAll().Where(x => x.Type == MediaType.Movie).Any(x => x.TheMovieDbId == movie.Id.ToString() || x.TheMovieDbId == movie.TheMovieDbId.ToString());
if (exists)
{
return Fail(ErrorCode.AlreadyRequested, "This movie is already available." );
}
}
return Success();
}

@ -267,7 +267,7 @@ namespace Ombi.Schedule.Tests
_mocker.Verify<IPlexApi>(x => x.GetWatchlistMetadata("abc", It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once);
_mocker.Verify<IMovieRequestEngine>(x => x.SetUser(It.Is<OmbiUser>(x => x.Id == "abc")), Times.Once);
_mocker.Verify<IExternalRepository<PlexWatchlistHistory>>(x => x.GetAll(), Times.Once);
_mocker.Verify<IExternalRepository<PlexWatchlistHistory>>(x => x.Add(It.IsAny<PlexWatchlistHistory>()), Times.Never);
_mocker.Verify<IExternalRepository<PlexWatchlistHistory>>(x => x.Add(It.IsAny<PlexWatchlistHistory>()), Times.Once);
}
[Test]
@ -316,7 +316,7 @@ namespace Ombi.Schedule.Tests
_mocker.Verify<IPlexApi>(x => x.GetWatchlistMetadata("abc", It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once);
_mocker.Verify<ITvRequestEngine>(x => x.SetUser(It.Is<OmbiUser>(x => x.Id == "abc")), Times.Once);
_mocker.Verify<IExternalRepository<PlexWatchlistHistory>>(x => x.GetAll(), Times.Once);
_mocker.Verify<IExternalRepository<PlexWatchlistHistory>>(x => x.Add(It.IsAny<PlexWatchlistHistory>()), Times.Never);
_mocker.Verify<IExternalRepository<PlexWatchlistHistory>>(x => x.Add(It.IsAny<PlexWatchlistHistory>()), Times.Once);
}
[Test]

@ -124,24 +124,19 @@ namespace Ombi.Schedule.Jobs.Plex
if (response.ErrorCode == ErrorCode.AlreadyRequested)
{
_logger.LogDebug($"Movie already requested for user '{user.UserName}'");
await AddToHistory(theMovieDbId);
return;
}
_logger.LogInformation($"Error adding title from PlexWatchlist for user '{user.UserName}'. Message: '{response.ErrorMessage}'");
}
else
{
// Add to the watchlist history
var history = new PlexWatchlistHistory
{
TmdbId = theMovieDbId.ToString()
};
await _watchlistRepo.Add(history);
await AddToHistory(theMovieDbId);
_logger.LogInformation($"Added title from PlexWatchlist for user '{user.UserName}'. {response.Message}");
}
}
private async Task ProcessShow(int theMovieDbId, OmbiUser user)
{
_tvRequestEngine.SetUser(user);
@ -151,21 +146,27 @@ namespace Ombi.Schedule.Jobs.Plex
if (response.ErrorCode == ErrorCode.AlreadyRequested)
{
_logger.LogDebug($"Show already requested for user '{user.UserName}'");
await AddToHistory(theMovieDbId);
return;
}
_logger.LogInformation($"Error adding title from PlexWatchlist for user '{user.UserName}'. Message: '{response.ErrorMessage}'");
}
else
{
// Add to the watchlist history
var history = new PlexWatchlistHistory
{
TmdbId = theMovieDbId.ToString()
};
await _watchlistRepo.Add(history);
await AddToHistory(theMovieDbId);
_logger.LogInformation($"Added title from PlexWatchlist for user '{user.UserName}'. {response.Message}");
}
}
private async Task AddToHistory(int theMovieDbId)
{
// Add to the watchlist history
var history = new PlexWatchlistHistory
{
TmdbId = theMovieDbId.ToString()
};
await _watchlistRepo.Add(history);
}
private async Task<ProviderId> GetProviderIds(string authToken, Metadata movie, CancellationToken cancellationToken)
{

Loading…
Cancel
Save