ActivityManager.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Jellyfin.Data.Entities;
  6. using MediaBrowser.Model.Activity;
  7. using MediaBrowser.Model.Events;
  8. using MediaBrowser.Model.Querying;
  9. namespace Jellyfin.Server.Implementations.Activity
  10. {
  11. /// <summary>
  12. /// Manages the storage and retrieval of <see cref="ActivityLog"/> instances.
  13. /// </summary>
  14. public class ActivityManager : IActivityManager
  15. {
  16. private JellyfinDbProvider _provider;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="ActivityManager"/> class.
  19. /// </summary>
  20. /// <param name="provider">The Jellyfin database provider.</param>
  21. public ActivityManager(JellyfinDbProvider provider)
  22. {
  23. _provider = provider;
  24. }
  25. /// <inheritdoc/>
  26. public event EventHandler<GenericEventArgs<ActivityLogEntry>> EntryCreated;
  27. /// <inheritdoc/>
  28. public void Create(ActivityLog entry)
  29. {
  30. using var dbContext = _provider.CreateContext();
  31. dbContext.ActivityLogs.Add(entry);
  32. dbContext.SaveChanges();
  33. EntryCreated?.Invoke(this, new GenericEventArgs<ActivityLogEntry>(ConvertToOldModel(entry)));
  34. }
  35. /// <inheritdoc/>
  36. public async Task CreateAsync(ActivityLog entry)
  37. {
  38. using var dbContext = _provider.CreateContext();
  39. await dbContext.ActivityLogs.AddAsync(entry);
  40. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  41. EntryCreated?.Invoke(this, new GenericEventArgs<ActivityLogEntry>(ConvertToOldModel(entry)));
  42. }
  43. /// <inheritdoc/>
  44. public QueryResult<ActivityLogEntry> GetPagedResult(
  45. Func<IQueryable<ActivityLog>, IEnumerable<ActivityLog>> func,
  46. int? startIndex,
  47. int? limit)
  48. {
  49. using var dbContext = _provider.CreateContext();
  50. var result = func.Invoke(dbContext.ActivityLogs).AsQueryable();
  51. if (startIndex.HasValue)
  52. {
  53. result = result.Where(entry => entry.Id >= startIndex.Value);
  54. }
  55. if (limit.HasValue)
  56. {
  57. result = result.OrderByDescending(entry => entry.DateCreated).Take(limit.Value);
  58. }
  59. // This converts the objects from the new database model to the old for compatibility with the existing API.
  60. var list = result.Select(entry => ConvertToOldModel(entry)).ToList();
  61. return new QueryResult<ActivityLogEntry>()
  62. {
  63. Items = list,
  64. TotalRecordCount = list.Count
  65. };
  66. }
  67. /// <inheritdoc/>
  68. public QueryResult<ActivityLogEntry> GetPagedResult(int? startIndex, int? limit)
  69. {
  70. return GetPagedResult(logs => logs, startIndex, limit);
  71. }
  72. private static ActivityLogEntry ConvertToOldModel(ActivityLog entry)
  73. {
  74. return new ActivityLogEntry
  75. {
  76. Id = entry.Id,
  77. Name = entry.Name,
  78. Overview = entry.Overview,
  79. ShortOverview = entry.ShortOverview,
  80. Type = entry.Type,
  81. ItemId = entry.ItemId,
  82. UserId = entry.UserId,
  83. Date = entry.DateCreated,
  84. Severity = entry.LogSeverity
  85. };
  86. }
  87. }
  88. }