From dd54064507a8ed7f2ee7e6f0a89331c642d1db5b Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Tue, 31 Jan 2017 08:24:39 +0000 Subject: [PATCH] Fix for #1026 --- Ombi.Core/Users/UserHelper.cs | 67 ++++++++++++++++--- .../Notification/EmbyNotificationEngine.cs | 38 ++++++----- Ombi.UI/Modules/UserWizardModule.cs | 1 + 3 files changed, 79 insertions(+), 27 deletions(-) diff --git a/Ombi.Core/Users/UserHelper.cs b/Ombi.Core/Users/UserHelper.cs index 2e5b523af..ae3174de1 100644 --- a/Ombi.Core/Users/UserHelper.cs +++ b/Ombi.Core/Users/UserHelper.cs @@ -30,6 +30,7 @@ using System.Linq; using Ombi.Core.Models; using Ombi.Helpers; using Ombi.Helpers.Permissions; +using Ombi.Store.Models.Emby; using Ombi.Store.Models.Plex; using Ombi.Store.Repository; @@ -37,16 +38,18 @@ namespace Ombi.Core.Users { public class UserHelper : IUserHelper { - public UserHelper(IUserRepository userRepository, IExternalUserRepository plexUsers, ISecurityExtensions security) + public UserHelper(IUserRepository userRepository, IExternalUserRepository plexUsers, IExternalUserRepository emby, ISecurityExtensions security) { LocalUserRepository = userRepository; PlexUserRepository = plexUsers; Security = security; + EmbyUserRepository = emby; } private IUserRepository LocalUserRepository { get; } private IExternalUserRepository PlexUserRepository { get; } private ISecurityExtensions Security { get; } + private IExternalUserRepository EmbyUserRepository { get; } public IEnumerable GetUsers() @@ -54,7 +57,8 @@ namespace Ombi.Core.Users var model = new List(); var localUsers = LocalUserRepository.GetAll(); - var plexUsers = PlexUserRepository.GetAll(); + var plexUsers = PlexUserRepository.GetAll().ToList(); + var embyUsers = EmbyUserRepository.GetAll().ToList(); foreach (var user in localUsers) { @@ -69,14 +73,30 @@ namespace Ombi.Core.Users }); } - model.AddRange(plexUsers.Select(user => new UserHelperModel + if (plexUsers.Any()) { - Type = UserType.LocalUser, - Username = user.Username, - UserAlias = user.UserAlias, - EmailAddress = user.EmailAddress, - Permissions = (Permissions)user.Permissions - })); + model.AddRange(plexUsers.Select(user => new UserHelperModel + { + Type = UserType.PlexUser, + Username = user.Username, + UserAlias = user.UserAlias, + EmailAddress = user.EmailAddress, + Permissions = (Permissions) user.Permissions + })); + } + + if (embyUsers.Any()) + { + model.AddRange(embyUsers.Select(user => new UserHelperModel + { + Type = UserType.EmbyUser, + Username = user.Username, + UserAlias = user.UserAlias, + EmailAddress = user.EmailAddress, + Permissions = (Permissions)user.Permissions + })); + + } return model; } @@ -87,9 +107,11 @@ namespace Ombi.Core.Users var localUsers = LocalUserRepository.GetAll().ToList(); var plexUsers = PlexUserRepository.GetAll().ToList(); + var embyUsers = EmbyUserRepository.GetAll().ToList(); var filteredLocal = localUsers.Where(x => ((Permissions)x.Permissions).HasFlag(permission)); var filteredPlex = plexUsers.Where(x => ((Permissions)x.Permissions).HasFlag(permission)); + var filteredEmby = embyUsers.Where(x => ((Permissions)x.Permissions).HasFlag(permission)); foreach (var user in filteredLocal) @@ -108,7 +130,17 @@ namespace Ombi.Core.Users model.AddRange(filteredPlex.Select(user => new UserHelperModel { - Type = UserType.LocalUser, + Type = UserType.PlexUser, + Username = user.Username, + UserAlias = user.UserAlias, + EmailAddress = user.EmailAddress, + Permissions = (Permissions)user.Permissions, + Features = (Features)user.Features + })); + + model.AddRange(filteredEmby.Select(user => new UserHelperModel + { + Type = UserType.EmbyUser, Username = user.Username, UserAlias = user.UserAlias, EmailAddress = user.EmailAddress, @@ -116,6 +148,7 @@ namespace Ombi.Core.Users Features = (Features)user.Features })); + return model; } @@ -125,9 +158,11 @@ namespace Ombi.Core.Users var localUsers = LocalUserRepository.GetAll().ToList(); var plexUsers = PlexUserRepository.GetAll().ToList(); + var embyUsers = PlexUserRepository.GetAll().ToList(); var filteredLocal = localUsers.Where(x => ((Features)x.Features).HasFlag(features)); var filteredPlex = plexUsers.Where(x => ((Features)x.Features).HasFlag(features)); + var filteredEmby = embyUsers.Where(x => ((Features)x.Features).HasFlag(features)); foreach (var user in filteredLocal) @@ -146,7 +181,17 @@ namespace Ombi.Core.Users model.AddRange(filteredPlex.Select(user => new UserHelperModel { - Type = UserType.LocalUser, + Type = UserType.PlexUser, + Username = user.Username, + UserAlias = user.UserAlias, + EmailAddress = user.EmailAddress, + Permissions = (Permissions)user.Permissions, + Features = (Features)user.Features + })); + + model.AddRange(filteredEmby.Select(user => new UserHelperModel + { + Type = UserType.EmbyUser, Username = user.Username, UserAlias = user.UserAlias, EmailAddress = user.EmailAddress, diff --git a/Ombi.Services/Notification/EmbyNotificationEngine.cs b/Ombi.Services/Notification/EmbyNotificationEngine.cs index 6eab1cae5..ed8f587a6 100644 --- a/Ombi.Services/Notification/EmbyNotificationEngine.cs +++ b/Ombi.Services/Notification/EmbyNotificationEngine.cs @@ -12,19 +12,21 @@ using Ombi.Helpers.Permissions; using Ombi.Services.Interfaces; using Ombi.Store; using Ombi.Store.Models; +using Ombi.Store.Models.Emby; using Ombi.Store.Repository; namespace Ombi.Services.Notification { public class EmbyNotificationEngine : IEmbyNotificationEngine { - public EmbyNotificationEngine(IEmbyApi p, IRepository repo, ISettingsService embySettings, INotificationService service, IUserHelper userHelper) + public EmbyNotificationEngine(IEmbyApi p, IRepository repo, ISettingsService embySettings, INotificationService service, IUserHelper userHelper, IExternalUserRepository embyUsers) { EmbyApi = p; UserNotifyRepo = repo; Notification = service; UserHelper = userHelper; EmbySettings = embySettings; + EmbyUserRepo = embyUsers; } private IEmbyApi EmbyApi { get; } @@ -33,6 +35,7 @@ namespace Ombi.Services.Notification private INotificationService Notification { get; } private IUserHelper UserHelper { get; } private ISettingsService EmbySettings { get; } + private IExternalUserRepository EmbyUserRepo { get; } public async Task NotifyUsers(IEnumerable modelChanged, NotificationType type) { @@ -68,22 +71,23 @@ namespace Ombi.Services.Notification foreach (var user in selectedUsers) { + var localUser = + users.FirstOrDefault(x => + x.Username.Equals(user, StringComparison.CurrentCultureIgnoreCase) || + x.UserAlias.Equals(user, StringComparison.CurrentCultureIgnoreCase)); Log.Info("Notifying user {0}", user); if (user.Equals(adminUsername, StringComparison.CurrentCultureIgnoreCase)) { Log.Info("This user is the Plex server owner"); - await PublishUserNotification(userAccount?.Name, userAccount?.Name, model.Title, model.PosterPath, type, model.Type); // TODO Emby needs email address + await PublishUserNotification(userAccount?.Name, localUser?.EmailAddress, model.Title, model.PosterPath, type, model.Type); return; } - var localUser = - users.FirstOrDefault( x => - x.Username.Equals(user, StringComparison.CurrentCultureIgnoreCase) || - x.UserAlias.Equals(user, StringComparison.CurrentCultureIgnoreCase)); + // So if the request was from an alias, then we need to use the local user (since that contains the alias). - // If we do not have a local user, then we should be using the Plex user if that user exists. - // This will execute most of the time since Plex and Local users will most always be in the database. + // If we do not have a local user, then we should be using the Emby user if that user exists. + // This will execute most of the time since Emby and Local users will most always be in the database. if (localUser != null) { if (string.IsNullOrEmpty(localUser?.EmailAddress)) @@ -98,16 +102,17 @@ namespace Ombi.Services.Notification } else { + var embyUser = EmbyUserRepo.GetUserByUsername(user); var email = embyUsers.FirstOrDefault(x => x.Name.Equals(user, StringComparison.CurrentCultureIgnoreCase)); - if (string.IsNullOrEmpty(email?.Name)) // TODO this needs to be the email + if (string.IsNullOrEmpty(embyUser?.EmailAddress)) // TODO this needs to be the email { - Log.Info("There is no email address for this Emby user ({0}), cannot send notification", email?.Name); //TODO + Log.Info("There is no email address for this Emby user ({0}), cannot send notification", email?.Name); // We do not have a plex user that requested this! continue; } - Log.Info("Sending notification to: {0} at: {1}, for : {2}", email.Name, email.Name, model.Title); - await PublishUserNotification(email.Name, email.Name, model.Title, model.PosterPath, type, model.Type); //TODO + Log.Info("Sending notification to: {0} at: {1}, for : {2}", embyUser?.Username, embyUser?.EmailAddress, model.Title); + await PublishUserNotification(email?.Name, embyUser?.EmailAddress, model.Title, model.PosterPath, type, model.Type); } } } @@ -162,11 +167,12 @@ namespace Ombi.Services.Notification Log.Debug("Users being notified for this request count {0}", users.Count); foreach (var user in usersToNotify) { + var embyUser = EmbyUserRepo.GetUserByUsername(user); Log.Info("Notifying user {0}", user); if (user.Equals(adminUsername, StringComparison.CurrentCultureIgnoreCase)) { Log.Info("This user is the Emby server owner"); - await PublishUserNotification(userAccount.Name, userAccount.Name, model.Title, model.PosterPath, type, model.Type); // TODO + await PublishUserNotification(userAccount.Name, embyUser.EmailAddress, model.Title, model.PosterPath, type, model.Type); return; } @@ -174,12 +180,12 @@ namespace Ombi.Services.Notification if (email == null) { Log.Info("There is no email address for this Emby user, cannot send notification"); - // We do not have a plex user that requested this! + // We do not have a emby user that requested this! continue; } - Log.Info("Sending notification to: {0} at: {1}, for title: {2}", email.Name, email.Name, model.Title); // TODO - await PublishUserNotification(email.Name, email.Name, model.Title, model.PosterPath, type, model.Type); // TODO + Log.Info("Sending notification to: {0} at: {1}, for title: {2}", email.Name, embyUser.EmailAddress, model.Title); + await PublishUserNotification(email.Name, embyUser.EmailAddress, model.Title, model.PosterPath, type, model.Type); } } catch (Exception e) diff --git a/Ombi.UI/Modules/UserWizardModule.cs b/Ombi.UI/Modules/UserWizardModule.cs index 39cf8cc3e..884d90e52 100644 --- a/Ombi.UI/Modules/UserWizardModule.cs +++ b/Ombi.UI/Modules/UserWizardModule.cs @@ -153,6 +153,7 @@ namespace Ombi.UI.Modules var firstServer = servers.Server.FirstOrDefault(x => x.AccessToken == form.PlexAuthToken); Session[SessionKeys.UserWizardMachineId] = firstServer?.MachineIdentifier; + form.MachineIdentifier = firstServer?.MachineIdentifier; } catch (Exception e) {