Added the API to add user notification preferences

pull/2519/head
TidusJar 6 years ago
parent 2886f6e6fa
commit de2e3abfe0

@ -28,6 +28,7 @@ namespace Ombi.Store.Entities
public string UserAccessToken { get; set; }
public List<NotificationUserId> NotificationUserIds { get; set; }
public List<UserNotificationPreferences> UserNotificationPreferences { get; set; }
[NotMapped]
public bool IsEmbyConnect => UserType == UserType.EmbyUser && EmbyConnectUserId.HasValue();

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using Newtonsoft.Json;
using Ombi.Helpers;
namespace Ombi.Store.Entities
@ -15,6 +13,7 @@ namespace Ombi.Store.Entities
public string Value { get; set; }
[ForeignKey(nameof(UserId))]
[JsonIgnore]
public OmbiUser User { get; set; }
}
}

@ -793,15 +793,14 @@ namespace Ombi.Controllers
[HttpGet("notificationpreferences")]
public async Task<List<UserNotificationPreferences>> 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<NotificationAgent>();
foreach (var a in agents)
{
var hasAgent = userPreferences.Any(x => x.Agent == a);
if (!hasAgent)
var agent = userPreferences.FirstOrDefault(x => x.Agent == a);
if (agent == null)
{
// Create the default
userPreferences.Add(new UserNotificationPreferences
@ -809,11 +808,56 @@ namespace Ombi.Controllers
Agent = a,
});
}
else
{
userPreferences.Add(agent);
}
}
return userPreferences;
}
[HttpPost("NotificationPreferences")]
public async Task<IActionResult> AddUserNotificationPreference([FromBody] AddNotificationPreference pref)
{
// Make sure the user exists
var user = await UserManager.Users.FirstOrDefaultAsync(x => x.Id == pref.UserId);
if (user == null)
{
return NotFound();
}
// Check if we are editing a different user than ourself, if we are then we need to power user role
var me = await UserManager.Users.FirstOrDefaultAsync(x => x.UserName == User.Identity.Name);
if (!me.Id.Equals(user.Id, StringComparison.InvariantCultureIgnoreCase))
{
var isPowerUser = await UserManager.IsInRoleAsync(me, OmbiRoles.PowerUser);
var isAdmin = await UserManager.IsInRoleAsync(me, OmbiRoles.Admin);
if (!isPowerUser && !isAdmin)
{
return Unauthorized();
}
}
// Make sure we don't already have a preference for this agent
var existingPreference = await _userNotificationPreferences.GetAll()
.FirstOrDefaultAsync(x => x.UserId == user.Id && x.Agent == pref.Agent);
if (existingPreference != null)
{
// Update it
existingPreference.Value = pref.Value;
existingPreference.Enabled = pref.Enabled;
}
await _userNotificationPreferences.Add(new UserNotificationPreferences
{
Agent = pref.Agent,
Enabled = pref.Enabled,
UserId = pref.UserId,
Value = pref.Value
});
return Json(true);
}
private async Task<List<IdentityResult>> AddRoles(IEnumerable<ClaimCheckboxes> roles, OmbiUser ombiUser)
{
var roleResult = new List<IdentityResult>();

@ -0,0 +1,12 @@
using Ombi.Helpers;
namespace Ombi.Models.Identity
{
public class AddNotificationPreference
{
public NotificationAgent Agent { get; set; }
public string UserId { get; set; }
public string Value { get; set; }
public bool Enabled { get; set; }
}
}
Loading…
Cancel
Save