< Summary - Jellyfin

Information
Class: Jellyfin.LiveTv.Recordings.RecordingNotifier
Assembly: Jellyfin.LiveTv
File(s): /srv/git/jellyfin/src/Jellyfin.LiveTv/Recordings/RecordingNotifier.cs
Line coverage
100%
Covered lines: 15
Uncovered lines: 0
Coverable lines: 15
Total lines: 97
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
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%11100%
StartAsync(...)100%11100%
StopAsync(...)100%11100%

File(s)

/srv/git/jellyfin/src/Jellyfin.LiveTv/Recordings/RecordingNotifier.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using System.Threading;
 4using System.Threading.Tasks;
 5using Jellyfin.Data;
 6using Jellyfin.Data.Events;
 7using Jellyfin.Database.Implementations.Enums;
 8using MediaBrowser.Controller.Library;
 9using MediaBrowser.Controller.LiveTv;
 10using MediaBrowser.Controller.Session;
 11using MediaBrowser.Model.Session;
 12using Microsoft.Extensions.Hosting;
 13using Microsoft.Extensions.Logging;
 14
 15namespace Jellyfin.LiveTv.Recordings
 16{
 17    /// <summary>
 18    /// <see cref="IHostedService"/> responsible for notifying users when a LiveTV recording is completed.
 19    /// </summary>
 20    public sealed class RecordingNotifier : IHostedService
 21    {
 22        private readonly ILogger<RecordingNotifier> _logger;
 23        private readonly ISessionManager _sessionManager;
 24        private readonly IUserManager _userManager;
 25        private readonly ILiveTvManager _liveTvManager;
 26
 27        /// <summary>
 28        /// Initializes a new instance of the <see cref="RecordingNotifier"/> class.
 29        /// </summary>
 30        /// <param name="logger">The <see cref="ILogger"/>.</param>
 31        /// <param name="sessionManager">The <see cref="ISessionManager"/>.</param>
 32        /// <param name="userManager">The <see cref="IUserManager"/>.</param>
 33        /// <param name="liveTvManager">The <see cref="ILiveTvManager"/>.</param>
 34        public RecordingNotifier(
 35            ILogger<RecordingNotifier> logger,
 36            ISessionManager sessionManager,
 37            IUserManager userManager,
 38            ILiveTvManager liveTvManager)
 39        {
 2140            _logger = logger;
 2141            _sessionManager = sessionManager;
 2142            _userManager = userManager;
 2143            _liveTvManager = liveTvManager;
 2144        }
 45
 46        /// <inheritdoc />
 47        public Task StartAsync(CancellationToken cancellationToken)
 48        {
 2149            _liveTvManager.TimerCancelled += OnLiveTvManagerTimerCancelled;
 2150            _liveTvManager.SeriesTimerCancelled += OnLiveTvManagerSeriesTimerCancelled;
 2151            _liveTvManager.TimerCreated += OnLiveTvManagerTimerCreated;
 2152            _liveTvManager.SeriesTimerCreated += OnLiveTvManagerSeriesTimerCreated;
 53
 2154            return Task.CompletedTask;
 55        }
 56
 57        /// <inheritdoc />
 58        public Task StopAsync(CancellationToken cancellationToken)
 59        {
 2160            _liveTvManager.TimerCancelled -= OnLiveTvManagerTimerCancelled;
 2161            _liveTvManager.SeriesTimerCancelled -= OnLiveTvManagerSeriesTimerCancelled;
 2162            _liveTvManager.TimerCreated -= OnLiveTvManagerTimerCreated;
 2163            _liveTvManager.SeriesTimerCreated -= OnLiveTvManagerSeriesTimerCreated;
 64
 2165            return Task.CompletedTask;
 66        }
 67
 68        private async void OnLiveTvManagerSeriesTimerCreated(object? sender, GenericEventArgs<TimerEventInfo> e)
 69            => await SendMessage(SessionMessageType.SeriesTimerCreated, e.Argument).ConfigureAwait(false);
 70
 71        private async void OnLiveTvManagerTimerCreated(object? sender, GenericEventArgs<TimerEventInfo> e)
 72            => await SendMessage(SessionMessageType.TimerCreated, e.Argument).ConfigureAwait(false);
 73
 74        private async void OnLiveTvManagerSeriesTimerCancelled(object? sender, GenericEventArgs<TimerEventInfo> e)
 75            => await SendMessage(SessionMessageType.SeriesTimerCancelled, e.Argument).ConfigureAwait(false);
 76
 77        private async void OnLiveTvManagerTimerCancelled(object? sender, GenericEventArgs<TimerEventInfo> e)
 78            => await SendMessage(SessionMessageType.TimerCancelled, e.Argument).ConfigureAwait(false);
 79
 80        private async Task SendMessage(SessionMessageType name, TimerEventInfo info)
 81        {
 82            var users = _userManager.Users
 83                .Where(i => i.HasPermission(PermissionKind.EnableLiveTvAccess))
 84                .Select(i => i.Id)
 85                .ToList();
 86
 87            try
 88            {
 89                await _sessionManager.SendMessageToUserSessions(users, name, info, CancellationToken.None).ConfigureAwai
 90            }
 91            catch (Exception ex)
 92            {
 93                _logger.LogError(ex, "Error sending message");
 94            }
 95        }
 96    }
 97}