瀏覽代碼

Migrate ActivityLogEntryPoint.OnPlaybackStopped to IEventConsumer

Patrick Barron 4 年之前
父節點
當前提交
5c29b8982d

+ 0 - 76
Emby.Server.Implementations/Activity/ActivityLogEntryPoint.cs

@@ -12,8 +12,6 @@ using MediaBrowser.Controller.Plugins;
 using MediaBrowser.Controller.Session;
 using MediaBrowser.Controller.Session;
 using MediaBrowser.Controller.Subtitles;
 using MediaBrowser.Controller.Subtitles;
 using MediaBrowser.Model.Activity;
 using MediaBrowser.Model.Activity;
-using MediaBrowser.Model.Dto;
-using MediaBrowser.Model.Entities;
 using MediaBrowser.Model.Events;
 using MediaBrowser.Model.Events;
 using MediaBrowser.Model.Globalization;
 using MediaBrowser.Model.Globalization;
 using MediaBrowser.Model.Notifications;
 using MediaBrowser.Model.Notifications;
@@ -28,7 +26,6 @@ namespace Emby.Server.Implementations.Activity
     /// </summary>
     /// </summary>
     public sealed class ActivityLogEntryPoint : IServerEntryPoint
     public sealed class ActivityLogEntryPoint : IServerEntryPoint
     {
     {
-        private readonly ILogger<ActivityLogEntryPoint> _logger;
         private readonly IInstallationManager _installationManager;
         private readonly IInstallationManager _installationManager;
         private readonly ISessionManager _sessionManager;
         private readonly ISessionManager _sessionManager;
         private readonly ITaskManager _taskManager;
         private readonly ITaskManager _taskManager;
@@ -40,7 +37,6 @@ namespace Emby.Server.Implementations.Activity
         /// <summary>
         /// <summary>
         /// Initializes a new instance of the <see cref="ActivityLogEntryPoint"/> class.
         /// Initializes a new instance of the <see cref="ActivityLogEntryPoint"/> class.
         /// </summary>
         /// </summary>
-        /// <param name="logger">The logger.</param>
         /// <param name="sessionManager">The session manager.</param>
         /// <param name="sessionManager">The session manager.</param>
         /// <param name="taskManager">The task manager.</param>
         /// <param name="taskManager">The task manager.</param>
         /// <param name="activityManager">The activity manager.</param>
         /// <param name="activityManager">The activity manager.</param>
@@ -49,7 +45,6 @@ namespace Emby.Server.Implementations.Activity
         /// <param name="subManager">The subtitle manager.</param>
         /// <param name="subManager">The subtitle manager.</param>
         /// <param name="userManager">The user manager.</param>
         /// <param name="userManager">The user manager.</param>
         public ActivityLogEntryPoint(
         public ActivityLogEntryPoint(
-            ILogger<ActivityLogEntryPoint> logger,
             ISessionManager sessionManager,
             ISessionManager sessionManager,
             ITaskManager taskManager,
             ITaskManager taskManager,
             IActivityManager activityManager,
             IActivityManager activityManager,
@@ -58,7 +53,6 @@ namespace Emby.Server.Implementations.Activity
             ISubtitleManager subManager,
             ISubtitleManager subManager,
             IUserManager userManager)
             IUserManager userManager)
         {
         {
-            _logger = logger;
             _sessionManager = sessionManager;
             _sessionManager = sessionManager;
             _taskManager = taskManager;
             _taskManager = taskManager;
             _activityManager = activityManager;
             _activityManager = activityManager;
@@ -82,7 +76,6 @@ namespace Emby.Server.Implementations.Activity
             _sessionManager.AuthenticationFailed += OnAuthenticationFailed;
             _sessionManager.AuthenticationFailed += OnAuthenticationFailed;
             _sessionManager.AuthenticationSucceeded += OnAuthenticationSucceeded;
             _sessionManager.AuthenticationSucceeded += OnAuthenticationSucceeded;
             _sessionManager.SessionEnded += OnSessionEnded;
             _sessionManager.SessionEnded += OnSessionEnded;
-            _sessionManager.PlaybackStopped += OnPlaybackStopped;
 
 
             _subManager.SubtitleDownloadFailure += OnSubtitleDownloadFailure;
             _subManager.SubtitleDownloadFailure += OnSubtitleDownloadFailure;
 
 
@@ -124,73 +117,6 @@ namespace Emby.Server.Implementations.Activity
             }).ConfigureAwait(false);
             }).ConfigureAwait(false);
         }
         }
 
 
