aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs
blob: a97711684c7ae7c32bd431275dc880324abe4a13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Common.Updates;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Notifications;
using System;
using System.Linq;
using System.Threading;
using MediaBrowser.Model.Tasks;

namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
{
    /// <summary>
    /// Creates notifications for various system events
    /// </summary>
    public class Notifications : IServerEntryPoint
    {
        private readonly INotificationsRepository _notificationsRepo;
        private readonly IInstallationManager _installationManager;
        private readonly IUserManager _userManager;
        private readonly ILogger _logger;

        private readonly ITaskManager _taskManager;

        public Notifications(IInstallationManager installationManager, INotificationsRepository notificationsRepo, IUserManager userManager, ILogger logger, ITaskManager taskManager)
        {
            _installationManager = installationManager;
            _notificationsRepo = notificationsRepo;
            _userManager = userManager;
            _logger = logger;
            _taskManager = taskManager;
        }

        public void Run()
        {
            _installationManager.PackageInstallationCompleted += _installationManager_PackageInstallationCompleted;
            _installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed;
            _installationManager.PluginUninstalled += _installationManager_PluginUninstalled;

            _taskManager.TaskCompleted += _taskManager_TaskCompleted;

            _userManager.UserCreated += _userManager_UserCreated;
        }

        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);
            }
        }

        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);
                    }
                }
            }
        }

        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);
                }
            }
        }

        async void _installationManager_PackageInstallationCompleted(object sender, InstallationEventArgs 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 = "PackageInstallationCompleted",
                    Name = installationInfo.Name + " " + installationInfo.Version + " was installed",
                    RelatedId = installationInfo.Name,
                    Description = e.PackageVersionInfo.description
                };

                try
                {
                    await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error adding notification", ex);
                }
            }
        }

        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);
                }
            }
        }

        public void Dispose()
        {
            _installationManager.PackageInstallationCompleted -= _installationManager_PackageInstallationCompleted;
            _installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
            _installationManager.PluginUninstalled -= _installationManager_PluginUninstalled;

            _taskManager.TaskCompleted -= _taskManager_TaskCompleted;

            _userManager.UserCreated -= _userManager_UserCreated;
        }
    }
}