Example #1
0
        async void _userManager_UserCreated(object sender, GenericEventArgs<User> e)
        {
            var notification = new Notification
            {
                UserId = e.Argument.Id,
                Category = "UserCreated",
                Name = "Welcome to Media Browser!",
                Description = "Check back here for more notifications."
            };

            try
            {
                await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error adding notification", ex);
            }
        }
Example #2
0
        async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
        {
            var result = e.Argument;

            if (result.Status == TaskCompletionStatus.Failed)
            {
                foreach (var user in _userManager
                    .Users
                    .Where(i => i.Configuration.IsAdministrator)
                    .ToList())
                {
                    var notification = new Notification
                    {
                        UserId = user.Id,
                        Category = "ScheduledTaskFailed",
                        Name = result.Name + " failed",
                        RelatedId = result.Name,
                        Description = result.ErrorMessage,
                        Level = NotificationLevel.Error
                    };

                    try
                    {
                        await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorException("Error adding notification", ex);
                    }
                }
            }
        }
Example #3
0
        async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
        {
            var installationInfo = e.InstallationInfo;

            foreach (var user in _userManager
                .Users
                .Where(i => i.Configuration.IsAdministrator)
                .ToList())
            {
                var notification = new Notification
                {
                    UserId = user.Id,
                    Category = "PackageInstallationFailed",
                    Level = NotificationLevel.Error,
                    Name = installationInfo.Name + " " + installationInfo.Version + " installation failed",
                    RelatedId = installationInfo.Name,
                    Description = e.Exception.Message
                };

                try
                {
                    await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error adding notification", ex);
                }
            }
        }
