2020-08-14 02:33:16 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Jellyfin.Data.Entities;
|
2023-05-13 20:44:31 +02:00
|
|
|
|
using Jellyfin.Data.Enums;
|
2020-08-14 02:33:16 +02:00
|
|
|
|
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;
|
|
|
|
|
|
2022-12-05 15:00:20 +01:00
|
|
|
|
if (item is null)
|
2020-08-14 02:33:16 +02:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("PlaybackStopped reported with null media info.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 15:01:13 +01:00
|
|
|
|
if (eventArgs.Item is not null && eventArgs.Item.IsThemeMedia)
|
2020-08-14 02:33:16 +02:00
|
|
|
|
{
|
|
|
|
|
// Don't report theme song or local trailer playback
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (eventArgs.Users.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var user = eventArgs.Users[0];
|
|
|
|
|
|
2020-11-23 16:18:07 +01:00
|
|
|
|
var notificationType = GetPlaybackStoppedNotificationType(item.MediaType);
|
2022-12-05 15:00:20 +01:00
|
|
|
|
if (notificationType is null)
|
2020-11-23 16:18:07 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 02:33:16 +02:00
|
|
|
|
await _activityManager.CreateAsync(new ActivityLog(
|
|
|
|
|
string.Format(
|
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
|
_localizationManager.GetLocalizedString("UserStoppedPlayingItemWithValues"),
|
|
|
|
|
user.Username,
|
|
|
|
|
GetItemName(item),
|
|
|
|
|
eventArgs.DeviceName),
|
2020-11-23 16:18:07 +01:00
|
|
|
|
notificationType,
|
2023-06-08 10:36:45 +02:00
|
|
|
|
user.Id)
|
|
|
|
|
{
|
2023-06-10 18:06:22 +02:00
|
|
|
|
ItemId = eventArgs.Item?.Id.ToString("N", CultureInfo.InvariantCulture),
|
2023-06-08 10:36:45 +02:00
|
|
|
|
})
|
2020-08-14 02:33:16 +02:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetItemName(BaseItemDto item)
|
|
|
|
|
{
|
|
|
|
|
var name = item.Name;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(item.SeriesName))
|
|
|
|
|
{
|
|
|
|
|
name = item.SeriesName + " - " + name;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 15:01:13 +01:00
|
|
|
|
if (item.Artists is not null && item.Artists.Count > 0)
|
2020-08-14 02:33:16 +02:00
|
|
|
|
{
|
|
|
|
|
name = item.Artists[0] + " - " + name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 20:44:31 +02:00
|
|
|
|
private static string? GetPlaybackStoppedNotificationType(MediaType mediaType)
|
2020-08-14 02:33:16 +02:00
|
|
|
|
{
|
2023-05-13 20:44:31 +02:00
|
|
|
|
if (mediaType == MediaType.Audio)
|
2020-08-14 02:33:16 +02:00
|
|
|
|
{
|
|
|
|
|
return NotificationType.AudioPlaybackStopped.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 20:44:31 +02:00
|
|
|
|
if (mediaType == MediaType.Video)
|
2020-08-14 02:33:16 +02:00
|
|
|
|
{
|
|
|
|
|
return NotificationType.VideoPlaybackStopped.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|