2020-05-02 15:32:22 -07:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Jellyfin.Data.Entities;
|
2020-08-13 17:48:28 -07:00
|
|
|
using Jellyfin.Data.Events;
|
2020-10-03 18:14:25 -07:00
|
|
|
using Jellyfin.Data.Queries;
|
2020-05-02 15:32:22 -07:00
|
|
|
using MediaBrowser.Model.Activity;
|
|
|
|
using MediaBrowser.Model.Querying;
|
2020-10-03 18:14:25 -07:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-05-02 15:32:22 -07: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 12:03:35 -07:00
|
|
|
private readonly JellyfinDbProvider _provider;
|
2020-05-02 15:32:22 -07: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 09:20:48 -07:00
|
|
|
public event EventHandler<GenericEventArgs<ActivityLogEntry>>? EntryCreated;
|
2020-05-02 15:32:22 -07:00
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public async Task CreateAsync(ActivityLog entry)
|
|
|
|
{
|
2020-08-01 13:20:08 -07:00
|
|
|
await using var dbContext = _provider.CreateContext();
|
|
|
|
|
|
|
|
dbContext.ActivityLogs.Add(entry);
|
2020-05-02 15:32:22 -07:00
|
|
|
await dbContext.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
EntryCreated?.Invoke(this, new GenericEventArgs<ActivityLogEntry>(ConvertToOldModel(entry)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2020-10-03 18:14:25 -07:00
|
|
|
public async Task<QueryResult<ActivityLogEntry>> GetPagedResultAsync(ActivityLogQuery query)
|
2020-05-02 15:32:22 -07:00
|
|
|
{
|
2020-10-03 18:14:25 -07:00
|
|
|
await using var dbContext = _provider.CreateContext();
|
2020-05-02 15:32:22 -07:00
|
|
|
|
2020-10-05 19:51:52 -07:00
|
|
|
IQueryable<ActivityLog> entries = dbContext.ActivityLogs
|
|
|
|
.AsQueryable()
|
|
|
|
.OrderByDescending(entry => entry.DateCreated);
|
2020-05-02 15:32:22 -07:00
|
|
|
|
2020-10-03 18:14:25 -07:00
|
|
|
if (query.MinDate.HasValue)
|
2020-05-02 15:32:22 -07:00
|
|
|
{
|
2020-10-03 18:14:25 -07:00
|
|
|
entries = entries.Where(entry => entry.DateCreated >= query.MinDate);
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
2020-10-03 18:14:25 -07:00
|
|
|
if (query.HasUserId.HasValue)
|
2020-05-02 15:32:22 -07:00
|
|
|
{
|
2022-02-21 06:15:09 -07:00
|
|
|
entries = entries.Where(entry => (!entry.UserId.Equals(default)) == query.HasUserId.Value);
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
2022-01-20 08:46:17 -07:00
|
|
|
return new QueryResult<ActivityLogEntry>(
|
|
|
|
query.Skip,
|
|
|
|
await entries.CountAsync().ConfigureAwait(false),
|
|
|
|
await entries
|
2021-05-20 17:48:41 -07:00
|
|
|
.Skip(query.Skip ?? 0)
|
2020-10-03 18:14:25 -07:00
|
|
|
.Take(query.Limit ?? 100)
|
2020-10-05 19:51:52 -07:00
|
|
|
.AsAsyncEnumerable()
|
2020-10-03 18:14:25 -07:00
|
|
|
.Select(ConvertToOldModel)
|
|
|
|
.ToListAsync()
|
2022-01-20 08:46:17 -07:00
|
|
|
.ConfigureAwait(false));
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
2020-10-14 10:44:11 -07: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 15:32:22 -07:00
|
|
|
private static ActivityLogEntry ConvertToOldModel(ActivityLog entry)
|
|
|
|
{
|
2021-07-26 14:02:32 -07:00
|
|
|
return new ActivityLogEntry(entry.Name, entry.Type, entry.UserId)
|
2020-05-02 15:32:22 -07:00
|
|
|
{
|
|
|
|
Id = entry.Id,
|
|
|
|
Overview = entry.Overview,
|
|
|
|
ShortOverview = entry.ShortOverview,
|
|
|
|
ItemId = entry.ItemId,
|
|
|
|
Date = entry.DateCreated,
|
|
|
|
Severity = entry.LogSeverity
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|