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