Example #4
0
        async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
        {
            var plugin = e.Argument;

            foreach (var user in _userManager
                .Users
                .Where(i => i.Configuration.IsAdministrator)
                .ToList())
            {
                var notification = new Notification
                {
                    UserId = user.Id,
                    Category = "PluginUninstalled",
                    Name = plugin.Name + " has been uninstalled",
                    RelatedId = plugin.Id.ToString()
                };

                try
                {
                    await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error adding notification", ex);
                }
            }
        }
        /// <summary>
        /// Replaces the notification.
        /// </summary>
        /// <param name="notification">The notification.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        private async Task ReplaceNotification(Notification notification, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(notification.Id))
            {
                notification.Id = Guid.NewGuid().ToString("N");
            }
            if (string.IsNullOrEmpty(notification.UserId))
            {
                throw new ArgumentException("The notification must have a user id");
            }

            cancellationToken.ThrowIfCancellationRequested();

            await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);

            IDbTransaction transaction = null;

            try
            {
                transaction = _connection.BeginTransaction();

                _replaceNotificationCommand.GetParameter(0).Value = new Guid(notification.Id);
                _replaceNotificationCommand.GetParameter(1).Value = new Guid(notification.UserId);
                _replaceNotificationCommand.GetParameter(2).Value = notification.Date.ToUniversalTime();
                _replaceNotificationCommand.GetParameter(3).Value = notification.Name;
                _replaceNotificationCommand.GetParameter(4).Value = notification.Description;
                _replaceNotificationCommand.GetParameter(5).Value = notification.Url;
                _replaceNotificationCommand.GetParameter(6).Value = notification.Level.ToString();
                _replaceNotificationCommand.GetParameter(7).Value = notification.IsRead;
                _replaceNotificationCommand.GetParameter(8).Value = string.Empty;
                _replaceNotificationCommand.GetParameter(9).Value = string.Empty;

                _replaceNotificationCommand.Transaction = transaction;

                _replaceNotificationCommand.ExecuteNonQuery();

                transaction.Commit();
            }
            catch (OperationCanceledException)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }

                throw;
            }
            catch (Exception e)
            {
                _logger.ErrorException("Failed to save notification:", e);

                if (transaction != null)
                {
                    transaction.Rollback();
                }

                throw;
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Dispose();
                }

                _writeLock.Release();
            }
        }
        /// <summary>
        /// Adds the notification.
        /// </summary>
        /// <param name="notification">The notification.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task AddNotification(Notification notification, CancellationToken cancellationToken)
        {
            await ReplaceNotification(notification, cancellationToken).ConfigureAwait(false);

            if (NotificationAdded != null)
            {
                try
                {
                    NotificationAdded(this, new NotificationUpdateEventArgs
                    {
                        Notification = notification
                    });
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error in NotificationAdded event handler", ex);
                }
            }
        }
        private Notification GetNotification(IDataReader reader)
        {
            var notification = new Notification
            {
                Id = reader.GetGuid(0).ToString("N"),
                UserId = reader.GetGuid(1).ToString("N"),
                Date = reader.GetDateTime(2).ToUniversalTime(),
                Name = reader.GetString(3)
            };

            if (!reader.IsDBNull(4))
            {
                notification.Description = reader.GetString(4);
            }

            if (!reader.IsDBNull(5))
            {
                notification.Url = reader.GetString(5);
            }

            notification.Level = GetLevel(reader, 6);
            notification.IsRead = reader.GetBoolean(7);

            return notification;
        }
        /// <summary>
        /// Replaces the notification.
        /// </summary>
        /// <param name="notification">The notification.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        private async Task ReplaceNotification(Notification notification, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(notification.Id))
            {
                notification.Id = Guid.NewGuid().ToString("N");
            }
            if (string.IsNullOrEmpty(notification.UserId))
            {
                throw new ArgumentException("The notification must have a user id");
            }

            cancellationToken.ThrowIfCancellationRequested();

            using (var connection = await CreateConnection().ConfigureAwait(false))
            {
                using (var replaceNotificationCommand = connection.CreateCommand())
                {
                    replaceNotificationCommand.CommandText = "replace into Notifications (Id, UserId, Date, Name, Description, Url, Level, IsRead, Category, RelatedId) values (@Id, @UserId, @Date, @Name, @Description, @Url, @Level, @IsRead, @Category, @RelatedId)";

                    replaceNotificationCommand.Parameters.Add(replaceNotificationCommand, "@Id");
                    replaceNotificationCommand.Parameters.Add(replaceNotificationCommand, "@UserId");
                    replaceNotificationCommand.Parameters.Add(replaceNotificationCommand, "@Date");
                    replaceNotificationCommand.Parameters.Add(replaceNotificationCommand, "@Name");
                    replaceNotificationCommand.Parameters.Add(replaceNotificationCommand, "@Description");
                    replaceNotificationCommand.Parameters.Add(replaceNotificationCommand, "@Url");
                    replaceNotificationCommand.Parameters.Add(replaceNotificationCommand, "@Level");
                    replaceNotificationCommand.Parameters.Add(replaceNotificationCommand, "@IsRead");
                    replaceNotificationCommand.Parameters.Add(replaceNotificationCommand, "@Category");
                    replaceNotificationCommand.Parameters.Add(replaceNotificationCommand, "@RelatedId");

                    IDbTransaction transaction = null;

                    try
                    {
                        transaction = connection.BeginTransaction();

                        replaceNotificationCommand.GetParameter(0).Value = new Guid(notification.Id);
                        replaceNotificationCommand.GetParameter(1).Value = new Guid(notification.UserId);
                        replaceNotificationCommand.GetParameter(2).Value = notification.Date.ToUniversalTime();
                        replaceNotificationCommand.GetParameter(3).Value = notification.Name;
                        replaceNotificationCommand.GetParameter(4).Value = notification.Description;
                        replaceNotificationCommand.GetParameter(5).Value = notification.Url;
                        replaceNotificationCommand.GetParameter(6).Value = notification.Level.ToString();
                        replaceNotificationCommand.GetParameter(7).Value = notification.IsRead;
                        replaceNotificationCommand.GetParameter(8).Value = string.Empty;
                        replaceNotificationCommand.GetParameter(9).Value = string.Empty;

                        replaceNotificationCommand.Transaction = transaction;

                        replaceNotificationCommand.ExecuteNonQuery();

                        transaction.Commit();
                    }
                    catch (OperationCanceledException)
                    {
                        if (transaction != null)
                        {
                            transaction.Rollback();
                        }

                        throw;
                    }
                    catch (Exception e)
                    {
                        Logger.ErrorException("Failed to save notification:", e);

                        if (transaction != null)
                        {
                            transaction.Rollback();
                        }

                        throw;
                    }
                    finally
                    {
                        if (transaction != null)
                        {
                            transaction.Dispose();
                        }
                    }
                }
            }
        }