The Free Software Media System - Server Backend & API
Migrate ActivityLogEntryPoint.OnPlaybackStart to IEventConsumer
Patrick Barron3e5fe04427d6
diff --git a/Emby.Server.Implementations/Activity/ActivityLogEntryPoint.cs b/Emby.Server.Implementations/Activity/ActivityLogEntryPoint.csindex 84bec92014..53b3a6293b 100644--- a/Emby.Server.Implementations/Activity/ActivityLogEntryPoint.cs+++ b/Emby.Server.Implementations/Activity/ActivityLogEntryPoint.cs@@ -1,7 +1,6 @@using System;using System.Collections.Generic;using System.Globalization;-using System.Linq;using System.Text;using System.Threading.Tasks;using Jellyfin.Data.Entities;@@ -83,7 +82,6 @@ namespace Emby.Server.Implementations.Activity_sessionManager.AuthenticationFailed += OnAuthenticationFailed;_sessionManager.AuthenticationSucceeded += OnAuthenticationSucceeded;_sessionManager.SessionEnded += OnSessionEnded;- _sessionManager.PlaybackStart += OnPlaybackStart;_sessionManager.PlaybackStopped += OnPlaybackStopped;_subManager.SubtitleDownloadFailure += OnSubtitleDownloadFailure;@@ -161,41 +159,6 @@ namespace Emby.Server.Implementations.Activity.ConfigureAwait(false);}- private async void OnPlaybackStart(object sender, PlaybackProgressEventArgs e)- {- var item = e.MediaInfo;-- if (item == null)- {- _logger.LogWarning("PlaybackStart 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.First();-- await CreateLogEntry(new ActivityLog(- string.Format(- CultureInfo.InvariantCulture,- _localization.GetLocalizedString("UserStartedPlayingItemWithValues"),- user.Username,- GetItemName(item),- e.DeviceName),- GetPlaybackNotificationType(item.MediaType),- user.Id))- .ConfigureAwait(false);- }-private static string GetItemName(BaseItemDto item){var name = item.Name;@@ -213,21 +176,6 @@ namespace Emby.Server.Implementations.Activityreturn name;}- private static string GetPlaybackNotificationType(string mediaType)- {- if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))- {- return NotificationType.AudioPlayback.ToString();- }-- if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))- {- return NotificationType.VideoPlayback.ToString();- }-- return null;- }-private static string GetPlaybackStoppedNotificationType(string mediaType){if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))@@ -494,7 +442,6 @@ namespace Emby.Server.Implementations.Activity_sessionManager.AuthenticationSucceeded -= OnAuthenticationSucceeded;_sessionManager.SessionEnded -= OnSessionEnded;- _sessionManager.PlaybackStart -= OnPlaybackStart;_sessionManager.PlaybackStopped -= OnPlaybackStopped;_subManager.SubtitleDownloadFailure -= OnSubtitleDownloadFailure;diff --git a/Jellyfin.Server.Implementations/Events/Consumers/Session/PlaybackStartLogger.cs b/Jellyfin.Server.Implementations/Events/Consumers/Session/PlaybackStartLogger.csnew file mode 100644index 0000000000..c48683ea38--- /dev/null+++ b/Jellyfin.Server.Implementations/Events/Consumers/Session/PlaybackStartLogger.cs@@ -0,0 +1,105 @@+using System;+using System.Globalization;+using System.Linq;+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 entry in the activity log whenever a user starts playback.+ /// </summary>+ public class PlaybackStartLogger : IEventConsumer<PlaybackStartEventArgs>+ {+ private readonly ILogger<PlaybackStartLogger> _logger;+ private readonly ILocalizationManager _localizationManager;+ private readonly IActivityManager _activityManager;++ /// <summary>+ /// Initializes a new instance of the <see cref="PlaybackStartLogger"/> class.+ /// </summary>+ /// <param name="logger">The logger.</param>+ /// <param name="localizationManager">The localization manager.</param>+ /// <param name="activityManager">The activity manager.</param>+ public PlaybackStartLogger(ILogger<PlaybackStartLogger> logger, ILocalizationManager localizationManager, IActivityManager activityManager)+ {+ _logger = logger;+ _localizationManager = localizationManager;+ _activityManager = activityManager;+ }++ /// <inheritdoc />+ public async Task OnEvent(PlaybackStartEventArgs eventArgs)+ {+ if (eventArgs.MediaInfo == null)+ {+ _logger.LogWarning("PlaybackStart 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.First();++ await _activityManager.CreateAsync(new ActivityLog(+ string.Format(+ CultureInfo.InvariantCulture,+ _localizationManager.GetLocalizedString("UserStartedPlayingItemWithValues"),+ user.Username,+ GetItemName(eventArgs.MediaInfo),+ eventArgs.DeviceName),+ GetPlaybackNotificationType(eventArgs.MediaInfo.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 GetPlaybackNotificationType(string mediaType)+ {+ if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))+ {+ return NotificationType.AudioPlayback.ToString();+ }++ if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))+ {+ return NotificationType.VideoPlayback.ToString();+ }++ return null;+ }+ }+}diff --git a/MediaBrowser.Controller/Library/PlaybackStartEventArgs.cs b/MediaBrowser.Controller/Library/PlaybackStartEventArgs.csnew file mode 100644index 0000000000..3aa9c28954--- /dev/null+++ b/MediaBrowser.Controller/Library/PlaybackStartEventArgs.cs@@ -0,0 +1,6 @@+namespace MediaBrowser.Controller.Library+{+ public class PlaybackStartEventArgs : PlaybackProgressEventArgs+ {+ }+}