jellyfin/Emby.Server.Implementations/EntryPoints/UserDataChangeNotifier.cs

162 lines
5.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Session;
using Microsoft.Extensions.Hosting;
namespace Emby.Server.Implementations.EntryPoints
{
/// <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 readonly ISessionManager _sessionManager;
private readonly IUserDataManager _userDataManager;
private readonly IUserManager _userManager;
private readonly Dictionary<Guid, List<BaseItem>> _changedItems = new();
private readonly object _syncLock = new();
2021-05-28 05:33:54 -07:00
private Timer? _updateTimer;
/// <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;
_sessionManager = sessionManager;
_userManager = userManager;
}
/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken)
{
_userDataManager.UserDataSaved += OnUserDataManagerUserDataSaved;
2019-01-27 07:40:37 -07:00
return Task.CompletedTask;
}
/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken)
{
_userDataManager.UserDataSaved -= OnUserDataManagerUserDataSaved;
return Task.CompletedTask;
}
2021-05-28 05:33:54 -07:00
private void OnUserDataManagerUserDataSaved(object? sender, UserDataSaveEventArgs e)
{
if (e.SaveReason == UserDataSaveReason.PlaybackProgress)
{
return;
}
lock (_syncLock)
{
2022-12-05 07:00:20 -07:00
if (_updateTimer is null)
{
_updateTimer = new Timer(
UpdateTimerCallback,
null,
UpdateDuration,
Timeout.Infinite);
}
else
{
_updateTimer.Change(UpdateDuration, Timeout.Infinite);
}
2021-05-28 05:33:54 -07:00
if (!_changedItems.TryGetValue(e.UserId, out List<BaseItem>? keys))
{
2018-09-12 10:26:21 -07:00
keys = new List<BaseItem>();
_changedItems[e.UserId] = keys;
}
keys.Add(e.Item);
2015-04-16 20:31:19 -07:00
2018-09-12 10:26:21 -07:00
var baseItem = e.Item;
// Go up one level for indicators
2022-12-05 07:01:13 -07:00
if (baseItem is not null)
{
2018-09-12 10:26:21 -07:00
var parent = baseItem.GetOwner() ?? baseItem.GetParent();
2022-12-05 07:01:13 -07:00
if (parent is not null)
{
keys.Add(parent);
}
}
}
}
private async void UpdateTimerCallback(object? state)
{
List<KeyValuePair<Guid, List<BaseItem>>> changes;
lock (_syncLock)
{
// Remove dupes in case some were saved multiple times
changes = _changedItems.ToList();
_changedItems.Clear();
2022-12-05 07:01:13 -07:00
if (_updateTimer is not null)
{
_updateTimer.Dispose();
_updateTimer = null;
}
}
foreach (var (userId, changedItems) in changes)
{
await _sessionManager.SendMessageToUserSessions(
[userId],
SessionMessageType.UserDataChanged,
() => GetUserDataChangeInfo(userId, changedItems),
default).ConfigureAwait(false);
2018-09-12 10:26:21 -07:00
}
}
private UserDataChangeInfo GetUserDataChangeInfo(Guid userId, List<BaseItem> changedItems)
{
var user = _userManager.GetUserById(userId);
2018-09-12 10:26:21 -07:00
return new UserDataChangeInfo
{
UserId = userId.ToString("N", CultureInfo.InvariantCulture),
UserDataList = changedItems
.DistinctBy(x => x.Id)
.Select(i =>
{
var dto = _userDataManager.GetUserDataDto(i, user);
dto.ItemId = i.Id.ToString("N", CultureInfo.InvariantCulture);
return dto;
})
.ToArray()
2018-09-12 10:26:21 -07:00
};
}
/// <inheritdoc />
public void Dispose()
{
_updateTimer?.Dispose();
_updateTimer = null;
}
}
}