diff --git a/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs b/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs index 5d17f771d..c7e570399 100644 --- a/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs +++ b/PlexRequests.Api.Interfaces/ICouchPotatoApi.cs @@ -38,6 +38,6 @@ namespace PlexRequests.Api.Interfaces CouchPotatoProfiles GetProfiles(Uri url, string apiKey); CouchPotatoMovies GetMovies(Uri baseUrl, string apiKey, string[] status); - CoucPotatoApiKey GetApiKey(Uri baseUrl, string username, string password); + CouchPotatoApiKey GetApiKey(Uri baseUrl, string username, string password); } } \ No newline at end of file diff --git a/PlexRequests.Api.Models/Movie/CoucPotatoApiKey.cs b/PlexRequests.Api.Models/Movie/CouchPotatoApiKey.cs similarity index 93% rename from PlexRequests.Api.Models/Movie/CoucPotatoApiKey.cs rename to PlexRequests.Api.Models/Movie/CouchPotatoApiKey.cs index 369d438c3..54305fda6 100644 --- a/PlexRequests.Api.Models/Movie/CoucPotatoApiKey.cs +++ b/PlexRequests.Api.Models/Movie/CouchPotatoApiKey.cs @@ -1,7 +1,7 @@ #region Copyright // /************************************************************************ // Copyright (c) 2016 Jamie Rees -// File: CoucPotatoApiKey.cs +// File: CouchPotatoApiKey.cs // Created By: Jamie Rees // // Permission is hereby granted, free of charge, to any person obtaining @@ -28,10 +28,10 @@ using Newtonsoft.Json; namespace PlexRequests.Api.Models.Movie { - public class CoucPotatoApiKey + public class CouchPotatoApiKey { [JsonProperty("success")] - public bool Result { get; set; } + public bool success { get; set; } [JsonProperty("api_key")] public string ApiKey { get; set; } } diff --git a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj index 10d5aa1e8..2694cb2bf 100644 --- a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj +++ b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj @@ -49,7 +49,7 @@ - + diff --git a/PlexRequests.Api/CouchPotatoApi.cs b/PlexRequests.Api/CouchPotatoApi.cs index f9f886fbb..e4382ff22 100644 --- a/PlexRequests.Api/CouchPotatoApi.cs +++ b/PlexRequests.Api/CouchPotatoApi.cs @@ -160,18 +160,18 @@ namespace PlexRequests.Api } } - public CoucPotatoApiKey GetApiKey(Uri baseUrl, string username, string password) + public CouchPotatoApiKey GetApiKey(Uri baseUrl, string username, string password) { var request = new RestRequest { - Resource = "getkey/?p={username}&u={password}", + Resource = "getkey/?u={username}&p={password}", Method = Method.GET }; request.AddUrlSegment("username", StringHasher.CalcuateMd5Hash(username)); request.AddUrlSegment("password", StringHasher.CalcuateMd5Hash(password)); - var obj = RetryHandler.Execute(() => Api.Execute(request, baseUrl), + var obj = RetryHandler.Execute(() => Api.Execute(request, baseUrl), (exception, timespan) => Log.Error(exception, "Exception when calling GetApiKey for CP, Retrying {0}", timespan), null); return obj; diff --git a/PlexRequests.Services/Jobs/IRecentlyAdded.cs b/PlexRequests.Services/Jobs/IRecentlyAdded.cs new file mode 100644 index 000000000..f557d7893 --- /dev/null +++ b/PlexRequests.Services/Jobs/IRecentlyAdded.cs @@ -0,0 +1,10 @@ +using Quartz; + +namespace PlexRequests.Services.Jobs +{ + public interface IRecentlyAdded + { + void Execute(IJobExecutionContext context); + void Test(); + } +} \ No newline at end of file diff --git a/PlexRequests.Services/Jobs/RecentlyAdded.cs b/PlexRequests.Services/Jobs/RecentlyAdded.cs index 60fe31d88..8e12f76c9 100644 --- a/PlexRequests.Services/Jobs/RecentlyAdded.cs +++ b/PlexRequests.Services/Jobs/RecentlyAdded.cs @@ -45,7 +45,7 @@ using Quartz; namespace PlexRequests.Services.Jobs { - public class RecentlyAdded : IJob + public class RecentlyAdded : IJob, IRecentlyAdded { public RecentlyAdded(IPlexApi api, ISettingsService plexSettings, ISettingsService email, ISettingsService scheduledService, IJobRecord rec) @@ -83,7 +83,7 @@ namespace PlexRequests.Services.Jobs return; } - Start(); + Start(); } catch (Exception e) { @@ -95,10 +95,16 @@ namespace PlexRequests.Services.Jobs } } - private void Start() + public void Test() + { + Start(true); + } + + private void Start(bool testEmail = false) { var sb = new StringBuilder(); var plexSettings = PlexSettings.GetSettings(); + var recentlyAdded = Api.RecentlyAdded(plexSettings.PlexAuthToken, plexSettings.FullUri); var movies = @@ -115,7 +121,7 @@ namespace PlexRequests.Services.Jobs var template = new RecentlyAddedTemplate(); var html = template.LoadTemplate(sb.ToString()); - Send(html, plexSettings); + Send(html, plexSettings, testEmail); } private void GenerateMovieHtml(IEnumerable movies, PlexSettings plexSettings, ref StringBuilder sb) @@ -194,10 +200,15 @@ namespace PlexRequests.Services.Jobs sb.Append(""); } - private void Send(string html, PlexSettings plexSettings) + private void Send(string html, PlexSettings plexSettings, bool testEmail = false) { - var users = Api.GetUsers(plexSettings.PlexAuthToken); var settings = EmailSettings.GetSettings(); + + if (!settings.Enabled || string.IsNullOrEmpty(settings.EmailHost)) + { + return; + } + var body = new BodyBuilder { HtmlBody = html, TextBody = "This email is only available on devices that support HTML." }; var message = new MimeMessage { @@ -205,11 +216,16 @@ namespace PlexRequests.Services.Jobs Subject = "New Content on Plex!", }; - foreach (var user in users.User) + if (!testEmail) { - message.Bcc.Add(new MailboxAddress(user.Username, user.Email)); + var users = Api.GetUsers(plexSettings.PlexAuthToken); + foreach (var user in users.User) + { + message.Bcc.Add(new MailboxAddress(user.Username, user.Email)); + } } - + message.Bcc.Add(new MailboxAddress(settings.EmailUsername, settings.EmailSender)); // Include the admin + message.From.Add(new MailboxAddress(settings.EmailUsername, settings.EmailSender)); try { diff --git a/PlexRequests.Services/PlexRequests.Services.csproj b/PlexRequests.Services/PlexRequests.Services.csproj index 9819e387f..6c0f8392b 100644 --- a/PlexRequests.Services/PlexRequests.Services.csproj +++ b/PlexRequests.Services/PlexRequests.Services.csproj @@ -80,6 +80,7 @@ + diff --git a/PlexRequests.UI.Tests/AdminModuleTests.cs b/PlexRequests.UI.Tests/AdminModuleTests.cs index 35eaf9311..f2e48b27c 100644 --- a/PlexRequests.UI.Tests/AdminModuleTests.cs +++ b/PlexRequests.UI.Tests/AdminModuleTests.cs @@ -48,6 +48,7 @@ using PlexRequests.UI.Models; using PlexRequests.UI.Modules; using PlexRequests.Helpers; using PlexRequests.Helpers.Analytics; +using PlexRequests.Services.Jobs; using PlexRequests.UI.Helpers; namespace PlexRequests.UI.Tests @@ -81,6 +82,7 @@ namespace PlexRequests.UI.Tests private Mock SlackApi { get; set; } private Mock Analytics { get; set; } private Mock> NotifyV2 { get; set; } + private Mock RecentlyAdded { get; set; } private ConfigurableBootstrapper Bootstrapper { get; set; } @@ -122,6 +124,7 @@ namespace PlexRequests.UI.Tests RecorderMock = new Mock(); Analytics = new Mock(); NotifyV2= new Mock>(); + RecentlyAdded = new Mock(); Bootstrapper = new ConfigurableBootstrapper(with => @@ -153,6 +156,7 @@ namespace PlexRequests.UI.Tests with.Dependency(SlackSettings.Object); with.Dependency(ScheduledJobsSettingsMock.Object); with.Dependency(RecorderMock.Object); + with.Dependency(RecentlyAdded.Object); with.RootPathProvider(); with.RequestStartup((container, pipelines, context) => { diff --git a/PlexRequests.UI/Modules/AdminModule.cs b/PlexRequests.UI/Modules/AdminModule.cs index 5b093efa3..fba7e8a95 100644 --- a/PlexRequests.UI/Modules/AdminModule.cs +++ b/PlexRequests.UI/Modules/AdminModule.cs @@ -57,6 +57,7 @@ using PlexRequests.Helpers; using PlexRequests.Helpers.Analytics; using PlexRequests.Helpers.Exceptions; using PlexRequests.Services.Interfaces; +using PlexRequests.Services.Jobs; using PlexRequests.Services.Notification; using PlexRequests.Store.Models; using PlexRequests.Store.Repository; @@ -94,6 +95,7 @@ namespace PlexRequests.UI.Modules private ISlackApi SlackApi { get; } private IJobRecord JobRecorder { get; } private IAnalytics Analytics { get; } + private IRecentlyAdded RecentlyAdded { get; } private ISettingsService NotifySettings { get; } private static Logger Log = LogManager.GetCurrentClassLogger(); @@ -118,7 +120,7 @@ namespace PlexRequests.UI.Modules ICacheProvider cache, ISettingsService slackSettings, ISlackApi slackApi, ISettingsService lp, ISettingsService scheduler, IJobRecord rec, IAnalytics analytics, - ISettingsService notifyService) : base("admin", prService) + ISettingsService notifyService, IRecentlyAdded recentlyAdded) : base("admin", prService) { PrService = prService; CpService = cpService; @@ -146,6 +148,7 @@ namespace PlexRequests.UI.Modules JobRecorder = rec; Analytics = analytics; NotifySettings = notifyService; + RecentlyAdded = recentlyAdded; this.RequiresClaims(UserClaims.Admin); @@ -216,6 +219,8 @@ namespace PlexRequests.UI.Modules Get["/notificationsettings", true] = async (x, ct) => await NotificationSettings(); Post["/notificationsettings", true] = async (x, ct) => await SaveNotificationSettings(); + + Post["/recentlyAddedTest"] = x => RecentlyAddedTest(); } private async Task Authentication() @@ -985,5 +990,19 @@ namespace PlexRequests.UI.Modules var model = this.Bind(); return View["NotificationSettings", model]; } + + private Response RecentlyAddedTest() + { + try + { + RecentlyAdded.Test(); + return Response.AsJson(new JsonResponseModel { Result = true, Message = "Sent email to administrator" }); + } + catch (Exception e) + { + + return Response.AsJson(new JsonResponseModel { Result = false, Message = e.Message }); + } + } } } \ No newline at end of file diff --git a/PlexRequests.UI/NinjectModules/ServicesModule.cs b/PlexRequests.UI/NinjectModules/ServicesModule.cs index e2ce94dd7..a180335d6 100644 --- a/PlexRequests.UI/NinjectModules/ServicesModule.cs +++ b/PlexRequests.UI/NinjectModules/ServicesModule.cs @@ -45,6 +45,7 @@ namespace PlexRequests.UI.NinjectModules Bind().To(); Bind().To(); Bind().To(); + Bind().To(); Bind().To(); Bind().To(); diff --git a/PlexRequests.UI/Views/Admin/CouchPotato.cshtml b/PlexRequests.UI/Views/Admin/CouchPotato.cshtml index f4f33cbab..176ea202c 100644 --- a/PlexRequests.UI/Views/Admin/CouchPotato.cshtml +++ b/PlexRequests.UI/Views/Admin/CouchPotato.cshtml @@ -160,6 +160,7 @@ $('#requestToken').click(function (e) { e.preventDefault(); + debugger; var $form = $("#mainForm"); $.ajax({ type: $form.prop("method"), @@ -167,7 +168,7 @@ data: $form.serialize(), dataType: "json", success: function (response) { - if (response.result === true) { + if (response.apiKey) { generateNotify("Success!", "success"); $('#ApiKey').val(response.apiKey); } else { diff --git a/PlexRequests.UI/Views/Admin/Settings.cshtml b/PlexRequests.UI/Views/Admin/Settings.cshtml index 3e02c1ae4..13c6431bd 100644 --- a/PlexRequests.UI/Views/Admin/Settings.cshtml +++ b/PlexRequests.UI/Views/Admin/Settings.cshtml @@ -76,21 +76,22 @@ - - + + - Note: This will require you to setup your email notifications - @if (Model.SendRecentlyAddedEmail) - { - Send out a weekly email of recently added content to all your Plex 'Friends' - } - else - { - Send out a weekly email of recently added content to all your Plex 'Friends' - } + Note: This will require you to setup your email notifications + @if (Model.SendRecentlyAddedEmail) + { + Send out a weekly email of recently added content to all your Plex 'Friends' + } + else + { + Send out a weekly email of recently added content to all your Plex 'Friends' + } - + + Send test email to Admin @@ -372,5 +373,29 @@ } }); }); + + $('#recentlyAddedBtn').click(function (e) { + e.preventDefault(); + var base = '@Html.GetBaseUrl()'; + var url = createBaseUrl(base, '/admin/recentlyAddedTest'); + + $.ajax({ + type: "post", + url: url, + dataType: "json", + success: function (response) { + if (response) { + generateNotify(response.message, "success"); + } else { + + generateNotify(response.message, "danger"); + } + }, + error: function (e) { + console.log(e); + generateNotify("Something went wrong!", "danger"); + } + }); + }); }); \ No newline at end of file