Ejemplo n.º 1
0
        private IEnumerable<string> GetUserIds(NotificationRequest request, NotificationOption options)
        {
            if (request.SendToUserMode.HasValue)
            {
                switch (request.SendToUserMode.Value)
                {
                    case SendToUserType.Admins:
                        return _userManager.Users.Where(i => i.Configuration.IsAdministrator)
                                .Select(i => i.Id.ToString("N"));
                    case SendToUserType.All:
                        return _userManager.Users.Select(i => i.Id.ToString("N"));
                    case SendToUserType.Custom:
                        return request.UserIds;
                    default:
                        throw new ArgumentException("Unrecognized SendToUserMode: " + request.SendToUserMode.Value);
                }
            }

            if (options != null && !string.IsNullOrWhiteSpace(request.NotificationType))
            {
                var config = GetConfiguration();

                return _userManager.Users
                    .Where(i => config.IsEnabledToSendToUser(request.NotificationType, i.Id.ToString("N"), i.Configuration))
                    .Select(i => i.Id.ToString("N"));
            }

            return request.UserIds;
        }
Ejemplo n.º 2
0
        async void _appHost_ApplicationUpdated(object sender, GenericEventArgs<PackageVersionInfo> e)
        {
            var type = NotificationType.ApplicationUpdateInstalled.ToString();

            var notification = new NotificationRequest
            {
                NotificationType = type
            };

            notification.Variables["Version"] = e.Argument.versionStr;
            notification.Variables["ReleaseNotes"] = e.Argument.description;

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 3
0
        private Task SendNotification(NotificationRequest request,
            INotificationService service,
            IEnumerable<User> users,
            string title,
            string description,
            CancellationToken cancellationToken)
        {
            users = users.Where(i => IsEnabledForUser(service, i))
                .ToList();

            var tasks = users.Select(i => SendNotification(request, service, title, description, i, cancellationToken));

            return Task.WhenAll(tasks);

        }
Ejemplo n.º 4
0
        public Task SendNotification(NotificationRequest request, CancellationToken cancellationToken)
        {
            var notificationType = request.NotificationType;

            var options = string.IsNullOrWhiteSpace(notificationType) ?
                null :
                GetConfiguration().GetOptions(notificationType);

            var users = GetUserIds(request, options)
                .Select(i => _userManager.GetUserById(i));

            var title = GetTitle(request, options);
            var description = GetDescription(request, options);

            var tasks = _services.Where(i => IsEnabled(i, notificationType))
                .Select(i => SendNotification(request, i, users, title, description, cancellationToken));

            return Task.WhenAll(tasks);
        }
Ejemplo n.º 5
0
 private async Task SendNotification(NotificationRequest notification)
 {
     try
     {
         await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
     }
     catch (Exception ex)
     {
         _logger.ErrorException("Error sending notification", ex);
     }
 }
Ejemplo n.º 6
0
        async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
        {
            var installationInfo = e.InstallationInfo;

            var type = NotificationType.InstallationFailed.ToString();

            var notification = new NotificationRequest
            {
                Level = NotificationLevel.Error,
                Description = e.Exception.Message,
                NotificationType = type
            };

            notification.Variables["Name"] = installationInfo.Name;
            notification.Variables["Version"] = installationInfo.Version;

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 7
0
        async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
        {
            var type = NotificationType.PluginUninstalled.ToString();

            var plugin = e.Argument;

            var notification = new NotificationRequest
            {
                NotificationType = type
            };

            notification.Variables["Name"] = plugin.Name;
            notification.Variables["Version"] = plugin.Version.ToString();

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 8
0
        async void _taskManager_TaskCompleted(object sender, TaskCompletionEventArgs e)
        {
            var result = e.Result;

            if (result.Status == TaskCompletionStatus.Failed)
            {
                var type = NotificationType.TaskFailed.ToString();

                var notification = new NotificationRequest
                {
                    Description = result.ErrorMessage,
                    Level = NotificationLevel.Error,
                    NotificationType = type
                };

                notification.Variables["Name"] = result.Name;
                notification.Variables["ErrorMessage"] = result.ErrorMessage;

                await SendNotification(notification).ConfigureAwait(false);
            }
        }
Ejemplo n.º 9
0
        private async void SendPlaybackNotification(string type, PlaybackProgressEventArgs e)
        {
            var user = e.Users.FirstOrDefault();

            var item = e.MediaInfo;

            if (e.Item != null && e.Item.Parent == null)
            {
                // Don't report theme song or local trailer playback
                // TODO: This will also cause movie specials to not be reported
                return;
            }

            var notification = new NotificationRequest
            {
                NotificationType = type,

                ExcludeUserIds = e.Users.Select(i => i.Id.ToString("N")).ToList()
            };

            notification.Variables["ItemName"] = item.Name;
            notification.Variables["UserName"] = user == null ? "Unknown user" : user.Name;
            notification.Variables["AppName"] = e.ClientName;
            notification.Variables["DeviceName"] = e.DeviceName;

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 10
0
        private async Task SendNotification(NotificationRequest request,
            INotificationService service,
            string title,
            string description,
            User user,
            CancellationToken cancellationToken)
        {
            var notification = new UserNotification
            {
                Date = request.Date,
                Description = description,
                Level = request.Level,
                Name = title,
                Url = request.Url,
                User = user
            };

            _logger.Debug("Sending notification via {0} to user {1}", service.Name, user.Name);

            try
            {
                await service.SendNotification(notification, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error sending notification to {0}", ex, service.Name);
            }
        }
Ejemplo n.º 11
0
        async void _appHost_HasPendingRestartChanged(object sender, EventArgs e)
        {
            if (!_appHost.HasPendingRestart)
            {
                return;
            }

            var type = NotificationType.ServerRestartRequired.ToString();

            var notification = new NotificationRequest
            {
                NotificationType = type
            };

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 12
0
        async void _appHost_HasUpdateAvailableChanged(object sender, EventArgs e)
        {
            // This notification is for users who can't auto-update (aka running as service)
            if (!_appHost.HasUpdateAvailable || _appHost.CanSelfUpdate)
            {
                return;
            }

            var type = NotificationType.ApplicationUpdateAvailable.ToString();

            var notification = new NotificationRequest
            {
                Description = "Please see emby.media for details.",
                NotificationType = type
            };

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 13
0
        async void _installationManager_PluginInstalled(object sender, GenericEventArgs<PackageVersionInfo> e)
        {
            var type = NotificationType.PluginInstalled.ToString();

            var installationInfo = e.Argument;

            var notification = new NotificationRequest
            {
                Description = installationInfo.description,
                NotificationType = type
            };

            notification.Variables["Name"] = installationInfo.name;
            notification.Variables["Version"] = installationInfo.versionStr;

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 14
0
        async void _installationManager_PluginUpdated(object sender, GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>> e)
        {
            var type = NotificationType.PluginUpdateInstalled.ToString();

            var installationInfo = e.Argument.Item1;

            var notification = new NotificationRequest
            {
                Description = e.Argument.Item2.description,
                NotificationType = type
            };

            notification.Variables["Name"] = installationInfo.Name;
            notification.Variables["Version"] = installationInfo.Version.ToString();
            notification.Variables["ReleaseNotes"] = e.Argument.Item2.description;

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 15
0
        async void _userManager_UserLockedOut(object sender, GenericEventArgs<User> e)
        {
            var type = NotificationType.UserLockedOut.ToString();

            var notification = new NotificationRequest
            {
                NotificationType = type
            };

            notification.Variables["UserName"] = e.Argument.Name;

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 16
0
        async void _deviceManager_CameraImageUploaded(object sender, GenericEventArgs<CameraImageUploadInfo> e)
        {
            var type = NotificationType.CameraImageUploaded.ToString();

            var notification = new NotificationRequest
            {
                NotificationType = type
            };

            notification.Variables["DeviceName"] = e.Argument.Device.Name;

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 17
0
        private async void SendPlaybackNotification(string type, PlaybackProgressEventArgs e)
        {
            var user = e.Users.FirstOrDefault();

            if (user != null && !GetOptions().IsEnabledToMonitorUser(type, user.Id.ToString("N")))
            {
                return;
            }

            var item = e.MediaInfo;
            var themeMedia = item as IThemeMedia;

            if (themeMedia != null && themeMedia.IsThemeMedia)
            {
                // Don't report theme song or local trailer playback
                return;
            }

            var notification = new NotificationRequest
            {
                NotificationType = type
            };

            notification.Variables["ItemName"] = item.Name;
            notification.Variables["UserName"] = user == null ? "Unknown user" : user.Name;
            notification.Variables["AppName"] = e.ClientName;
            notification.Variables["DeviceName"] = e.DeviceName;

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 18
0
        private string GetDescription(NotificationRequest request, NotificationOption options)
        {
            var text = request.Description;

            // If empty, grab from options 
            if (string.IsNullOrEmpty(text))
            {
                if (!string.IsNullOrEmpty(request.NotificationType))
                {
                    if (options != null)
                    {
                        text = options.Description;
                    }
                }
            }

            // If still empty, grab default
            if (string.IsNullOrEmpty(text))
            {
                if (!string.IsNullOrEmpty(request.NotificationType))
                {
                    var info = GetNotificationTypes().FirstOrDefault(i => string.Equals(i.Type, request.NotificationType, StringComparison.OrdinalIgnoreCase));

                    if (info != null)
                    {
                        text = info.DefaultDescription;
                    }
                }
            }

            text = text ?? string.Empty;

            foreach (var pair in request.Variables)
            {
                var token = "{" + pair.Key + "}";

                text = text.Replace(token, pair.Value, StringComparison.OrdinalIgnoreCase);
            }

            return text;
        }
Ejemplo n.º 19
0
        private async void LibraryUpdateTimerCallback(object state)
        {
            List<BaseItem> items;

            lock (_libraryChangedSyncLock)
            {
                items = _itemsAdded.ToList();
                _itemsAdded.Clear();
                DisposeLibraryUpdateTimer();
            }

            if (items.Count == 1)
            {
                var item = items.First();

                var notification = new NotificationRequest
                {
                    NotificationType = NotificationType.NewLibraryContent.ToString()
                };

                notification.Variables["Name"] = GetItemName(item);

                await SendNotification(notification).ConfigureAwait(false);
            }
            else
            {
                var notification = new NotificationRequest
                {
                    NotificationType = NotificationType.NewLibraryContentMultiple.ToString()
                };

                notification.Variables["ItemCount"] = items.Count.ToString(CultureInfo.InvariantCulture);

                await SendNotification(notification).ConfigureAwait(false);
            }
        }
Ejemplo n.º 20
0
        async void _userManager_UserCreated(object sender, GenericEventArgs<User> e)
        {
            var notification = new NotificationRequest
            {
                UserIds = new List<string> { e.Argument.Id.ToString("N") },
                Name = "Welcome to Emby!",
                Description = "Check back here for more notifications."
            };

            await SendNotification(notification).ConfigureAwait(false);
        }
Ejemplo n.º 21
0
        private async void LibraryUpdateTimerCallback(object state)
        {
            List<BaseItem> items;

            lock (_libraryChangedSyncLock)
            {
                items = _itemsAdded.ToList();
                _itemsAdded.Clear();
                DisposeLibraryUpdateTimer();
            }

            items = items.Take(10).ToList();

            foreach (var item in items)
            {
                var notification = new NotificationRequest
                {
                    NotificationType = NotificationType.NewLibraryContent.ToString()
                };

                notification.Variables["Name"] = GetItemName(item);

                await SendNotification(notification).ConfigureAwait(false);
            }
        }