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