-        private async void OnPlaybackStopped(object sender, PlaybackStopEventArgs e)
-        {
-            var item = e.MediaInfo;
-
-            if (item == null)
-            {
-                _logger.LogWarning("PlaybackStopped reported with null media info.");
-                return;
-            }
-
-            if (e.Item != null && e.Item.IsThemeMedia)
-            {
-                // Don't report theme song or local trailer playback
-                return;
-            }
-
-            if (e.Users.Count == 0)
-            {
-                return;
-            }
-
-            var user = e.Users[0];
-
-            await CreateLogEntry(new ActivityLog(
-                string.Format(
-                    CultureInfo.InvariantCulture,
-                    _localization.GetLocalizedString("UserStoppedPlayingItemWithValues"),
-                    user.Username,
-                    GetItemName(item),
-                    e.DeviceName),
-                GetPlaybackStoppedNotificationType(item.MediaType),
-                user.Id))
-                .ConfigureAwait(false);
-        }
-
-        private static string GetItemName(BaseItemDto item)
-        {
-            var name = item.Name;
-
-            if (!string.IsNullOrEmpty(item.SeriesName))
-            {
-                name = item.SeriesName + " - " + name;
-            }
-
-            if (item.Artists != null && item.Artists.Count > 0)
-            {
-                name = item.Artists[0] + " - " + name;
-            }
-
-            return name;
-        }
-
-        private static string GetPlaybackStoppedNotificationType(string mediaType)
-        {
-            if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
-            {
-                return NotificationType.AudioPlaybackStopped.ToString();
-            }
-
-            if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
-            {
-                return NotificationType.VideoPlaybackStopped.ToString();
-            }
-
-            return null;
-        }
-
         private async void OnSessionEnded(object sender, SessionEventArgs e)
         private async void OnSessionEnded(object sender, SessionEventArgs e)
         {
         {
             var session = e.SessionInfo;
             var session = e.SessionInfo;
@@ -442,8 +368,6 @@ namespace Emby.Server.Implementations.Activity
             _sessionManager.AuthenticationSucceeded -= OnAuthenticationSucceeded;
             _sessionManager.AuthenticationSucceeded -= OnAuthenticationSucceeded;
             _sessionManager.SessionEnded -= OnSessionEnded;
             _sessionManager.SessionEnded -= OnSessionEnded;
 
 
-            _sessionManager.PlaybackStopped -= OnPlaybackStopped;
-
             _subManager.SubtitleDownloadFailure -= OnSubtitleDownloadFailure;
             _subManager.SubtitleDownloadFailure -= OnSubtitleDownloadFailure;
 
 
             _userManager.OnUserCreated -= OnUserCreated;
             _userManager.OnUserCreated -= OnUserCreated;

+ 106 - 0
Jellyfin.Server.Implementations/Events/Consumers/Session/PlaybackStopLogger.cs

@@ -0,0 +1,106 @@
+using System;
+using System.Globalization;
+using System.Threading.Tasks;
+using Jellyfin.Data.Entities;
+using MediaBrowser.Controller.Events;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Activity;
+using MediaBrowser.Model.Dto;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.Globalization;
+using MediaBrowser.Model.Notifications;
+using Microsoft.Extensions.Logging;
+
+namespace Jellyfin.Server.Implementations.Events.Consumers.Session
+{
+    /// <summary>
+    /// Creates an activity log entry whenever a user stops playback.
+    /// </summary>
+    public class PlaybackStopLogger : IEventConsumer<PlaybackStopEventArgs>
+    {
+        private readonly ILogger<PlaybackStopLogger> _logger;
+        private readonly ILocalizationManager _localizationManager;
+        private readonly IActivityManager _activityManager;
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="PlaybackStopLogger"/> class.
+        /// </summary>
+        /// <param name="logger">The logger.</param>
+        /// <param name="localizationManager">The localization manager.</param>
+        /// <param name="activityManager">The activity manager.</param>
+        public PlaybackStopLogger(ILogger<PlaybackStopLogger> logger, ILocalizationManager localizationManager, IActivityManager activityManager)
+        {
+            _logger = logger;
+            _localizationManager = localizationManager;
+            _activityManager = activityManager;
+        }
+
+        /// <inheritdoc />
+        public async Task OnEvent(PlaybackStopEventArgs eventArgs)
+        {
+            var item = eventArgs.MediaInfo;
+
+            if (item == null)
+            {
+                _logger.LogWarning("PlaybackStopped reported with null media info.");
+                return;
+            }
+
+            if (eventArgs.Item != null && eventArgs.Item.IsThemeMedia)
+            {
+                // Don't report theme song or local trailer playback
+                return;
+            }
+
+            if (eventArgs.Users.Count == 0)
+            {
+                return;
+            }
+
+            var user = eventArgs.Users[0];
+
+            await _activityManager.CreateAsync(new ActivityLog(
+                    string.Format(
+                        CultureInfo.InvariantCulture,
+                        _localizationManager.GetLocalizedString("UserStoppedPlayingItemWithValues"),
+                        user.Username,
+                        GetItemName(item),
+                        eventArgs.DeviceName),
+                    GetPlaybackStoppedNotificationType(item.MediaType),
+                    user.Id))
+                .ConfigureAwait(false);
+        }
+
+        private static string GetItemName(BaseItemDto item)
+        {
+            var name = item.Name;
+
+            if (!string.IsNullOrEmpty(item.SeriesName))
+            {
+                name = item.SeriesName + " - " + name;
+            }
+
+            if (item.Artists != null && item.Artists.Count > 0)
+            {
+                name = item.Artists[0] + " - " + name;
+            }
+
+            return name;
+        }
+
+        private static string GetPlaybackStoppedNotificationType(string mediaType)
+        {
+            if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
+            {
+                return NotificationType.AudioPlaybackStopped.ToString();
+            }
+
+            if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
+            {
+                return NotificationType.VideoPlaybackStopped.ToString();
+            }
+
+            return null;
+        }
+    }
+}