Rewrite activity log backend to use a query class.

This commit is contained in:
Patrick Barron
2020-10-03 21:14:25 -04:00
parent c0be770681
commit 4d7e7d6331
4 changed files with 58 additions and 38 deletions

View File

@@ -3,8 +3,10 @@ using System.Linq;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Events;
using Jellyfin.Data.Queries;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Querying;
using Microsoft.EntityFrameworkCore;
namespace Jellyfin.Server.Implementations.Activity
{
@@ -39,41 +41,34 @@ namespace Jellyfin.Server.Implementations.Activity
}
/// <inheritdoc/>
public QueryResult<ActivityLogEntry> GetPagedResult(
Func<IQueryable<ActivityLog>, IQueryable<ActivityLog>> func,
int? startIndex,
int? limit)
public async Task<QueryResult<ActivityLogEntry>> GetPagedResultAsync(ActivityLogQuery query)
{
using var dbContext = _provider.CreateContext();
await using var dbContext = _provider.CreateContext();
var query = func(dbContext.ActivityLogs.OrderByDescending(entry => entry.DateCreated));
IQueryable<ActivityLog> entries = dbContext.ActivityLogs.OrderByDescending(entry => entry.DateCreated);
if (startIndex.HasValue)
if (query.MinDate.HasValue)
{
query = query.Skip(startIndex.Value);
entries = entries.Where(entry => entry.DateCreated >= query.MinDate);
}
if (limit.HasValue)
if (query.HasUserId.HasValue)
{
query = query.Take(limit.Value);
entries = entries.Where(entry => entry.UserId != Guid.Empty == query.HasUserId.Value );
}
// This converts the objects from the new database model to the old for compatibility with the existing API.
var list = query.Select(ConvertToOldModel).ToList();
return new QueryResult<ActivityLogEntry>
{
Items = list,
TotalRecordCount = func(dbContext.ActivityLogs).Count()
Items = await entries.Skip(query.StartIndex ?? 0)
.Take(query.Limit ?? 100)
.Select(ConvertToOldModel)
.AsQueryable()
.ToListAsync()
.ConfigureAwait(false),
TotalRecordCount = await entries.CountAsync().ConfigureAwait(false)
};
}
/// <inheritdoc/>
public QueryResult<ActivityLogEntry> GetPagedResult(int? startIndex, int? limit)
{
return GetPagedResult(logs => logs, startIndex, limit);
}
private static ActivityLogEntry ConvertToOldModel(ActivityLog entry)
{
return new ActivityLogEntry