diff --git a/src/Ombi.Core/Engine/RecentlyAddedEngine.cs b/src/Ombi.Core/Engine/RecentlyAddedEngine.cs index cc6c9273a..59be359f8 100644 --- a/src/Ombi.Core/Engine/RecentlyAddedEngine.cs +++ b/src/Ombi.Core/Engine/RecentlyAddedEngine.cs @@ -69,7 +69,8 @@ namespace Ombi.Core.Engine { AddedAt = DateTime.Now, Type = RecentlyAddedType.Plex, - ContentId = p.Id + ContentId = p.Id, + ContentType = ContentType.Parent }); } else @@ -81,7 +82,8 @@ namespace Ombi.Core.Engine { AddedAt = DateTime.Now, Type = RecentlyAddedType.Plex, - ContentId = ep.Id + ContentId = ep.Id, + ContentType = ContentType.Episode }); } } @@ -95,7 +97,8 @@ namespace Ombi.Core.Engine { AddedAt = DateTime.Now, Type = RecentlyAddedType.Emby, - ContentId = e.Id + ContentId = e.Id, + ContentType = ContentType.Parent }); } else @@ -106,8 +109,9 @@ namespace Ombi.Core.Engine recentlyAddedLog.Add(new RecentlyAddedLog { AddedAt = DateTime.Now, - Type = RecentlyAddedType.Plex, - ContentId = ep.Id + Type = RecentlyAddedType.Emby, + ContentId = ep.Id, + ContentType = ContentType.Episode }); } } diff --git a/src/Ombi.Schedule/Jobs/Ombi/INewsletterJob.cs b/src/Ombi.Schedule/Jobs/Ombi/INewsletterJob.cs index e5e587bec..887508d34 100644 --- a/src/Ombi.Schedule/Jobs/Ombi/INewsletterJob.cs +++ b/src/Ombi.Schedule/Jobs/Ombi/INewsletterJob.cs @@ -1,9 +1,11 @@ using System.Threading.Tasks; +using Ombi.Settings.Settings.Models.Notifications; namespace Ombi.Schedule.Jobs.Ombi { public interface INewsletterJob : IBaseJob { Task Start(); + Task Start(NewsletterSettings settings, bool test); } } \ No newline at end of file diff --git a/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs b/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs index 693c8ae7a..9f303b906 100644 --- a/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs +++ b/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs @@ -38,6 +38,9 @@ namespace Ombi.Schedule.Jobs.Ombi _emailSettings = emailSettings; _newsletterSettings = newsletter; _userManager = um; + _emailSettings.ClearCache(); + _customizationSettings.ClearCache(); + _newsletterSettings.ClearCache(); } private readonly IPlexContentRepository _plex; @@ -52,10 +55,9 @@ namespace Ombi.Schedule.Jobs.Ombi private readonly ISettingsService _newsletterSettings; private readonly UserManager _userManager; - public async Task Start() + public async Task Start(NewsletterSettings settings, bool test) { - var newsletterSettings = await _newsletterSettings.GetSettingsAsync(); - if (!newsletterSettings.Enabled) + if (!settings.Enabled) { return; } @@ -85,17 +87,33 @@ namespace Ombi.Schedule.Jobs.Ombi var addedEmbyEpisodesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Episode).Select(x => x.ContentId); // Filter out the ones that we haven't sent yet - var plexContentMoviesToSend = plexContent.Where(x => !addedPlexMovieLogIds.Contains(x.Id)); - var embyContentMoviesToSend = embyContent.Where(x => !addedEmbyMoviesLogIds.Contains(x.Id)); + var plexContentMoviesToSend = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie && !addedPlexMovieLogIds.Contains(x.Id)); + var embyContentMoviesToSend = embyContent.Where(x => x.Type == EmbyMediaType.Movie && !addedEmbyMoviesLogIds.Contains(x.Id)); - var plexContentTvToSend = plexContent.Where(x => x.Episodes.Any(e => !addedPlexEpisodesLogIds.Contains(e.Id))); - var embyContentTvToSend = embyContent.Where(x => x.Episodes.Any(e => !addedEmbyEpisodesLogIds.Contains(e.Id))); + var plexContentTvToSend = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Show && x.Episodes.Any(e => !addedPlexEpisodesLogIds.Contains(e.Id))); + var embyContentTvToSend = embyContent.Where(x => x.Type == EmbyMediaType.Series && x.Episodes.Any(e => !addedEmbyEpisodesLogIds.Contains(e.Id))); var plexContentToSend = plexContentMoviesToSend.Union(plexContentTvToSend); var embyContentToSend = embyContentMoviesToSend.Union(embyContentTvToSend); + var body = string.Empty; + if (test) + { + var plexm = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie).OrderByDescending(x => x.AddedAt).Take(10); + var embym = embyContent.Where(x => x.Type == EmbyMediaType.Movie).OrderByDescending(x => x.AddedAt).Take(10); + var plext = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Show).OrderByDescending(x => x.AddedAt).Take(10); + var embyt = embyContent.Where(x => x.Type == EmbyMediaType.Series).OrderByDescending(x => x.AddedAt).Take(10); + body = await BuildHtml(plexm.Union(plext), embym.Union(embyt)); + } + else + { + body = await BuildHtml(plexContentToSend, embyContentToSend); + if (body.IsNullOrEmpty()) + { + return; + } - var body = await BuildHtml(plexContentToSend, embyContentToSend); + } // Get the users to send it to var users = await _userManager.GetUsersInRoleAsync(OmbiRoles.RecievesNewsletter); @@ -174,6 +192,12 @@ namespace Ombi.Schedule.Jobs.Ombi await Task.WhenAll(emailTasks.ToArray()); } + public async Task Start() + { + var newsletterSettings = await _newsletterSettings.GetSettingsAsync(); + await Start(newsletterSettings, false); + } + private string LoadTemplate(string body, NotificationTemplates template, CustomizationSettings settings, string username) { var email = new NewsletterTemplate(); diff --git a/src/Ombi.Store/Migrations/20180322085345_RecentlyAddedLog.Designer.cs b/src/Ombi.Store/Migrations/20180322204610_RecentlyAddedLog.Designer.cs similarity index 99% rename from src/Ombi.Store/Migrations/20180322085345_RecentlyAddedLog.Designer.cs rename to src/Ombi.Store/Migrations/20180322204610_RecentlyAddedLog.Designer.cs index 23f23bc4b..fb493660a 100644 --- a/src/Ombi.Store/Migrations/20180322085345_RecentlyAddedLog.Designer.cs +++ b/src/Ombi.Store/Migrations/20180322204610_RecentlyAddedLog.Designer.cs @@ -14,7 +14,7 @@ using System; namespace Ombi.Store.Migrations { [DbContext(typeof(OmbiContext))] - [Migration("20180322085345_RecentlyAddedLog")] + [Migration("20180322204610_RecentlyAddedLog")] partial class RecentlyAddedLog { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -440,6 +440,8 @@ namespace Ombi.Store.Migrations b.Property("ContentId"); + b.Property("ContentType"); + b.Property("Type"); b.HasKey("Id"); diff --git a/src/Ombi.Store/Migrations/20180322085345_RecentlyAddedLog.cs b/src/Ombi.Store/Migrations/20180322204610_RecentlyAddedLog.cs similarity index 94% rename from src/Ombi.Store/Migrations/20180322085345_RecentlyAddedLog.cs rename to src/Ombi.Store/Migrations/20180322204610_RecentlyAddedLog.cs index 0e815a06f..bc51af276 100644 --- a/src/Ombi.Store/Migrations/20180322085345_RecentlyAddedLog.cs +++ b/src/Ombi.Store/Migrations/20180322204610_RecentlyAddedLog.cs @@ -16,6 +16,7 @@ namespace Ombi.Store.Migrations .Annotation("Sqlite:Autoincrement", true), AddedAt = table.Column(nullable: false), ContentId = table.Column(nullable: false), + ContentType = table.Column(nullable: false), Type = table.Column(nullable: false) }, constraints: table => diff --git a/src/Ombi.Store/Migrations/OmbiContextModelSnapshot.cs b/src/Ombi.Store/Migrations/OmbiContextModelSnapshot.cs index 7571c6f11..a24aa583a 100644 --- a/src/Ombi.Store/Migrations/OmbiContextModelSnapshot.cs +++ b/src/Ombi.Store/Migrations/OmbiContextModelSnapshot.cs @@ -439,6 +439,8 @@ namespace Ombi.Store.Migrations b.Property("ContentId"); + b.Property("ContentType"); + b.Property("Type"); b.HasKey("Id"); diff --git a/src/Ombi.Store/Repository/NotificationTemplatesRepository.cs b/src/Ombi.Store/Repository/NotificationTemplatesRepository.cs index 3822f8001..175d0e6a9 100644 --- a/src/Ombi.Store/Repository/NotificationTemplatesRepository.cs +++ b/src/Ombi.Store/Repository/NotificationTemplatesRepository.cs @@ -40,6 +40,11 @@ namespace Ombi.Store.Repository public async Task Update(NotificationTemplates template) { + if (Db.Entry(template).State == EntityState.Detached) + { + Db.Attach(template); + Db.Entry(template).State = EntityState.Modified; + } await Db.SaveChangesAsync(); } diff --git a/src/Ombi/ClientApp/app/recentlyAdded/recentlyAdded.component.ts b/src/Ombi/ClientApp/app/recentlyAdded/recentlyAdded.component.ts index c5c09f814..53d652070 100644 --- a/src/Ombi/ClientApp/app/recentlyAdded/recentlyAdded.component.ts +++ b/src/Ombi/ClientApp/app/recentlyAdded/recentlyAdded.component.ts @@ -40,7 +40,7 @@ export class RecentlyAddedComponent implements OnInit { public tv: IRecentlyAddedTvShows[]; public range: Date[]; - public groupTv: boolean; + public groupTv: boolean = false; // https://github.com/sheikalthaf/ngu-carousel public carouselTile: NguCarousel; @@ -76,7 +76,7 @@ export class RecentlyAddedComponent implements OnInit { } this.getMovies(); } - + public change() { this.getShows(); } diff --git a/src/Ombi/ClientApp/app/services/applications/tester.service.ts b/src/Ombi/ClientApp/app/services/applications/tester.service.ts index 9122eb0b9..bab94d32f 100644 --- a/src/Ombi/ClientApp/app/services/applications/tester.service.ts +++ b/src/Ombi/ClientApp/app/services/applications/tester.service.ts @@ -12,6 +12,7 @@ import { IEmailNotificationSettings, IEmbyServer, IMattermostNotifcationSettings, + INewsletterNotificationSettings, IPlexServer, IPushbulletNotificationSettings, IPushoverNotificationSettings, @@ -77,5 +78,8 @@ export class TesterService extends ServiceHelpers { public sickrageTest(settings: ISickRageSettings): Observable { return this.http.post(`${this.url}sickrage`, JSON.stringify(settings), {headers: this.headers}); + } + public newsletterTest(settings: INewsletterNotificationSettings): Observable { + return this.http.post(`${this.url}newsletter`, JSON.stringify(settings), {headers: this.headers}); } } diff --git a/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.html b/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.html index 5f663dd20..1be27b578 100644 --- a/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.html +++ b/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.html @@ -1,47 +1,37 @@  -
+
Newsletter
-
- -
-
- - -
-
-
- - +
-
+
- +
- +
- - + + +
-
diff --git a/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.ts b/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.ts index 7742e6f4e..4e254688e 100644 --- a/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.ts +++ b/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.ts @@ -1,8 +1,7 @@ -import { Component, OnInit } from "@angular/core"; -import { FormBuilder, FormGroup } from "@angular/forms"; +import { TesterService } from './../../services/applications/tester.service'; +import { Component, OnInit } from "@angular/core"; -import { INewsletterNotificationSettings, INotificationTemplates, NotificationType } from "../../interfaces"; -import { TesterService } from "../../services"; +import { INewsletterNotificationSettings, NotificationType } from "../../interfaces"; import { NotificationService } from "../../services"; import { SettingsService } from "../../services"; @@ -12,21 +11,15 @@ import { SettingsService } from "../../services"; export class NewsletterComponent implements OnInit { public NotificationType = NotificationType; - public template: INotificationTemplates; - public form: FormGroup; + public settings: INewsletterNotificationSettings; constructor(private settingsService: SettingsService, private notificationService: NotificationService, - private fb: FormBuilder, - private testerService: TesterService) { } + private testService: TesterService) { } public ngOnInit() { this.settingsService.getNewsletterSettings().subscribe(x => { - this.template = x.notificationTemplate; - - this.form = this.fb.group({ - enabled: [x.enabled], - }); + this.settings = x; }); } @@ -34,16 +27,12 @@ export class NewsletterComponent implements OnInit { this.settingsService.updateNewsletterDatabase().subscribe(); } - public onSubmit(form: FormGroup) { - if (form.invalid) { - this.notificationService.error("Please check your entered values"); - return; - } - - const settings = form.value; - settings.notificationTemplate = this.template; + public test() { + this.testService.testNewsletter(this.settings).subscribe(); + } - this.settingsService.saveNewsletterSettings(settings).subscribe(x => { + public onSubmit() { + this.settingsService.saveNewsletterSettings(this.settings).subscribe(x => { if (x) { this.notificationService.success("Successfully saved the Newsletter settings"); } else { @@ -52,20 +41,4 @@ export class NewsletterComponent implements OnInit { }); } - - public test(form: FormGroup) { - if (form.invalid) { - this.notificationService.error("Please check your entered values"); - return; - } - - this.testerService.discordTest(form.value).subscribe(x => { - if (x) { - this.notificationService.success("Successfully sent a Discord message, please check the discord channel"); - } else { - this.notificationService.error("There was an error when sending the Discord message. Please check your settings"); - } - }); - - } } diff --git a/src/Ombi/Controllers/External/TesterController.cs b/src/Ombi/Controllers/External/TesterController.cs index 013798e13..bad933e86 100644 --- a/src/Ombi/Controllers/External/TesterController.cs +++ b/src/Ombi/Controllers/External/TesterController.cs @@ -10,12 +10,14 @@ using Ombi.Api.Radarr; using Ombi.Api.SickRage; using Ombi.Api.Sonarr; using Ombi.Attributes; +using Ombi.Core.Models.UI; using Ombi.Core.Notifications; using Ombi.Core.Settings.Models.External; using Ombi.Helpers; using Ombi.Notifications; using Ombi.Notifications.Agents; using Ombi.Notifications.Models; +using Ombi.Schedule.Jobs.Ombi; using Ombi.Settings.Settings.Models.External; using Ombi.Settings.Settings.Models.Notifications; @@ -35,7 +37,7 @@ namespace Ombi.Controllers.External public TesterController(INotificationService service, IDiscordNotification notification, IEmailNotification emailN, IPushbulletNotification pushbullet, ISlackNotification slack, IPushoverNotification po, IMattermostNotification mm, IPlexApi plex, IEmbyApi emby, IRadarrApi radarr, ISonarrApi sonarr, ILogger log, IEmailProvider provider, - ICouchPotatoApi cpApi, ITelegramNotification telegram, ISickRageApi srApi) + ICouchPotatoApi cpApi, ITelegramNotification telegram, ISickRageApi srApi, INewsletterJob newsletter) { Service = service; DiscordNotification = notification; @@ -53,6 +55,7 @@ namespace Ombi.Controllers.External CouchPotatoApi = cpApi; TelegramNotification = telegram; SickRageApi = srApi; + Newsletter = newsletter; } private INotificationService Service { get; } @@ -71,6 +74,7 @@ namespace Ombi.Controllers.External private IEmailProvider EmailProvider { get; } private ITelegramNotification TelegramNotification { get; } private ISickRageApi SickRageApi { get; } + private INewsletterJob Newsletter { get; } /// @@ -368,5 +372,21 @@ namespace Ombi.Controllers.External return false; } } + + [HttpPost("newsletter")] + public async Task NewsletterTest([FromBody] NewsletterNotificationViewModel settings) + { + try + { + settings.Enabled = true; + await Newsletter.Start(settings, true); + return true; + } + catch (Exception e) + { + Log.LogError(LoggingEvents.Api, e, "Could not test Newsletter"); + return false; + } + } } } \ No newline at end of file diff --git a/src/Ombi/Controllers/SettingsController.cs b/src/Ombi/Controllers/SettingsController.cs index de2a87317..9ffa9d81f 100644 --- a/src/Ombi/Controllers/SettingsController.cs +++ b/src/Ombi/Controllers/SettingsController.cs @@ -71,6 +71,7 @@ namespace Ombi.Controllers _radarrSync = radarrSync; _cache = memCache; _githubApi = githubApi; + _recentlyAdded = engine; } private ISettingsResolver SettingsResolver { get; }