From 31e5c073835365ac258af420ac6621dce4928c3c Mon Sep 17 00:00:00 2001 From: tidusjar Date: Fri, 27 May 2016 10:42:55 +0100 Subject: [PATCH] Fixed a bug in the user notification where if an admin wants to be notified they wouldn't be. Also added more diagnostic logging under Debug. Added logging under Info to see who notifications are being sent to. --- .../Jobs/PlexAvailabilityChecker.cs | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs b/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs index d6bd244ca..b22961317 100644 --- a/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs +++ b/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs @@ -326,33 +326,36 @@ namespace PlexRequests.Services.Jobs try { var plexUser = PlexApi.GetUsers(apiKey); - if (plexUser?.User == null || plexUser.User.Length == 0) - { - return; - } + var userAccount = PlexApi.GetAccount(apiKey); + + var adminUsername = userAccount.Username ?? string.Empty; var users = UserNotifyRepo.GetAll().ToList(); + Log.Debug("Notifying Users Count {0}", users.Count); foreach (var model in modelChanged) { var selectedUsers = users.Select(x => x.Username).Intersect(model.RequestedUsers); + Log.Debug("Selected Users {0}", selectedUsers.DumpJson()); foreach (var user in selectedUsers) { + Log.Info("Notifying user {0}", user); + if (user == adminUsername) + { + Log.Info("This user is the Plex server owner"); + PublishUserNotification(userAccount.Username, userAccount.Email, model.Title); + return; + } + var email = plexUser.User.FirstOrDefault(x => x.Username == user); if (email == null) { + Log.Info("There is no email address for this Plex user, cannot send notification"); // We do not have a plex user that requested this! continue; } - var notificationModel = new NotificationModel - { - User = email.Username, - UserEmail = email.Email, - NotificationType = NotificationType.RequestAvailable, - Title = model.Title - }; - - // Send the notification to the user. - Notification.Publish(notificationModel); + + Log.Info("Sending notification to: {0} at: {1}, for title: {2}", email.Username, email.Email, model.Title); + PublishUserNotification(email.Username, email.Email, model.Title); } } } @@ -362,6 +365,20 @@ namespace PlexRequests.Services.Jobs } } + private void PublishUserNotification(string username, string email, string title) + { + var notificationModel = new NotificationModel + { + User = username, + UserEmail = email, + NotificationType = NotificationType.RequestAvailable, + Title = title + }; + + // Send the notification to the user. + Notification.Publish(notificationModel); + } + public void Execute(IJobExecutionContext context) { CheckAndUpdateAll();