Skip to content

Commit

Permalink
Merge pull request #511 from tidusjar/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Jamie authored Aug 30, 2016
2 parents 6556a1d + 8df742a commit 3917ebc
Show file tree
Hide file tree
Showing 53 changed files with 1,533 additions and 403 deletions.
1 change: 1 addition & 0 deletions PlexRequests.Api/ApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class ApiRequest : IApiRequest
Log.Trace("Api Content Response:");
Log.Trace(response.Content);


if (response.ErrorException != null)
{
var message = "Error retrieving response. Check inner details for more info.";
Expand Down
1 change: 1 addition & 0 deletions PlexRequests.Core/PlexRequests.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="SettingModels\HeadphonesSettings.cs" />
<Compile Include="SettingModels\LandingPageSettings.cs" />
<Compile Include="SettingModels\NotificationSettings.cs" />
<Compile Include="SettingModels\RequestSettings.cs" />
<Compile Include="SettingModels\ScheduledJobsSettings.cs" />
<Compile Include="SettingModels\SlackNotificationSettings.cs" />
<Compile Include="SettingModels\PushoverNotificationSettings.cs" />
Expand Down
4 changes: 4 additions & 0 deletions PlexRequests.Core/SettingModels/PlexSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion

using Newtonsoft.Json;

namespace PlexRequests.Core.SettingModels
{
public sealed class PlexSettings : ExternalSettings
Expand All @@ -36,5 +39,6 @@ public PlexSettings()
public bool EnableTvEpisodeSearching { get; set; }

public string PlexAuthToken { get; set; }
public string MachineIdentifier { get; set; }
}
}
57 changes: 57 additions & 0 deletions PlexRequests.Core/SettingModels/RequestSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: RequestSettings.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion

using System;
using System.Collections.Generic;

namespace PlexRequests.Core.SettingModels
{
public sealed class RequestSettings : Settings
{
public OrderType Order { get; set; }
public List<FilterType> Filters { get; set; }
}

public enum OrderType
{
NewRequests,
OldRequests,
NewReleases,
OldReleases
}

public enum FilterType
{
// ALL is not here, it's managed in the angular controller
Approved,
NotApproved,
Available,
NotAvailable,
Released,
NotReleased
}
}
31 changes: 31 additions & 0 deletions PlexRequests.Core/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#endregion

using System;
using System.Linq;
using System.Text.RegularExpressions;

using Mono.Data.Sqlite;
Expand Down Expand Up @@ -66,6 +67,11 @@ public string SetupDb(string urlBase)
{
MigrateToVersion1900();
}

if(version > 1899 && version <= 1910)
{
MigrateToVersion1910();
}
}

return Db.DbConnection().ConnectionString;
Expand Down Expand Up @@ -244,5 +250,30 @@ public void MigrateToVersion1900()
Log.Error(e);
}
}

/// <summary>
/// Migrates to version1910.
/// </summary>
public void MigrateToVersion1910()
{
try
{
// Get the new machine Identifier
var settings = new SettingsServiceV2<PlexSettings>(new SettingsJsonRepository(Db, new MemoryCacheProvider()));
var plex = settings.GetSettings();
if (!string.IsNullOrEmpty(plex.PlexAuthToken))
{
var api = new PlexApi(new ApiRequest());
var server = api.GetServer(plex.PlexAuthToken); // Get the server info
plex.MachineIdentifier = server.Server.FirstOrDefault(x => x.AccessToken == plex.PlexAuthToken)?.MachineIdentifier;

settings.SaveSettings(plex); // Save the new settings
}
}
catch (Exception e)
{
Log.Error(e);
}
}
}
}
11 changes: 10 additions & 1 deletion PlexRequests.Core/UserMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public bool DoUsersExist()
return users.Any();
}

private Guid? CreateUser(string username, string password, string[] claims = default(string[]), UserProperties properties = null)
public Guid? CreateUser(string username, string password, string[] claims = default(string[]), UserProperties properties = null)
{
var salt = PasswordHasher.GenerateSalt();

Expand Down Expand Up @@ -134,6 +134,13 @@ public bool DoUsersExist()
return CreateUser(username, password, new[] { UserClaims.User }, properties);
}


public IEnumerable<string> GetAllClaims()
{
var properties = typeof(UserClaims).GetConstantsValues<string>();
return properties;
}

