aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Activity/ActivityManager.cs
blob: 65ceee32bf8bceb0b15c6ba47ba1c731a46ebe7c (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
using System;
using System.Linq;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Querying;

namespace Jellyfin.Server.Implementations.Activity
{
    /// <summary>
    /// Manages the storage and retrieval of <see cref="ActivityLog"/> instances.
    /// </summary>
    public class ActivityManager : IActivityManager
    {
        private readonly JellyfinDbProvider _provider;

        /// <summary>
        /// Initializes a new instance of the <see cref="ActivityManager"/> class.
        /// </summary>
        /// <param name="provider">The Jellyfin database provider.</param>
        public ActivityManager(JellyfinDbProvider provider)
        {
            _provider = provider;
        }

        /// <inheritdoc/>
        public event EventHandler<GenericEventArgs<ActivityLogEntry>> EntryCreated;

        /// <inheritdoc/>
        public void Create(ActivityLog entry)
        {
            using var dbContext = _provider.CreateContext();
            dbContext.ActivityLogs.Add(entry);
            dbContext.SaveChanges();

            EntryCreated?.Invoke(this, new GenericEventArgs<ActivityLogEntry>(ConvertToOldModel(entry)));
        }

        /// <inheritdoc/>
        public async Task CreateAsync(ActivityLog entry)
        {
            using var dbContext = _provider.CreateContext();
            await dbContext.ActivityLogs.AddAsync(entry);
            await dbContext.SaveChangesAsync().ConfigureAwait(false);

            EntryCreated?.Invoke(this, new GenericEventArgs<ActivityLogEntry>(ConvertToOldModel(entry)));
        }

        /// <inheritdoc/>
        public QueryResult<ActivityLogEntry> GetPagedResult(
            Func<IQueryable<ActivityLog>, IQueryable<ActivityLog>> func,
            int? startIndex,
            int? limit)
        {
            using var dbContext = _provider.CreateContext();

            var query = func(dbContext.ActivityLogs.OrderByDescending(entry => entry.DateCreated));

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

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

            // This converts the objects from the new database model to the old for compatibility with the existing API.
            var list = query.Select(ConvertToOldModel).ToList();

            return new QueryResult<ActivityLogEntry>
            {
                Items = list,
                TotalRecordCount = func(dbContext.ActivityLogs).Count()
            };
        }

        /// <inheritdoc/>
        public QueryResult<ActivityLogEntry> GetPagedResult(int? startIndex, int? limit)
        {
            return GetPagedResult(logs => logs, startIndex, limit);
        }

        private static ActivityLogEntry ConvertToOldModel(ActivityLog entry)
        {
            return new ActivityLogEntry
            {
                Id = entry.Id,
                Name = entry.Name,
                Overview = entry.Overview,
                ShortOverview = entry.ShortOverview,
                Type = entry.Type,
                ItemId = entry.ItemId,
                UserId = entry.UserId,
                Date = entry.DateCreated,
                Severity = entry.LogSeverity
            };
        }
    }
}