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

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

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

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

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

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

        /// <inheritdoc/>
        public async Task<QueryResult<ActivityLogEntry>> GetPagedResultAsync(ActivityLogQuery query)
        {
            var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
            await using (dbContext.ConfigureAwait(false))
            {
                var entries = dbContext.ActivityLogs
                    .OrderByDescending(entry => entry.DateCreated)
                    .Where(entry => query.MinDate == null || entry.DateCreated >= query.MinDate)
                    .Where(entry => !query.HasUserId.HasValue || entry.UserId.Equals(default) != query.HasUserId.Value);

                return new QueryResult<ActivityLogEntry>(
                    query.Skip,
                    await entries.CountAsync().ConfigureAwait(false),
                    await entries
                        .Skip(query.Skip ?? 0)
                        .Take(query.Limit ?? 100)
                        .Select(entity => new ActivityLogEntry(entity.Name, entity.Type, entity.UserId)
                        {
                            Id = entity.Id,
                            Overview = entity.Overview,
                            ShortOverview = entity.ShortOverview,
                            ItemId = entity.ItemId,
                            Date = entity.DateCreated,
                            Severity = entity.LogSeverity
                        })
                        .ToListAsync()
                        .ConfigureAwait(false));
            }
        }

        /// <inheritdoc />
        public async Task CleanAsync(DateTime startDate)
        {
            var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
            await using (dbContext.ConfigureAwait(false))
            {
                await dbContext.ActivityLogs
                    .Where(entry => entry.DateCreated <= startDate)
                    .ExecuteDeleteAsync()
                    .ConfigureAwait(false);
            }
        }

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