Migrate ActivityLogEntryPoint.OnSessionEnded to IEventConsumer

This commit is contained in:
Patrick Barron 2020-08-15 02:07:28 -04:00
parent ca3a8bdb98
commit 8570cfdba6
3 changed files with 73 additions and 78 deletions

View File

@ -1,78 +0,0 @@
using System.Globalization;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Globalization;
namespace Emby.Server.Implementations.Activity
{
/// <summary>
/// Entry point for the activity logger.
/// </summary>
public sealed class ActivityLogEntryPoint : IServerEntryPoint
{
private readonly ISessionManager _sessionManager;
private readonly IActivityManager _activityManager;
private readonly ILocalizationManager _localization;
/// <summary>
/// Initializes a new instance of the <see cref="ActivityLogEntryPoint"/> class.
/// </summary>
/// <param name="sessionManager">The session manager.</param>
/// <param name="activityManager">The activity manager.</param>
/// <param name="localization">The localization manager.</param>
public ActivityLogEntryPoint(
ISessionManager sessionManager,
IActivityManager activityManager,
ILocalizationManager localization)
{
_sessionManager = sessionManager;
_activityManager = activityManager;
_localization = localization;
}
/// <inheritdoc />
public Task RunAsync()
{
_sessionManager.SessionEnded += OnSessionEnded;
return Task.CompletedTask;
}
private async void OnSessionEnded(object sender, SessionEventArgs e)
{
var session = e.SessionInfo;
if (string.IsNullOrEmpty(session.UserName))
{
return;
}
await CreateLogEntry(new ActivityLog(
string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("UserOfflineFromDevice"),
session.UserName,
session.DeviceName),
"SessionEnded",
session.UserId)
{
ShortOverview = string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("LabelIpAddressValue"),
session.RemoteEndPoint),
}).ConfigureAwait(false);
}
private async Task CreateLogEntry(ActivityLog entry)
=> await _activityManager.CreateAsync(entry).ConfigureAwait(false);
/// <inheritdoc />
public void Dispose()
{
_sessionManager.SessionEnded -= OnSessionEnded;
}
}
}

View File

@ -0,0 +1,54 @@
using System.Globalization;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using MediaBrowser.Controller.Events;
using MediaBrowser.Controller.Events.Session;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Globalization;
namespace Jellyfin.Server.Implementations.Events.Consumers.Session
{
/// <summary>
/// Creates an entry in the activity log whenever a session ends.
/// </summary>
public class SessionManagerEndedLogger : IEventConsumer<SessionEndedEventArgs>
{
private readonly ILocalizationManager _localizationManager;
private readonly IActivityManager _activityManager;
/// <summary>
/// Initializes a new instance of the <see cref="SessionManagerEndedLogger"/> class.
/// </summary>
/// <param name="localizationManager">The localization manager.</param>
/// <param name="activityManager">The activity manager.</param>
public SessionManagerEndedLogger(ILocalizationManager localizationManager, IActivityManager activityManager)
{
_localizationManager = localizationManager;
_activityManager = activityManager;
}
/// <inheritdoc />
public async Task OnEvent(SessionEndedEventArgs eventArgs)
{
if (string.IsNullOrEmpty(eventArgs.Argument.UserName))
{
return;
}
await _activityManager.CreateAsync(new ActivityLog(
string.Format(
CultureInfo.InvariantCulture,
_localizationManager.GetLocalizedString("UserOfflineFromDevice"),
eventArgs.Argument.UserName,
eventArgs.Argument.DeviceName),
"SessionEnded",
eventArgs.Argument.UserId)
{
ShortOverview = string.Format(
CultureInfo.InvariantCulture,
_localizationManager.GetLocalizedString("LabelIpAddressValue"),
eventArgs.Argument.RemoteEndPoint),
}).ConfigureAwait(false);
}
}
}

View File

@ -0,0 +1,19 @@
using Jellyfin.Data.Events;
using MediaBrowser.Controller.Session;
namespace MediaBrowser.Controller.Events.Session
{
/// <summary>
/// An event that fires when a session is ended.
/// </summary>
public class SessionEndedEventArgs : GenericEventArgs<SessionInfo>
{
/// <summary>
/// Initializes a new instance of the <see cref="SessionEndedEventArgs"/> class.
/// </summary>
/// <param name="arg">The session info.</param>
public SessionEndedEventArgs(SessionInfo arg) : base(arg)
{
}
}
}