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
|
|
|
|
{
|
2023-01-16 17:14:44 +00:00
|
|
|
private readonly IDbContextFactory<JellyfinDbContext> _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>
|
2023-01-16 17:14:44 +00:00
|
|
|
public ActivityManager(IDbContextFactory<JellyfinDbContext> provider)
|
2020-05-02 22:32:22 +00:00
|
|
|
{
|
|
|
|
_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)
|
|
|
|
{
|
2022-10-21 09:55:32 +00:00
|
|
|
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/>
|
2020-10-04 01:14:25 +00:00
|
|
|
public async Task<QueryResult<ActivityLogEntry>> GetPagedResultAsync(ActivityLogQuery query)
|
2020-05-02 22:32:22 +00:00
|
|
|
{
|
2022-10-21 09:55:32 +00:00
|
|
|
var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
|
|
|
|
await using (dbContext.ConfigureAwait(false))
|
|
|
|
{
|
2023-01-16 16:49:59 +00:00
|
|
|
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
|
|
|
|
2022-10-21 09:55:32 +00:00
|
|
|
return new QueryResult<ActivityLogEntry>(
|
|
|
|
query.Skip,
|
|
|
|
await entries.CountAsync().ConfigureAwait(false),
|
|
|
|
await entries
|
|
|
|
.Skip(query.Skip ?? 0)
|
|
|
|
.Take(query.Limit ?? 100)
|
2023-01-16 16:49:59 +00:00
|
|
|
.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()
|
2022-10-21 09:55:32 +00:00
|
|
|
.ToListAsync()
|
|
|
|
.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)
|
|
|
|
{
|
2022-10-21 09:55:32 +00:00
|
|
|
var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
|
|
|
|
await using (dbContext.ConfigureAwait(false))
|
|
|
|
{
|
|
|
|
var entries = dbContext.ActivityLogs
|
|
|
|
.Where(entry => entry.DateCreated <= startDate);
|
2020-10-14 17:44:11 +00:00
|
|
|
|
2022-10-21 09:55:32 +00:00
|
|
|
dbContext.RemoveRange(entries);
|
|
|
|
await dbContext.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
}
|
2020-10-14 17:44:11 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|