public bool UpdatePassword(string username, string oldPassword, string newPassword)
{
var users = Repo.GetAll();
Expand Down Expand Up @@ -175,6 +182,8 @@ public UsersModel GetUser(Guid userId)

public interface ICustomUserMapper
{
Guid? CreateUser(string username, string password, string[] claims, UserProperties props);
IEnumerable<string> GetAllClaims();
IEnumerable<UsersModel> GetUsers();
Task<IEnumerable<UsersModel>> GetUsersAsync();
UsersModel GetUser(Guid userId);
Expand Down
15 changes: 15 additions & 0 deletions PlexRequests.Helpers.Tests/PlexHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public int TitleToSeasonNumber(string title)
return PlexHelper.GetSeasonNumberFromTitle(title);
}

[TestCaseSource(nameof(MediaUrls))]
public string GetPlexMediaUrlTest(string machineId, string mediaId)
{
return PlexHelper.GetPlexMediaUrl(machineId, mediaId);
}

private static IEnumerable<TestCaseData> PlexGuids
{
get
Expand All @@ -75,6 +81,15 @@ private static IEnumerable<TestCaseData> PlexGuids
}
}

private static IEnumerable<TestCaseData> MediaUrls
{
get
{
yield return new TestCaseData("abcd","99").Returns("https://app.plex.tv/web/app#!/server/abcd/details/%2Flibrary%2Fmetadata%2F99").SetName("Test 1");
yield return new TestCaseData("a54d1db669799308cd704b791f331eca6648b952", "51").Returns("https://app.plex.tv/web/app#!/server/a54d1db669799308cd704b791f331eca6648b952/details/%2Flibrary%2Fmetadata%2F51").SetName("Test 2");
}
}

private static IEnumerable<TestCaseData> SeasonNumbers
{
get
Expand Down
10 changes: 9 additions & 1 deletion PlexRequests.Helpers.Tests/TypeHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#endregion
using System;
using System.Collections.Generic;

using System.Linq;
using NUnit.Framework;

using PlexRequests.Store;
Expand All @@ -42,6 +42,14 @@ public string[] FirstCharToUpperTest(Type input)
return input.GetPropertyNames();
}

[Test]
public void GetConstantsTest()
{
var consts = typeof(UserClaims).GetConstantsValues<string>();
Assert.That(consts.Contains("Admin"),Is.True);
Assert.That(consts.Contains("PowerUser"),Is.True);
Assert.That(consts.Contains("User"),Is.True);
}

private static IEnumerable<TestCaseData> TypeData
{
Expand Down
7 changes: 7 additions & 0 deletions PlexRequests.Helpers/PlexHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public static int GetSeasonNumberFromTitle(string title)

return 0;
}

public static string GetPlexMediaUrl(string machineId, string mediaId)
{
var url =
$"https://app.plex.tv/web/app#!/server/{machineId}/details/%2Flibrary%2Fmetadata%2F{mediaId}";
return url;
}
}

public class EpisodeModelHelper
Expand Down
16 changes: 16 additions & 0 deletions PlexRequests.Helpers/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace PlexRequests.Helpers
{
Expand All @@ -35,5 +37,19 @@ public static string[] GetPropertyNames(this Type t)
{
return t.GetProperties().Select(x => x.Name).ToArray();
}

public static IEnumerable<FieldInfo> GetConstants(this Type type)
{
var fieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

return fieldInfos.Where(fi => fi.IsLiteral && !fi.IsInitOnly);
}

public static IEnumerable<T> GetConstantsValues<T>(this Type type) where T : class
{
var fieldInfos = GetConstants(type);

return fieldInfos.Select(fi => fi.GetRawConstantValue() as T);
}
}
}
16 changes: 11 additions & 5 deletions PlexRequests.Services.Tests/PlexAvailabilityCheckerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
using PlexRequests.Helpers;
using PlexRequests.Services.Jobs;
using PlexRequests.Services.Models;
using PlexRequests.Services.Notification;
using PlexRequests.Store.Models;
using PlexRequests.Store.Repository;

Expand All @@ -63,6 +64,11 @@ public class PlexAvailabilityCheckerTests
private Mock<IJobRecord> JobRec { get; set; }
private Mock<IRepository<UsersToNotify>> NotifyUsers { get; set; }
private Mock<IRepository<PlexEpisodes>> PlexEpisodes { get; set; }
private Mock<INotificationEngine> Engine
{
get;
set;
}

