jellyfin/Jellyfin.Server.Implementations/Activity/ActivityManager.cs

105 lines
4.0 KiB
C#
Raw Normal View History

2020-05-02 22:32:22 +00:00
using System;
using System.Linq;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Events;
using Jellyfin.Data.Queries;
2020-05-02 22:32:22 +00:00
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Querying;
using Microsoft.EntityFrameworkCore;
2020-05-02 22:32:22 +00:00
namespace Jellyfin.Server.Implementations.Activity
{
/// <summary>
/// Manages the storage and retrieval of <see cref="ActivityLog"/> instances.
/// </summary>
public class ActivityManager : IActivityManager
{
private readonly IDbContextFactory<JellyfinDb> _provider;
2020-05-02 22:32:22 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="ActivityManager"/> class.
/// </summary>
/// <param name="provider">The Jellyfin database provider.</param>
public ActivityManager(IDbContextFactory<JellyfinDb> provider)
2020-05-02 22:32:22 +00:00
{
_provider = provider;
}
/// <inheritdoc/>
public event EventHandler<GenericEventArgs<ActivityLogEntry>>? EntryCreated;
2020-05-02 22:32:22 +00:00
/// <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);
}
2020-05-02 22:32:22 +00:00
EntryCreated?.Invoke(this, new GenericEventArgs<ActivityLogEntry>(ConvertToOldModel(entry)));
}
/// <inheritdoc/>
public async Task<QueryResult<ActivityLogEntry>> GetPagedResultAsync(ActivityLogQuery query)
2020-05-02 22:32:22 +00:00
{
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);
2020-05-02 22:32:22 +00:00
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
})
.AsQueryable()
.ToListAsync()
.ConfigureAwait(false));
2020-05-02 22:32:22 +00:00
}
}
/// <inheritdoc />
public async Task CleanAsync(DateTime startDate)
{
var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
{
var entries = dbContext.ActivityLogs
.Where(entry => entry.DateCreated <= startDate);
dbContext.RemoveRange(entries);
await dbContext.SaveChangesAsync().ConfigureAwait(false);
}
}
2020-05-02 22:32:22 +00:00
private static ActivityLogEntry ConvertToOldModel(ActivityLog entry)
{
2021-07-26 21:02:32 +00:00
return new ActivityLogEntry(entry.Name, entry.Type, entry.UserId)
2020-05-02 22:32:22 +00:00
{
Id = entry.Id,
Overview = entry.Overview,
ShortOverview = entry.ShortOverview,
ItemId = entry.ItemId,
Date = entry.DateCreated,
Severity = entry.LogSeverity
};
}
}
}