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-05-02 15:32:22 -07:00
|
|
|
using MediaBrowser.Model.Activity;
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
|
|
|
|
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/>
|
|
|
|
public event EventHandler<GenericEventArgs<ActivityLogEntry>> EntryCreated;
|
|
|
|
|
|
|
|
/// <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/>
|
|
|
|
public QueryResult<ActivityLogEntry> GetPagedResult(
|
2020-05-14 14:13:45 -07:00
|
|
|
Func<IQueryable<ActivityLog>, IQueryable<ActivityLog>> func,
|
2020-05-02 15:32:22 -07:00
|
|
|
int? startIndex,
|
|
|
|
int? limit)
|
|
|
|
{
|
|
|
|
using var dbContext = _provider.CreateContext();
|
|
|
|
|
2020-05-15 09:51:18 -07:00
|
|
|
var query = func(dbContext.ActivityLogs.OrderByDescending(entry => entry.DateCreated));
|
2020-05-02 15:32:22 -07:00
|
|
|
|
|
|
|
if (startIndex.HasValue)
|
|
|
|
{
|
2020-05-14 14:13:45 -07:00
|
|
|
query = query.Skip(startIndex.Value);
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (limit.HasValue)
|
|
|
|
{
|
2020-05-14 14:13:45 -07:00
|
|
|
query = query.Take(limit.Value);
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This converts the objects from the new database model to the old for compatibility with the existing API.
|
2020-05-15 09:51:18 -07:00
|
|
|
var list = query.Select(ConvertToOldModel).ToList();
|
2020-05-02 15:32:22 -07:00
|
|
|
|
2020-05-14 14:13:45 -07:00
|
|
|
return new QueryResult<ActivityLogEntry>
|
2020-05-02 15:32:22 -07:00
|
|
|
{
|
|
|
|
Items = list,
|
2020-05-15 09:51:18 -07:00
|
|
|
TotalRecordCount = func(dbContext.ActivityLogs).Count()
|
2020-05-02 15:32:22 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public QueryResult<ActivityLogEntry> GetPagedResult(int? startIndex, int? limit)
|
|
|
|
{
|
|
|
|
return GetPagedResult(logs => logs, startIndex, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|