2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2013-10-03 18:02:23 +00:00
|
|
|
using System.Collections.Generic;
|
2019-02-28 22:22:57 +00:00
|
|
|
using System.Globalization;
|
2013-10-03 18:02:23 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 19:20:41 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Session;
|
2024-02-06 20:54:03 +00:00
|
|
|
using Microsoft.Extensions.Hosting;
|
2013-10-03 18:02:23 +00:00
|
|
|
|
2016-11-03 22:06:00 +00:00
|
|
|
namespace Emby.Server.Implementations.EntryPoints
|
2013-10-03 18:02:23 +00:00
|
|
|
{
|
2024-02-06 20:54:03 +00:00
|
|
|
/// <summary>
|
|
|
|
/// <see cref="IHostedService"/> responsible for notifying users when associated item data is updated.
|
|
|
|
/// </summary>
|
|
|
|
public sealed class UserDataChangeNotifier : IHostedService, IDisposable
|
2013-10-03 18:02:23 +00:00
|
|
|
{
|
2020-02-06 14:20:23 +00:00
|
|
|
private const int UpdateDuration = 500;
|
|
|
|
|
2013-10-03 18:02:23 +00:00
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
private readonly IUserDataManager _userDataManager;
|
2014-07-04 02:22:57 +00:00
|
|
|
private readonly IUserManager _userManager;
|
2013-10-03 18:02:23 +00:00
|
|
|
|
2024-02-06 20:54:03 +00:00
|
|
|
private readonly Dictionary<Guid, List<BaseItem>> _changedItems = new();
|
|
|
|
private readonly object _syncLock = new();
|
2020-02-06 14:20:23 +00:00
|
|
|
|
2021-05-28 12:33:54 +00:00
|
|
|
private Timer? _updateTimer;
|
2013-10-03 18:02:23 +00:00
|
|
|
|
2024-02-06 20:54:03 +00:00
|
|
|
/// <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)
|
2013-10-03 18:02:23 +00:00
|
|
|
{
|
|
|
|
_userDataManager = userDataManager;
|
|
|
|
_sessionManager = sessionManager;
|
2014-07-04 02:22:57 +00:00
|
|
|
_userManager = userManager;
|
2013-10-03 18:02:23 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 20:54:03 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
2013-10-03 18:02:23 +00:00
|
|
|
{
|
2020-02-06 14:20:23 +00:00
|
|
|
_userDataManager.UserDataSaved += OnUserDataManagerUserDataSaved;
|
2019-01-27 14:40:37 +00:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2013-10-03 18:02:23 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 20:54:03 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
_userDataManager.UserDataSaved -= OnUserDataManagerUserDataSaved;
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2021-05-28 12:33:54 +00:00
|
|
|
private void OnUserDataManagerUserDataSaved(object? sender, UserDataSaveEventArgs e)
|
2013-10-03 18:02:23 +00:00
|
|
|
{
|
|
|
|
if (e.SaveReason == UserDataSaveReason.PlaybackProgress)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_syncLock)
|
|
|
|
{
|
2022-12-05 14:00:20 +00:00
|
|
|
if (_updateTimer is null)
|
2013-10-03 18:02:23 +00:00
|
|
|
{
|
2020-02-06 14:20:23 +00:00
|
|
|
_updateTimer = new Timer(
|
|
|
|
UpdateTimerCallback,
|
|
|
|
null,
|
|
|
|
UpdateDuration,
|
|
|
|
Timeout.Infinite);
|
2013-10-03 18:02:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-06 14:20:23 +00:00
|
|
|
_updateTimer.Change(UpdateDuration, Timeout.Infinite);
|
2013-10-03 18:02:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 12:33:54 +00:00
|
|
|
if (!_changedItems.TryGetValue(e.UserId, out List<BaseItem>? keys))
|
2013-10-03 18:02:23 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
keys = new List<BaseItem>();
|
2014-07-04 02:22:57 +00:00
|
|
|
_changedItems[e.UserId] = keys;
|
2013-10-03 18:02:23 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 02:22:57 +00:00
|
|
|
keys.Add(e.Item);
|
2015-04-17 03:31:19 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var baseItem = e.Item;
|
2014-07-04 02:22:57 +00:00
|
|
|
|
|
|
|
// Go up one level for indicators
|
2022-12-05 14:01:13 +00:00
|
|
|
if (baseItem is not null)
|
2014-07-04 02:22:57 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var parent = baseItem.GetOwner() ?? baseItem.GetParent();
|
2014-07-04 02:22:57 +00:00
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
if (parent is not null)
|
2014-07-04 02:22:57 +00:00
|
|
|
{
|
|
|
|
keys.Add(parent);
|
|
|
|
}
|
|
|
|
}
|
2013-10-03 18:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:57:23 +00:00
|
|
|
private async void UpdateTimerCallback(object? state)
|
2013-10-03 18:02:23 +00:00
|
|
|
{
|
2023-03-01 17:57:23 +00:00
|
|
|
List<KeyValuePair<Guid, List<BaseItem>>> changes;
|
2013-10-03 18:02:23 +00:00
|
|
|
lock (_syncLock)
|
|
|
|
{
|
|
|
|
// Remove dupes in case some were saved multiple times
|
2023-03-01 17:57:23 +00:00
|
|
|
changes = _changedItems.ToList();
|
2014-07-04 02:22:57 +00:00
|
|
|
_changedItems.Clear();
|
2013-10-03 18:02:23 +00:00
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
if (_updateTimer is not null)
|
2013-10-03 18:02:23 +00:00
|
|
|
{
|
2020-02-06 14:20:23 +00:00
|
|
|
_updateTimer.Dispose();
|
|
|
|
_updateTimer = null;
|
2013-10-03 18:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-01 17:57:23 +00:00
|
|
|
|
2024-02-06 20:54:03 +00:00
|
|
|
foreach (var (userId, changedItems) in changes)
|
2013-10-03 18:02:23 +00:00
|
|
|
{
|
2024-02-06 20:54:03 +00:00
|
|
|
await _sessionManager.SendMessageToUserSessions(
|
|
|
|
[userId],
|
|
|
|
SessionMessageType.UserDataChanged,
|
|
|
|
() => GetUserDataChangeInfo(userId, changedItems),
|
|
|
|
default).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private UserDataChangeInfo GetUserDataChangeInfo(Guid userId, List<BaseItem> changedItems)
|
|
|
|
{
|
|
|
|
var user = _userManager.GetUserById(userId);
|
2013-10-03 18:02:23 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return new UserDataChangeInfo
|
|
|
|
{
|
2024-02-06 20:54:03 +00:00
|
|
|
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 17:26:21 +00:00
|
|
|
};
|
2013-10-03 18:02:23 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 20:54:03 +00:00
|
|
|
/// <inheritdoc />
|
2013-10-03 18:02:23 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
2024-02-06 20:54:03 +00:00
|
|
|
_updateTimer?.Dispose();
|
|
|
|
_updateTimer = null;
|
2013-10-03 18:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|