2020-05-02 22:32:22 +00:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Jellyfin.Data.Entities;
|
2020-08-14 00:48:28 +00:00
|
|
|
using Jellyfin.Data.Events;
|
2020-10-04 01:14:25 +00:00
|
|
|
using Jellyfin.Data.Queries;
|
2020-05-02 22:32:22 +00:00
|
|
|
using MediaBrowser.Model.Activity;
|
|
|
|
using MediaBrowser.Model.Querying;
|
2020-10-04 01:14:25 +00:00
|
|
|
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
|
|
|
|
{
|
2020-05-13 19:03:35 +00:00
|
|
|
private readonly JellyfinDbProvider _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(JellyfinDbProvider provider)
|
|
|
|
{
|
|
|
|
_provider = provider;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2020-12-12 16:20:48 +00:00
|
|
|
public event EventHandler<GenericEventArgs<ActivityLogEntry>>? EntryCreated;
|
2020-05-02 22:32:22 +00:00
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public async Task CreateAsync(ActivityLog entry)
|
|
|
|
{
|
2020-08-01 20:20:08 +00:00
|
|
|
await using var dbContext = _provider.CreateContext();
|
|
|
|
|
|
|
|
dbContext.ActivityLogs.Add(entry);
|
2020-05-02 22:32:22 +00:00
|
|
|
await dbContext.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
EntryCreated?.Invoke(this, new GenericEventArgs<ActivityLogEntry>(ConvertToOldModel(entry)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2020-10-04 01:14:25 +00:00
|
|
|
public async Task<QueryResult<ActivityLogEntry>> GetPagedResultAsync(ActivityLogQuery query)
|
2020-05-02 22:32:22 +00:00
|
|
|
{
|
2020-10-04 01:14:25 +00:00
|
|
|
await using var dbContext = _provider.CreateContext();
|
2020-05-02 22:32:22 +00:00
|
|
|
|
2020-10-06 02:51:52 +00:00
|
|
|
IQueryable<ActivityLog> entries = dbContext.ActivityLogs
|
|
|
|
.AsQueryable()
|
|
|
|
.OrderByDescending(entry => entry.DateCreated);
|
2020-05-02 22:32:22 +00:00
|
|
|
|
2020-10-04 01:14:25 +00:00
|
|
|
if (query.MinDate.HasValue)
|
2020-05-02 22:32:22 +00:00
|
|
|
{
|
2020-10-04 01:14:25 +00:00
|
|
|
entries = entries.Where(entry => entry.DateCreated >= query.MinDate);
|
2020-05-02 22:32:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 01:14:25 +00:00
|
|
|
if (query.HasUserId.HasValue)
|
2020-05-02 22:32:22 +00:00
|
|
|
{
|
2020-10-04 01:14:25 +00:00
|
|
|
entries = entries.Where(entry => entry.UserId != Guid.Empty == query.HasUserId.Value );
|
2020-05-02 22:32:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 21:13:45 +00:00
|
|
|
return new QueryResult<ActivityLogEntry>
|
2020-05-02 22:32:22 +00:00
|
|
|
{
|
2020-10-06 02:51:52 +00:00
|
|
|
Items = await entries
|
|
|
|
.Skip(query.StartIndex ?? 0)
|
2020-10-04 01:14:25 +00:00
|
|
|
.Take(query.Limit ?? 100)
|
2020-10-06 02:51:52 +00:00
|
|
|
.AsAsyncEnumerable()
|
2020-10-04 01:14:25 +00:00
|
|
|
.Select(ConvertToOldModel)
|
|
|
|
.ToListAsync()
|
|
|
|
.ConfigureAwait(false),
|
|
|
|
TotalRecordCount = await entries.CountAsync().ConfigureAwait(false)
|
2020-05-02 22:32:22 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-14 17:44:11 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public async Task CleanAsync(DateTime startDate)
|
|
|
|
{
|
|
|
|
await using var dbContext = _provider.CreateContext();
|
|
|
|
var entries = dbContext.ActivityLogs
|
|
|
|
.AsQueryable()
|
|
|
|
.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)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|