diff --git a/src/Ombi.Schedule.Tests/NewsletterTests.cs b/src/Ombi.Schedule.Tests/NewsletterTests.cs index fcbd35107..b3c2ce98a 100644 --- a/src/Ombi.Schedule.Tests/NewsletterTests.cs +++ b/src/Ombi.Schedule.Tests/NewsletterTests.cs @@ -18,7 +18,7 @@ namespace Ombi.Schedule.Tests var emailSettings = new Mock>(); var customziation = new Mock>(); var newsletterSettings = new Mock>(); - var newsletter = new NewsletterJob(null, null, null, null, null, null, customziation.Object, emailSettings.Object, null, null, newsletterSettings.Object, null); + var newsletter = new NewsletterJob(null, null, null, null, null, null, customziation.Object, emailSettings.Object, null, null, newsletterSettings.Object, null, null, null, null); var ep = new List(); foreach (var i in episodes) diff --git a/src/Ombi.Store/Entities/UserNotificationPreferences.cs b/src/Ombi.Store/Entities/UserNotificationPreferences.cs new file mode 100644 index 000000000..c779480c8 --- /dev/null +++ b/src/Ombi.Store/Entities/UserNotificationPreferences.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Text; +using Ombi.Helpers; + +namespace Ombi.Store.Entities +{ + [Table(nameof(UserNotificationPreferences))] + public class UserNotificationPreferences : Entity + { + public string UserId { get; set; } + public NotificationAgent Agent { get; set; } + public bool Enabled { get; set; } + public string Value { get; set; } + + [ForeignKey(nameof(UserId))] + public OmbiUser User { get; set; } + } +} diff --git a/src/Ombi/Controllers/IdentityController.cs b/src/Ombi/Controllers/IdentityController.cs index 742d75b34..d7e556f26 100644 --- a/src/Ombi/Controllers/IdentityController.cs +++ b/src/Ombi/Controllers/IdentityController.cs @@ -60,7 +60,8 @@ namespace Ombi.Controllers IRepository issueComments, IRepository notificationRepository, IRepository subscriptionRepository, - ISettingsService umSettings) + ISettingsService umSettings, + IRepository notificationPreferences) { UserManager = user; Mapper = mapper; @@ -81,6 +82,7 @@ namespace Ombi.Controllers _requestSubscriptionRepository = subscriptionRepository; _notificationRepository = notificationRepository; _userManagementSettings = umSettings; + _userNotificationPreferences = notificationPreferences; } private OmbiUserManager UserManager { get; } @@ -102,6 +104,7 @@ namespace Ombi.Controllers private readonly IRepository _requestLogRepository; private readonly IRepository _notificationRepository; private readonly IRepository _requestSubscriptionRepository; + private readonly IRepository _userNotificationPreferences; /// @@ -787,6 +790,30 @@ namespace Ombi.Controllers return user.UserAccessToken; } + [HttpGet("notificationpreferences")] + public async Task> GetUserPreferences() + { + //TODO potentially use a view model + var user = await UserManager.Users.FirstOrDefaultAsync(x => x.UserName == User.Identity.Name); + var userPreferences = await _userNotificationPreferences.GetAll().Where(x => x.UserId == user.Id).ToListAsync(); + + var agents = Enum.GetValues(typeof(NotificationAgent)).Cast(); + foreach (var a in agents) + { + var hasAgent = userPreferences.Any(x => x.Agent == a); + if (!hasAgent) + { + // Create the default + userPreferences.Add(new UserNotificationPreferences + { + Agent = a, + }); + } + } + + return userPreferences; + } + private async Task> AddRoles(IEnumerable roles, OmbiUser ombiUser) { var roleResult = new List();