mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-19 12:59:39 -07:00
Added the API to add user notification preferences
This commit is contained in:
parent
2886f6e6fa
commit
de2e3abfe0
4 changed files with 63 additions and 7 deletions
|
@ -28,6 +28,7 @@ namespace Ombi.Store.Entities
|
||||||
public string UserAccessToken { get; set; }
|
public string UserAccessToken { get; set; }
|
||||||
|
|
||||||
public List<NotificationUserId> NotificationUserIds { get; set; }
|
public List<NotificationUserId> NotificationUserIds { get; set; }
|
||||||
|
public List<UserNotificationPreferences> UserNotificationPreferences { get; set; }
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
public bool IsEmbyConnect => UserType == UserType.EmbyUser && EmbyConnectUserId.HasValue();
|
public bool IsEmbyConnect => UserType == UserType.EmbyUser && EmbyConnectUserId.HasValue();
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
using System;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using System.Collections.Generic;
|
using Newtonsoft.Json;
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
|
||||||
using System.Text;
|
|
||||||
using Ombi.Helpers;
|
using Ombi.Helpers;
|
||||||
|
|
||||||
namespace Ombi.Store.Entities
|
namespace Ombi.Store.Entities
|
||||||
|
@ -15,6 +13,7 @@ namespace Ombi.Store.Entities
|
||||||
public string Value { get; set; }
|
public string Value { get; set; }
|
||||||
|
|
||||||
[ForeignKey(nameof(UserId))]
|
[ForeignKey(nameof(UserId))]
|
||||||
|
[JsonIgnore]
|
||||||
public OmbiUser User { get; set; }
|
public OmbiUser User { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -793,15 +793,14 @@ namespace Ombi.Controllers
|
||||||
[HttpGet("notificationpreferences")]
|
[HttpGet("notificationpreferences")]
|
||||||
public async Task<List<UserNotificationPreferences>> GetUserPreferences()
|
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 user = await UserManager.Users.FirstOrDefaultAsync(x => x.UserName == User.Identity.Name);
|
||||||
var userPreferences = await _userNotificationPreferences.GetAll().Where(x => x.UserId == user.Id).ToListAsync();
|
var userPreferences = await _userNotificationPreferences.GetAll().Where(x => x.UserId == user.Id).ToListAsync();
|
||||||
|
|
||||||
var agents = Enum.GetValues(typeof(NotificationAgent)).Cast<NotificationAgent>();
|
var agents = Enum.GetValues(typeof(NotificationAgent)).Cast<NotificationAgent>();
|
||||||
foreach (var a in agents)
|
foreach (var a in agents)
|
||||||
{
|
{
|
||||||
var hasAgent = userPreferences.Any(x => x.Agent == a);
|
var agent = userPreferences.FirstOrDefault(x => x.Agent == a);
|
||||||
if (!hasAgent)
|
if (agent == null)
|
||||||
{
|
{
|
||||||
// Create the default
|
// Create the default
|
||||||
userPreferences.Add(new UserNotificationPreferences
|
userPreferences.Add(new UserNotificationPreferences
|
||||||
|
@ -809,11 +808,56 @@ namespace Ombi.Controllers
|
||||||
Agent = a,
|
Agent = a,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
userPreferences.Add(agent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return userPreferences;
|
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)
|
private async Task<List<IdentityResult>> AddRoles(IEnumerable<ClaimCheckboxes> roles, OmbiUser ombiUser)
|
||||||
{
|
{
|
||||||
var roleResult = new List<IdentityResult>();
|
var roleResult = new List<IdentityResult>();
|
||||||
|
|
12
src/Ombi/Models/Identity/AddNotificationPreference.cs
Normal file
12
src/Ombi/Models/Identity/AddNotificationPreference.cs
Normal file
|
@ -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…
Add table
Add a link
Reference in a new issue