aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Events/Consumers/Session/PlaybackStopLogger.cs
blob: 7d452ea2fd3ff28e9eabc33b8f13d9cb47074025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Globalization;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
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
{
    /// <summary>
    /// Creates an activity log entry whenever a user stops playback.
    /// </summary>
    public class PlaybackStopLogger : IEventConsumer<PlaybackStopEventArgs>
    {
        private readonly ILogger<PlaybackStopLogger> _logger;
        private readonly ILocalizationManager _localizationManager;
        private readonly IActivityManager _activityManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="PlaybackStopLogger"/> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="localizationManager">The localization manager.</param>
        /// <param name="activityManager">The activity manager.</param>
        public PlaybackStopLogger(ILogger<PlaybackStopLogger> logger, ILocalizationManager localizationManager, IActivityManager activityManager)
        {
            _logger = logger;
            _localizationManager = localizationManager;
            _activityManager = activityManager;
        }

        /// <inheritdoc />
        public async Task OnEvent(PlaybackStopEventArgs eventArgs)
        {
            var item = eventArgs.MediaInfo;

            if (item is null)
            {
                _logger.LogWarning("PlaybackStopped 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];

            var notificationType = GetPlaybackStoppedNotificationType(item.MediaType);
            if (notificationType is null)
            {
                return;
            }

            await _activityManager.CreateAsync(new ActivityLog(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        _localizationManager.GetLocalizedString("UserStoppedPlayingItemWithValues"),
                        user.Username,
                        GetItemName(item),
                        eventArgs.DeviceName),
                    notificationType,
                    user.Id)
                {
                    ItemId = eventArgs.Item?.Id.ToString("N", CultureInfo.InvariantCulture),
                })
                .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? GetPlaybackStoppedNotificationType(MediaType mediaType)
        {
            if (mediaType == MediaType.Audio)
            {
                return NotificationType.AudioPlaybackStopped.ToString();
            }

            if (mediaType == MediaType.Video)
            {
                return NotificationType.VideoPlaybackStopped.ToString();
            }

            return null;
        }
    }
}