ActivityManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Jellyfin.Data.Entities;
  5. using Jellyfin.Data.Events;
  6. using Jellyfin.Data.Queries;
  7. using MediaBrowser.Model.Activity;
  8. using MediaBrowser.Model.Querying;
  9. using Microsoft.EntityFrameworkCore;
  10. namespace Jellyfin.Server.Implementations.Activity
  11. {
  12. /// <summary>
  13. /// Manages the storage and retrieval of <see cref="ActivityLog"/> instances.
  14. /// </summary>
  15. public class ActivityManager : IActivityManager
  16. {
  17. private readonly IDbContextFactory<JellyfinDb> _provider;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="ActivityManager"/> class.
  20. /// </summary>
  21. /// <param name="provider">The Jellyfin database provider.</param>
  22. public ActivityManager(IDbContextFactory<JellyfinDb> provider)
  23. {
  24. _provider = provider;
  25. }
  26. /// <inheritdoc/>
  27. public event EventHandler<GenericEventArgs<ActivityLogEntry>>? EntryCreated;
  28. /// <inheritdoc/>
  29. public async Task CreateAsync(ActivityLog entry)
  30. {
  31. var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
  32. await using (dbContext.ConfigureAwait(false))
  33. {
  34. dbContext.ActivityLogs.Add(entry);
  35. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  36. }
  37. EntryCreated?.Invoke(this, new GenericEventArgs<ActivityLogEntry>(ConvertToOldModel(entry)));
  38. }
  39. /// <inheritdoc/>
  40. public async Task<QueryResult<ActivityLogEntry>> GetPagedResultAsync(ActivityLogQuery query)
  41. {
  42. var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
  43. await using (dbContext.ConfigureAwait(false))
  44. {
  45. IQueryable<ActivityLog> entries = dbContext.ActivityLogs
  46. .OrderByDescending(entry => entry.DateCreated);
  47. if (query.MinDate.HasValue)
  48. {
  49. entries = entries.Where(entry => entry.DateCreated >= query.MinDate);
  50. }
  51. if (query.HasUserId.HasValue)
  52. {
  53. entries = entries.Where(entry => (!entry.UserId.Equals(default)) == query.HasUserId.Value);
  54. }
  55. return new QueryResult<ActivityLogEntry>(
  56. query.Skip,
  57. await entries.CountAsync().ConfigureAwait(false),
  58. await entries
  59. .Skip(query.Skip ?? 0)
  60. .Take(query.Limit ?? 100)
  61. .AsAsyncEnumerable()
  62. .Select(ConvertToOldModel)
  63. .ToListAsync()
  64. .ConfigureAwait(false));
  65. }
  66. }
  67. /// <inheritdoc />
  68. public async Task CleanAsync(DateTime startDate)
  69. {
  70. var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
  71. await using (dbContext.ConfigureAwait(false))
  72. {
  73. var entries = dbContext.ActivityLogs
  74. .Where(entry => entry.DateCreated <= startDate);
  75. dbContext.RemoveRange(entries);
  76. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  77. }
  78. }
  79. private static ActivityLogEntry ConvertToOldModel(ActivityLog entry)
  80. {
  81. return new ActivityLogEntry(entry.Name, entry.Type, entry.UserId)
  82. {
  83. Id = entry.Id,
  84. Overview = entry.Overview,
  85. ShortOverview = entry.ShortOverview,
  86. ItemId = entry.ItemId,
  87. Date = entry.DateCreated,
  88. Severity = entry.LogSeverity
  89. };
  90. }
  91. }
  92. }