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 { /// /// Creates an entry in the activity log whenever a user starts playback. /// public class PlaybackStartLogger : IEventConsumer { private readonly ILogger _logger; private readonly ILocalizationManager _localizationManager; private readonly IActivityManager _activityManager; /// /// Initializes a new instance of the class. /// /// The logger. /// The localization manager. /// The activity manager. public PlaybackStartLogger(ILogger logger, ILocalizationManager localizationManager, IActivityManager activityManager) { _logger = logger; _localizationManager = localizationManager; _activityManager = activityManager; } /// public async Task OnEvent(PlaybackStartEventArgs eventArgs) { if (eventArgs.MediaInfo is null) { _logger.LogWarning("PlaybackStart reported with null media info."); return; } if (eventArgs.Item is not 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("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 is not 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 "Playback"; } } }