< Summary - Jellyfin

Information
Class: Jellyfin.Server.Implementations.Events.Consumers.Session.PlaybackStopLogger
Assembly: Jellyfin.Server.Implementations
File(s): /srv/git/jellyfin/Jellyfin.Server.Implementations/Events/Consumers/Session/PlaybackStopLogger.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 15
Coverable lines: 15
Total lines: 116
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetItemName(...)0%4260%
GetPlaybackStoppedNotificationType(...)0%2040%

File(s)

/srv/git/jellyfin/Jellyfin.Server.Implementations/Events/Consumers/Session/PlaybackStopLogger.cs

#LineLine coverage
 1using System;
 2using System.Globalization;
 3using System.Threading.Tasks;
 4using Jellyfin.Data.Entities;
 5using Jellyfin.Data.Enums;
 6using MediaBrowser.Controller.Events;
 7using MediaBrowser.Controller.Library;
 8using MediaBrowser.Model.Activity;
 9using MediaBrowser.Model.Dto;
 10using MediaBrowser.Model.Entities;
 11using MediaBrowser.Model.Globalization;
 12using MediaBrowser.Model.Notifications;
 13using Microsoft.Extensions.Logging;
 14
 15namespace 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        {
 034            _logger = logger;
 035            _localizationManager = localizationManager;
 036            _activityManager = activityManager;
 037        }
 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        {
 086            var name = item.Name;
 87
 088            if (!string.IsNullOrEmpty(item.SeriesName))
 89            {
 090                name = item.SeriesName + " - " + name;
 91            }
 92
 093            if (item.Artists is not null && item.Artists.Count > 0)
 94            {
 095                name = item.Artists[0] + " - " + name;
 96            }
 97
 098            return name;
 99        }
 100
 101        private static string? GetPlaybackStoppedNotificationType(MediaType mediaType)
 102        {
 0103            if (mediaType == MediaType.Audio)
 104            {
 0105                return NotificationType.AudioPlaybackStopped.ToString();
 106            }
 107
 0108            if (mediaType == MediaType.Video)
 109            {
 0110                return NotificationType.VideoPlaybackStopped.ToString();
 111            }
 112
 0113            return null;
 114        }
 115    }
 116}