| | 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 entry in the activity log whenever a user starts playback. |
| | 19 | | /// </summary> |
| | 20 | | public class PlaybackStartLogger : IEventConsumer<PlaybackStartEventArgs> |
| | 21 | | { |
| | 22 | | private readonly ILogger<PlaybackStartLogger> _logger; |
| | 23 | | private readonly ILocalizationManager _localizationManager; |
| | 24 | | private readonly IActivityManager _activityManager; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the <see cref="PlaybackStartLogger"/> 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 PlaybackStartLogger(ILogger<PlaybackStartLogger> logger, ILocalizationManager localizationManager, IActiv |
| | 33 | | { |
| 0 | 34 | | _logger = logger; |
| 0 | 35 | | _localizationManager = localizationManager; |
| 0 | 36 | | _activityManager = activityManager; |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <inheritdoc /> |
| | 40 | | public async Task OnEvent(PlaybackStartEventArgs eventArgs) |
| | 41 | | { |
| | 42 | | if (eventArgs.MediaInfo is null) |
| | 43 | | { |
| | 44 | | _logger.LogWarning("PlaybackStart reported with null media info."); |
| | 45 | | return; |
| | 46 | | } |
| | 47 | |
|
| | 48 | | if (eventArgs.Item is not null && eventArgs.Item.IsThemeMedia) |
| | 49 | | { |
| | 50 | | // Don't report theme song or local trailer playback |
| | 51 | | return; |
| | 52 | | } |
| | 53 | |
|
| | 54 | | if (eventArgs.Users.Count == 0) |
| | 55 | | { |
| | 56 | | return; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | var user = eventArgs.Users[0]; |
| | 60 | |
|
| | 61 | | await _activityManager.CreateAsync(new ActivityLog( |
| | 62 | | string.Format( |
| | 63 | | CultureInfo.InvariantCulture, |
| | 64 | | _localizationManager.GetLocalizedString("UserStartedPlayingItemWithValues"), |
| | 65 | | user.Username, |
| | 66 | | GetItemName(eventArgs.MediaInfo), |
| | 67 | | eventArgs.DeviceName), |
| | 68 | | GetPlaybackNotificationType(eventArgs.MediaInfo.MediaType), |
| | 69 | | user.Id) |
| | 70 | | { |
| | 71 | | ItemId = eventArgs.Item?.Id.ToString("N", CultureInfo.InvariantCulture), |
| | 72 | | }) |
| | 73 | | .ConfigureAwait(false); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | private static string GetItemName(BaseItemDto item) |
| | 77 | | { |
| 0 | 78 | | var name = item.Name; |
| | 79 | |
|
| 0 | 80 | | if (!string.IsNullOrEmpty(item.SeriesName)) |
| | 81 | | { |
| 0 | 82 | | name = item.SeriesName + " - " + name; |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | if (item.Artists is not null && item.Artists.Count > 0) |
| | 86 | | { |
| 0 | 87 | | name = item.Artists[0] + " - " + name; |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | return name; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | private static string GetPlaybackNotificationType(MediaType mediaType) |
| | 94 | | { |
| 0 | 95 | | if (mediaType == MediaType.Audio) |
| | 96 | | { |
| 0 | 97 | | return NotificationType.AudioPlayback.ToString(); |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | if (mediaType == MediaType.Video) |
| | 101 | | { |
| 0 | 102 | | return NotificationType.VideoPlayback.ToString(); |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | return "Playback"; |
| | 106 | | } |
| | 107 | | } |
| | 108 | | } |