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

155 lines
4.9 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.Plugins;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Session;
using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.EntryPoints
{
public class UserDataChangeNotifier : IServerEntryPoint
{
private readonly ISessionManager _sessionManager;
private readonly ILogger _logger;
private readonly IUserDataManager _userDataManager;
private readonly IUserManager _userManager;
private readonly object _syncLock = new object();
2019-02-05 08:49:46 +00:00
private Timer UpdateTimer { get; set; }
2013-10-10 16:55:07 +00:00
private const int UpdateDuration = 500;
2018-09-12 17:26:21 +00:00
private readonly Dictionary<Guid, List<BaseItem>> _changedItems = new Dictionary<Guid, List<BaseItem>>();
2019-02-05 08:49:46 +00:00
public UserDataChangeNotifier(IUserDataManager userDataManager, ISessionManager sessionManager, ILogger logger, IUserManager userManager)
{
_userDataManager = userDataManager;
_sessionManager = sessionManager;
_logger = logger;
_userManager = userManager;
}
2019-01-27 14:40:37 +00:00
public Task RunAsync()
{
_userDataManager.UserDataSaved += _userDataManager_UserDataSaved;
2019-01-27 14:40:37 +00:00
return Task.CompletedTask;
}
void _userDataManager_UserDataSaved(object sender, UserDataSaveEventArgs e)
{
if (e.SaveReason == UserDataSaveReason.PlaybackProgress)
{
return;
}
lock (_syncLock)
{
if (UpdateTimer == null)
{
2019-02-05 08:49:46 +00:00
UpdateTimer = new Timer(UpdateTimerCallback, null, UpdateDuration,
Timeout.Infinite);
}
else
{
UpdateTimer.Change(UpdateDuration, Timeout.Infinite);
}
if (!_changedItems.TryGetValue(e.UserId, out List<BaseItem> keys))
{
2018-09-12 17:26:21 +00:00
keys = new List<BaseItem>();
_changedItems[e.UserId] = keys;
}
keys.Add(e.Item);
2015-04-17 03:31:19 +00:00
2018-09-12 17:26:21 +00:00
var baseItem = e.Item;
// Go up one level for indicators
if (baseItem != null)
{
2018-09-12 17:26:21 +00:00
var parent = baseItem.GetOwner() ?? baseItem.GetParent();
if (parent != null)
{
keys.Add(parent);
}
}
}
}
private void UpdateTimerCallback(object state)
{
lock (_syncLock)
{
// Remove dupes in case some were saved multiple times
var changes = _changedItems.ToList();
_changedItems.Clear();
var task = SendNotifications(changes, CancellationToken.None);
if (UpdateTimer != null)
{
UpdateTimer.Dispose();
UpdateTimer = null;
}
}
}
2018-09-12 17:26:21 +00:00
private async Task SendNotifications(List<KeyValuePair<Guid, List<BaseItem>>> changes, CancellationToken cancellationToken)
{
foreach (var pair in changes)
{
2018-09-12 17:26:21 +00:00
await SendNotifications(pair.Key, pair.Value, cancellationToken).ConfigureAwait(false);
}
}
private Task SendNotifications(Guid userId, List<BaseItem> changedItems, CancellationToken cancellationToken)
{
return _sessionManager.SendMessageToUserSessions(new List<Guid> { userId }, "UserDataChanged", () => GetUserDataChangeInfo(userId, changedItems), cancellationToken);
}
private UserDataChangeInfo GetUserDataChangeInfo(Guid userId, List<BaseItem> changedItems)
{
var user = _userManager.GetUserById(userId);
2018-09-12 17:26:21 +00:00
var dtoList = changedItems
2019-02-02 11:27:06 +00:00
.GroupBy(x => x.Id)
.Select(x => x.First())
2018-09-12 17:26:21 +00:00
.Select(i =>
{
2018-09-12 17:26:21 +00:00
var dto = _userDataManager.GetUserDataDto(i, user);
dto.ItemId = i.Id.ToString("N", CultureInfo.InvariantCulture);
2018-09-12 17:26:21 +00:00
return dto;
})
.ToArray();
var userIdString = userId.ToString("N", CultureInfo.InvariantCulture);
2018-09-12 17:26:21 +00:00
return new UserDataChangeInfo
{
UserId = userIdString,
2018-09-12 17:26:21 +00:00
UserDataList = dtoList
};
}
public void Dispose()
{
if (UpdateTimer != null)
{
UpdateTimer.Dispose();
UpdateTimer = null;
}
_userDataManager.UserDataSaved -= _userDataManager_UserDataSaved;
}
}
}