aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Notifications/SqliteNotificationsRepository.cs
blob: 7302431e172e3dc1e7b0cce7ed7c446b7ffa8538 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
using MediaBrowser.Controller;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Notifications;
using MediaBrowser.Server.Implementations.Persistence;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace MediaBrowser.Server.Implementations.Notifications
{
    public class SqliteNotificationsRepository : BaseSqliteRepository, INotificationsRepository
    {
        private IDbConnection _connection;
        private readonly IServerApplicationPaths _appPaths;

        public event EventHandler<NotificationUpdateEventArgs> NotificationAdded;
        public event EventHandler<NotificationReadEventArgs> NotificationsMarkedRead;
        public event EventHandler<NotificationUpdateEventArgs> NotificationUpdated;

        private IDbCommand _replaceNotificationCommand;
        private IDbCommand _markReadCommand;
        private IDbCommand _markAllReadCommand;

        public SqliteNotificationsRepository(ILogManager logManager, IServerApplicationPaths appPaths)
            : base(logManager)
        {
            _appPaths = appPaths;
        }

        public async Task Initialize()
        {
            var dbFile = Path.Combine(_appPaths.DataPath, "notifications.db");

            _connection = await SqliteExtensions.ConnectToDb(dbFile, Logger).ConfigureAwait(false);

            string[] queries = {

                                "create table if not exists Notifications (Id GUID NOT NULL, UserId GUID NOT NULL, Date DATETIME NOT NULL, Name TEXT NOT NULL, Description TEXT, Url TEXT, Level TEXT NOT NULL, IsRead BOOLEAN NOT NULL, Category TEXT NOT NULL, RelatedId TEXT, PRIMARY KEY (Id, UserId))",
                                "create index if not exists idx_Notifications on Notifications(Id, UserId)",

                                //pragmas
                                "pragma temp_store = memory",

                                "pragma shrink_memory"
                               };

            _connection.RunQueries(queries, Logger);

            PrepareStatements();
        }

        private void PrepareStatements()
        {
            _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");

            _markReadCommand = _connection.CreateCommand();
            _markReadCommand.CommandText = "update Notifications set IsRead=@IsRead where Id=@Id and UserId=@UserId";

            _markReadCommand.Parameters.Add(_replaceNotificationCommand, "@UserId");
            _markReadCommand.Parameters.Add(_replaceNotificationCommand, "@IsRead");
            _markReadCommand.Parameters.Add(_replaceNotificationCommand, "@Id");

            _markAllReadCommand = _connection.CreateCommand();
            _markAllReadCommand.CommandText = "update Notifications set IsRead=@IsRead where UserId=@UserId";

            _markAllReadCommand.Parameters.Add(_replaceNotificationCommand, "@UserId");
            _markAllReadCommand.Parameters.Add(_replaceNotificationCommand, "@IsRead");
        }

        /// <summary>
        /// Gets the notifications.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <returns>NotificationResult.</returns>
        public NotificationResult GetNotifications(NotificationQuery query)
        {
            var result = new NotificationResult();

            using (var cmd = _connection.CreateCommand())
            {
                var clauses = new List<string>();

                if (query.IsRead.HasValue)
                {
                    clauses.Add("IsRead=@IsRead");
                    cmd.Parameters.Add(cmd, "@IsRead", DbType.Boolean).Value = query.IsRead.Value;
                }

                clauses.Add("UserId=@UserId");
                cmd.Parameters.Add(cmd, "@UserId", DbType.Guid).Value = new Guid(query.UserId);

                var whereClause = " where " + string.Join(" And ", clauses.ToArray());

                cmd.CommandText = string.Format("select count(Id) from Notifications{0};select Id,UserId,Date,Name,Description,Url,Level,IsRead,Category,RelatedId from Notifications{0} order by IsRead asc, Date desc", whereClause);

                using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                {
                    if (reader.Read())
                    {
                        result.TotalRecordCount = reader.GetInt32(0);
                    }

                    if (reader.NextResult())
                    {
                        var notifications = GetNotifications(reader);

                        if (query.StartIndex.HasValue)
                        {
                            notifications = notifications.Skip(query.StartIndex.Value);
                        }

                        if (query.Limit.HasValue)
                        {
                            notifications = notifications.Take(query.Limit.Value);
                        }

                        result.Notifications = notifications.ToArray();
                    }
                }

                return result;
            }
        }

        public NotificationsSummary GetNotificationsSummary(string userId)
        {
            var result = new NotificationsSummary();

            using (var cmd = _connection.CreateCommand())
            {
                cmd.CommandText = "select Level from Notifications where UserId=@UserId and IsRead=@IsRead";

                cmd.Parameters.Add(cmd, "@UserId", DbType.Guid).Value = new Guid(userId);
                cmd.Parameters.Add(cmd, "@IsRead", DbType.Boolean).Value = false;

                using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                {
                    var levels = new List<NotificationLevel>();

                    while (reader.Read())
                    {
                        levels.Add(GetLevel(reader, 0));
                    }

                    result.UnreadCount = levels.Count;

                    if (levels.Count > 0)
                    {
                        result.MaxUnreadNotificationLevel = levels.Max();
                    }
                }

                return result;
            }
        }

        /// <summary>
        /// Gets the notifications.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns>IEnumerable{Notification}.</returns>
        private IEnumerable<Notification> GetNotifications(IDataReader reader)
        {
            while (reader.Read())
            {
                yield return GetNotification(reader);
            }
        }

        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>
        /// Gets the level.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="index">The index.</param>
        /// <returns>NotificationLevel.</returns>
        private NotificationLevel GetLevel(IDataReader reader, int index)
        {
            NotificationLevel level;

            var val = reader.GetString(index);

            Enum.TryParse(val, true, out level);

            return level;
        }

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

        /// <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>
        /// Marks the read.
        /// </summary>
        /// <param name="notificationIdList">The notification id list.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="isRead">if set to <c>true</c> [is read].</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task MarkRead(IEnumerable<string> notificationIdList, string userId, bool isRead, CancellationToken cancellationToken)
        {
            var list = notificationIdList.ToList();
            var idArray = list.Select(i => new Guid(i)).ToArray();

            await MarkReadInternal(idArray, userId, isRead, cancellationToken).ConfigureAwait(false);

            if (NotificationsMarkedRead != null)
            {
                try
                {
                    NotificationsMarkedRead(this, new NotificationReadEventArgs
                    {
                        IdList = list.ToArray(),
                        IsRead = isRead,
                        UserId = userId
                    });
                }
                catch (Exception ex)
                {
                    Logger.ErrorException("Error in NotificationsMarkedRead event handler", ex);
                }
            }
        }

        public async Task MarkAllRead(string userId, bool isRead, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

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

            IDbTransaction transaction = null;

            try
            {
                cancellationToken.ThrowIfCancellationRequested();

                transaction = _connection.BeginTransaction();

                _markAllReadCommand.GetParameter(0).Value = new Guid(userId);
                _markAllReadCommand.GetParameter(1).Value = isRead;

                _markAllReadCommand.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();
            }
        }

        private async Task MarkReadInternal(IEnumerable<Guid> notificationIdList, string userId, bool isRead, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

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

            IDbTransaction transaction = null;

            try
            {
                cancellationToken.ThrowIfCancellationRequested();

                transaction = _connection.BeginTransaction();

                _markReadCommand.GetParameter(0).Value = new Guid(userId);
                _markReadCommand.GetParameter(1).Value = isRead;

                foreach (var id in notificationIdList)
                {
                    _markReadCommand.GetParameter(2).Value = id;

                    _markReadCommand.Transaction = transaction;

                    _markReadCommand.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();
            }
        }

        protected override void CloseConnection()
        {
            if (_connection != null)
            {
                if (_connection.IsOpen())
                {
                    _connection.Close();
                }

                _connection.Dispose();
                _connection = null;
            }
        }
    }
}