Convert UserDataChangeNotifier to IHostedService
This commit is contained in:
parent
4e02d8aa21
commit
9e62b6919f
|
@ -1,5 +1,3 @@
|
||||||
#pragma warning disable CS1591
|
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
@ -8,14 +6,17 @@ using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MediaBrowser.Controller.Entities;
|
using MediaBrowser.Controller.Entities;
|
||||||
using MediaBrowser.Controller.Library;
|
using MediaBrowser.Controller.Library;
|
||||||
using MediaBrowser.Controller.Plugins;
|
|
||||||
using MediaBrowser.Controller.Session;
|
using MediaBrowser.Controller.Session;
|
||||||
using MediaBrowser.Model.Entities;
|
using MediaBrowser.Model.Entities;
|
||||||
using MediaBrowser.Model.Session;
|
using MediaBrowser.Model.Session;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace Emby.Server.Implementations.EntryPoints
|
namespace Emby.Server.Implementations.EntryPoints
|
||||||
{
|
{
|
||||||
public sealed class UserDataChangeNotifier : IServerEntryPoint
|
/// <summary>
|
||||||
|
/// <see cref="IHostedService"/> responsible for notifying users when associated item data is updated.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class UserDataChangeNotifier : IHostedService, IDisposable
|
||||||
{
|
{
|
||||||
private const int UpdateDuration = 500;
|
private const int UpdateDuration = 500;
|
||||||
|
|
||||||
|
@ -23,25 +24,43 @@ namespace Emby.Server.Implementations.EntryPoints
|
||||||
private readonly IUserDataManager _userDataManager;
|
private readonly IUserDataManager _userDataManager;
|
||||||
private readonly IUserManager _userManager;
|
private readonly IUserManager _userManager;
|
||||||
|
|
||||||
private readonly Dictionary<Guid, List<BaseItem>> _changedItems = new Dictionary<Guid, List<BaseItem>>();
|
private readonly Dictionary<Guid, List<BaseItem>> _changedItems = new();
|
||||||
|
private readonly object _syncLock = new();
|
||||||
|
|
||||||
private readonly object _syncLock = new object();
|
|
||||||
private Timer? _updateTimer;
|
private Timer? _updateTimer;
|
||||||
|
|
||||||
public UserDataChangeNotifier(IUserDataManager userDataManager, ISessionManager sessionManager, IUserManager userManager)
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="UserDataChangeNotifier"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userDataManager">The <see cref="IUserDataManager"/>.</param>
|
||||||
|
/// <param name="sessionManager">The <see cref="ISessionManager"/>.</param>
|
||||||
|
/// <param name="userManager">The <see cref="IUserManager"/>.</param>
|
||||||
|
public UserDataChangeNotifier(
|
||||||
|
IUserDataManager userDataManager,
|
||||||
|
ISessionManager sessionManager,
|
||||||
|
IUserManager userManager)
|
||||||
{
|
{
|
||||||
_userDataManager = userDataManager;
|
_userDataManager = userDataManager;
|
||||||
_sessionManager = sessionManager;
|
_sessionManager = sessionManager;
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task RunAsync()
|
/// <inheritdoc />
|
||||||
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_userDataManager.UserDataSaved += OnUserDataManagerUserDataSaved;
|
_userDataManager.UserDataSaved += OnUserDataManagerUserDataSaved;
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
_userDataManager.UserDataSaved -= OnUserDataManagerUserDataSaved;
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
private void OnUserDataManagerUserDataSaved(object? sender, UserDataSaveEventArgs e)
|
private void OnUserDataManagerUserDataSaved(object? sender, UserDataSaveEventArgs e)
|
||||||
{
|
{
|
||||||
if (e.SaveReason == UserDataSaveReason.PlaybackProgress)
|
if (e.SaveReason == UserDataSaveReason.PlaybackProgress)
|
||||||
|
@ -103,55 +122,40 @@ namespace Emby.Server.Implementations.EntryPoints
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await SendNotifications(changes, CancellationToken.None).ConfigureAwait(false);
|
foreach (var (userId, changedItems) in changes)
|
||||||
}
|
|
||||||
|
|
||||||
private async Task SendNotifications(List<KeyValuePair<Guid, List<BaseItem>>> changes, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
foreach ((var key, var value) in changes)
|
|
||||||
{
|
{
|
||||||
await SendNotifications(key, value, cancellationToken).ConfigureAwait(false);
|
await _sessionManager.SendMessageToUserSessions(
|
||||||
|
[userId],
|
||||||
|
SessionMessageType.UserDataChanged,
|
||||||
|
() => GetUserDataChangeInfo(userId, changedItems),
|
||||||
|
default).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task SendNotifications(Guid userId, List<BaseItem> changedItems, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return _sessionManager.SendMessageToUserSessions(new List<Guid> { userId }, SessionMessageType.UserDataChanged, () => GetUserDataChangeInfo(userId, changedItems), cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
private UserDataChangeInfo GetUserDataChangeInfo(Guid userId, List<BaseItem> changedItems)
|
private UserDataChangeInfo GetUserDataChangeInfo(Guid userId, List<BaseItem> changedItems)
|
||||||
{
|
{
|
||||||
var user = _userManager.GetUserById(userId);
|
var user = _userManager.GetUserById(userId);
|
||||||
|
|
||||||
var dtoList = changedItems
|
|
||||||
.DistinctBy(x => x.Id)
|
|
||||||
.Select(i =>
|
|
||||||
{
|
|
||||||
var dto = _userDataManager.GetUserDataDto(i, user);
|
|
||||||
dto.ItemId = i.Id.ToString("N", CultureInfo.InvariantCulture);
|
|
||||||
return dto;
|
|
||||||
})
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
var userIdString = userId.ToString("N", CultureInfo.InvariantCulture);
|
|
||||||
|
|
||||||
return new UserDataChangeInfo
|
return new UserDataChangeInfo
|
||||||
{
|
{
|
||||||
UserId = userIdString,
|
UserId = userId.ToString("N", CultureInfo.InvariantCulture),
|
||||||
|
UserDataList = changedItems
|
||||||
UserDataList = dtoList
|
.DistinctBy(x => x.Id)
|
||||||
|
.Select(i =>
|
||||||
|
{
|
||||||
|
var dto = _userDataManager.GetUserDataDto(i, user);
|
||||||
|
dto.ItemId = i.Id.ToString("N", CultureInfo.InvariantCulture);
|
||||||
|
return dto;
|
||||||
|
})
|
||||||
|
.ToArray()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (_updateTimer is not null)
|
_updateTimer?.Dispose();
|
||||||
{
|
_updateTimer = null;
|
||||||
_updateTimer.Dispose();
|
|
||||||
_updateTimer = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
_userDataManager.UserDataSaved -= OnUserDataManagerUserDataSaved;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,6 +128,7 @@ namespace Jellyfin.Server
|
||||||
services.AddHostedService<AutoDiscoveryHost>();
|
services.AddHostedService<AutoDiscoveryHost>();
|
||||||
services.AddHostedService<PortForwardingHost>();
|
services.AddHostedService<PortForwardingHost>();
|
||||||
services.AddHostedService<LibraryChangedNotifier>();
|
services.AddHostedService<LibraryChangedNotifier>();
|
||||||
|
services.AddHostedService<UserDataChangeNotifier>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user