Migrate ActivityLogEntryPoint.OnSessionStarted to IEventConsumer

This commit is contained in:
Patrick Barron 2020-08-15 02:02:58 -04:00
parent 98cbf1c2de
commit ca3a8bdb98
3 changed files with 73 additions and 27 deletions

View File

@ -36,7 +36,6 @@ namespace Emby.Server.Implementations.Activity
/// <inheritdoc />
public Task RunAsync()
{
_sessionManager.SessionStarted += OnSessionStarted;
_sessionManager.SessionEnded += OnSessionEnded;
return Task.CompletedTask;
@ -67,38 +66,12 @@ namespace Emby.Server.Implementations.Activity
}).ConfigureAwait(false);
}
private async void OnSessionStarted(object sender, SessionEventArgs e)
{
var session = e.SessionInfo;
if (string.IsNullOrEmpty(session.UserName))
{
return;
}
await CreateLogEntry(new ActivityLog(
string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("UserOnlineFromDevice"),
session.UserName,
session.DeviceName),
"SessionStarted",
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.SessionStarted -= OnSessionStarted;
_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 when a session is started.
/// </summary>
public class SessionStartedLogger : IEventConsumer<SessionStartedEventArgs>
{
private readonly ILocalizationManager _localizationManager;
private readonly IActivityManager _activityManager;
/// <summary>
/// Initializes a new instance of the <see cref="SessionStartedLogger"/> class.
/// </summary>
/// <param name="localizationManager">The localization manager.</param>
/// <param name="activityManager">The activity manager.</param>
public SessionStartedLogger(ILocalizationManager localizationManager, IActivityManager activityManager)
{
_localizationManager = localizationManager;
_activityManager = activityManager;
}
/// <inheritdoc />
public async Task OnEvent(SessionStartedEventArgs eventArgs)
{
if (string.IsNullOrEmpty(eventArgs.Argument.UserName))
{
return;
}
await _activityManager.CreateAsync(new ActivityLog(
string.Format(
CultureInfo.InvariantCulture,
_localizationManager.GetLocalizedString("UserOnlineFromDevice"),
eventArgs.Argument.UserName,
eventArgs.Argument.DeviceName),
"SessionStarted",
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 started.
/// </summary>
public class SessionStartedEventArgs : GenericEventArgs<SessionInfo>
{
/// <summary>
/// Initializes a new instance of the <see cref="SessionStartedEventArgs"/> class.
/// </summary>
/// <param name="arg">The session info.</param>
public SessionStartedEventArgs(SessionInfo arg) : base(arg)
{
}
}
}