using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Querying;
namespace Jellyfin.Server.Implementations.Activity
{
///
/// Manages the storage and retrieval of instances.
///
public class ActivityManager : IActivityManager
{
private readonly JellyfinDbProvider _provider;
///
/// Initializes a new instance of the class.
///
/// The Jellyfin database provider.
public ActivityManager(JellyfinDbProvider provider)
{
_provider = provider;
}
///
public event EventHandler> EntryCreated;
///
public void Create(ActivityLog entry)
{
using var dbContext = _provider.CreateContext();
dbContext.ActivityLogs.Add(entry);
dbContext.SaveChanges();
EntryCreated?.Invoke(this, new GenericEventArgs(ConvertToOldModel(entry)));
}
///
public async Task CreateAsync(ActivityLog entry)
{
using var dbContext = _provider.CreateContext();
await dbContext.ActivityLogs.AddAsync(entry);
await dbContext.SaveChangesAsync().ConfigureAwait(false);
EntryCreated?.Invoke(this, new GenericEventArgs(ConvertToOldModel(entry)));
}
///
public QueryResult GetPagedResult(
Func, IQueryable> func,
int? startIndex,
int? limit)
{
using var dbContext = _provider.CreateContext();
var query = func(dbContext.ActivityLogs).OrderByDescending(entry => entry.DateCreated).AsQueryable();
if (startIndex.HasValue)
{
query = query.Skip(startIndex.Value);
}
if (limit.HasValue)
{
query = query.Take(limit.Value);
}
// This converts the objects from the new database model to the old for compatibility with the existing API.
var list = query.AsEnumerable().Select(ConvertToOldModel).ToList();
return new QueryResult
{
Items = list,
TotalRecordCount = dbContext.ActivityLogs.Count()
};
}
///
public QueryResult 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
};
}
}
}