[SetUp]
public void Setup()
Expand All @@ -76,7 +82,8 @@ public void Setup()
NotifyUsers = new Mock<IRepository<UsersToNotify>>();
PlexEpisodes = new Mock<IRepository<PlexEpisodes>>();
JobRec = new Mock<IJobRecord>();
Checker = new PlexAvailabilityChecker(SettingsMock.Object, RequestMock.Object, PlexMock.Object, CacheMock.Object, NotificationMock.Object, JobRec.Object, NotifyUsers.Object, PlexEpisodes.Object);
Engine = new Mock<INotificationEngine>();
Checker = new PlexAvailabilityChecker(SettingsMock.Object, RequestMock.Object, PlexMock.Object, CacheMock.Object, NotificationMock.Object, JobRec.Object, NotifyUsers.Object, PlexEpisodes.Object, Engine.Object);

}

Expand Down Expand Up @@ -212,8 +219,7 @@ public bool IsEpisodeAvailableTest(string providerId, int season, int episode)
new PlexEpisodes {EpisodeNumber = 1, ShowTitle = "The Flash",ProviderId = 23.ToString(), SeasonNumber = 1, EpisodeTitle = "Pilot"}
};
PlexEpisodes.Setup(x => x.Custom(It.IsAny<Func<IDbConnection, IEnumerable<PlexEpisodes>>>())).Returns(expected);
Checker = new PlexAvailabilityChecker(SettingsMock.Object, RequestMock.Object, PlexMock.Object, CacheMock.Object, NotificationMock.Object, JobRec.Object, NotifyUsers.Object, PlexEpisodes.Object);


var result = Checker.IsEpisodeAvailable(providerId, season, episode);

return result;
Expand Down Expand Up @@ -242,6 +248,7 @@ public void GetPlexMoviesTests()
}
});
CacheMock.Setup(x => x.Get<List<PlexSearch>>(CacheKeys.PlexLibaries)).Returns(cachedMovies);
SettingsMock.Setup(x => x.GetSettings()).Returns(F.Create<PlexSettings>());
var movies = Checker.GetPlexMovies();

Assert.That(movies.Any(x => x.ProviderId == "1212"));
Expand All @@ -258,6 +265,7 @@ public void GetPlexTvShowsTests()
new Directory1 {Type = "show", Title = "title1", Year = "2016", ProviderId = "1212", Seasons = new List<Directory1>()}
}
});
SettingsMock.Setup(x => x.GetSettings()).Returns(F.Create<PlexSettings>());
CacheMock.Setup(x => x.Get<List<PlexSearch>>(CacheKeys.PlexLibaries)).Returns(cachedTv);
var movies = Checker.GetPlexTvShows();

Expand All @@ -268,8 +276,6 @@ public void GetPlexTvShowsTests()
public async Task GetAllPlexEpisodes()
{
PlexEpisodes.Setup(x => x.GetAllAsync()).ReturnsAsync(F.CreateMany<PlexEpisodes>().ToList());
Checker = new PlexAvailabilityChecker(SettingsMock.Object, RequestMock.Object, PlexMock.Object, CacheMock.Object, NotificationMock.Object, JobRec.Object, NotifyUsers.Object, PlexEpisodes.Object);

var episodes = await Checker.GetEpisodes();

Assert.That(episodes.Count(), Is.GreaterThan(0));
Expand Down
3 changes: 3 additions & 0 deletions PlexRequests.Services/Interfaces/IAvailabilityChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public interface IAvailabilityChecker
List<PlexAlbum> GetPlexAlbums();
bool IsAlbumAvailable(PlexAlbum[] plexAlbums, string title, string year, string artist);
bool IsEpisodeAvailable(string theTvDbId, int season, int episode);
PlexAlbum GetAlbum(PlexAlbum[] plexAlbums, string title, string year, string artist);
PlexMovie GetMovie(PlexMovie[] plexMovies, string title, string year, string providerId = null);
PlexTvShow GetTvShow(PlexTvShow[] plexShows, string title, string year, string providerId = null, int[] seasons = null);
/// <summary>
/// Gets the episode's stored in the cache.
/// </summary>
Expand Down
39 changes: 39 additions & 0 deletions PlexRequests.Services/Interfaces/INotificationEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: INotificationEngine.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion

using System.Collections.Generic;
using System.Threading.Tasks;
using PlexRequests.Store;

namespace PlexRequests.Services.Interfaces
{
public interface INotificationEngine
{
Task NotifyUsers(IEnumerable<RequestedModel> modelChanged, string apiKey);
Task NotifyUsers(RequestedModel modelChanged, string apiKey);
}
}
Loading

0 comments on commit 3917ebc

Please sign in to comment.