PlaybackStopLogger.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Globalization;
  3. using System.Threading.Tasks;
  4. using Jellyfin.Data.Entities;
  5. using Jellyfin.Data.Enums;
  6. using MediaBrowser.Controller.Events;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Model.Activity;
  9. using MediaBrowser.Model.Dto;
  10. using MediaBrowser.Model.Entities;
  11. using MediaBrowser.Model.Globalization;
  12. using MediaBrowser.Model.Notifications;
  13. using Microsoft.Extensions.Logging;
  14. namespace Jellyfin.Server.Implementations.Events.Consumers.Session
  15. {
  16. /// <summary>
  17. /// Creates an activity log entry whenever a user stops playback.
  18. /// </summary>
  19. public class PlaybackStopLogger : IEventConsumer<PlaybackStopEventArgs>
  20. {
  21. private readonly ILogger<PlaybackStopLogger> _logger;
  22. private readonly ILocalizationManager _localizationManager;
  23. private readonly IActivityManager _activityManager;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="PlaybackStopLogger"/> class.
  26. /// </summary>
  27. /// <param name="logger">The logger.</param>
  28. /// <param name="localizationManager">The localization manager.</param>
  29. /// <param name="activityManager">The activity manager.</param>
  30. public PlaybackStopLogger(ILogger<PlaybackStopLogger> logger, ILocalizationManager localizationManager, IActivityManager activityManager)
  31. {
  32. _logger = logger;
  33. _localizationManager = localizationManager;
  34. _activityManager = activityManager;
  35. }
  36. /// <inheritdoc />
  37. public async Task OnEvent(PlaybackStopEventArgs eventArgs)
  38. {
  39. var item = eventArgs.MediaInfo;
  40. if (item is null)
  41. {
  42. _logger.LogWarning("PlaybackStopped reported with null media info.");
  43. return;
  44. }
  45. if (eventArgs.Item is not null && eventArgs.Item.IsThemeMedia)
  46. {
  47. // Don't report theme song or local trailer playback
  48. return;
  49. }
  50. if (eventArgs.Users.Count == 0)
  51. {
  52. return;
  53. }
  54. var user = eventArgs.Users[0];
  55. var notificationType = GetPlaybackStoppedNotificationType(item.MediaType);
  56. if (notificationType is null)
  57. {
  58. return;
  59. }
  60. await _activityManager.CreateAsync(new ActivityLog(
  61. string.Format(
  62. CultureInfo.InvariantCulture,
  63. _localizationManager.GetLocalizedString("UserStoppedPlayingItemWithValues"),
  64. user.Username,
  65. GetItemName(item),
  66. eventArgs.DeviceName),
  67. notificationType,
  68. user.Id)
  69. {
  70. ItemId = eventArgs.Item?.Id.ToString("N", CultureInfo.InvariantCulture),
  71. })
  72. .ConfigureAwait(false);
  73. }
  74. private static string GetItemName(BaseItemDto item)
  75. {
  76. var name = item.Name;
  77. if (!string.IsNullOrEmpty(item.SeriesName))
  78. {
  79. name = item.SeriesName + " - " + name;
  80. }
  81. if (item.Artists is not null && item.Artists.Count > 0)
  82. {
  83. name = item.Artists[0] + " - " + name;
  84. }
  85. return name;
  86. }
  87. private static string? GetPlaybackStoppedNotificationType(MediaType mediaType)
  88. {
  89. if (mediaType == MediaType.Audio)
  90. {
  91. return NotificationType.AudioPlaybackStopped.ToString();
  92. }
  93. if (mediaType == MediaType.Video)
  94. {
  95. return NotificationType.VideoPlaybackStopped.ToString();
  96. }
  97. return null;
  98. }
  99. }
  100. }