2014-07-04 02:22:57 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-10-03 18:02:23 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Session;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-10-22 02:08:34 +00:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
2016-11-03 22:06:00 +00:00
|
|
|
|
using MediaBrowser.Model.Threading;
|
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
|
|
|
|
{
|
|
|
|
|
class UserDataChangeNotifier : IServerEntryPoint
|
|
|
|
|
{
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IUserDataManager _userDataManager;
|
2014-07-04 02:22:57 +00:00
|
|
|
|
private readonly IUserManager _userManager;
|
2013-10-03 18:02:23 +00:00
|
|
|
|
|
|
|
|
|
private readonly object _syncLock = new object();
|
2016-11-03 22:06:00 +00:00
|
|
|
|
private ITimer UpdateTimer { get; set; }
|
|
|
|
|
private readonly ITimerFactory _timerFactory;
|
2013-10-10 16:55:07 +00:00
|
|
|
|
private const int UpdateDuration = 500;
|
2013-10-03 18:02:23 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
private readonly Dictionary<Guid, List<BaseItem>> _changedItems = new Dictionary<Guid, List<BaseItem>>();
|
2013-10-03 18:02:23 +00:00
|
|
|
|
|
2016-11-03 22:06:00 +00:00
|
|
|
|
public UserDataChangeNotifier(IUserDataManager userDataManager, ISessionManager sessionManager, ILogger logger, IUserManager userManager, ITimerFactory timerFactory)
|
2013-10-03 18:02:23 +00:00
|
|
|
|
{
|
|
|
|
|
_userDataManager = userDataManager;
|
|
|
|
|
_sessionManager = sessionManager;
|
|
|
|
|
_logger = logger;
|
2014-07-04 02:22:57 +00:00
|
|
|
|
_userManager = userManager;
|
2016-11-03 22:06:00 +00:00
|
|
|
|
_timerFactory = timerFactory;
|
2013-10-03 18:02:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
_userDataManager.UserDataSaved += _userDataManager_UserDataSaved;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _userDataManager_UserDataSaved(object sender, UserDataSaveEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.SaveReason == UserDataSaveReason.PlaybackProgress)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock (_syncLock)
|
|
|
|
|
{
|
|
|
|
|
if (UpdateTimer == null)
|
|
|
|
|
{
|
2016-11-03 22:06:00 +00:00
|
|
|
|
UpdateTimer = _timerFactory.Create(UpdateTimerCallback, null, UpdateDuration,
|
2013-10-03 18:02:23 +00:00
|
|
|
|
Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UpdateTimer.Change(UpdateDuration, Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
List<BaseItem> keys;
|
2013-10-03 18:02:23 +00:00
|
|
|
|
|
2014-07-04 02:22:57 +00:00
|
|
|
|
if (!_changedItems.TryGetValue(e.UserId, out 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
|
|
|
|
|
if (baseItem != null)
|
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
|
var parent = baseItem.GetOwner() ?? baseItem.GetParent();
|
2014-07-04 02:22:57 +00:00
|
|
|
|
|
|
|
|
|
if (parent != null)
|
|
|
|
|
{
|
|
|
|
|
keys.Add(parent);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-03 18:02:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateTimerCallback(object state)
|
|
|
|
|
{
|
|
|
|
|
lock (_syncLock)
|
|
|
|
|
{
|
|
|
|
|
// Remove dupes in case some were saved multiple times
|
2014-07-04 02:22:57 +00:00
|
|
|
|
var changes = _changedItems.ToList();
|
|
|
|
|
_changedItems.Clear();
|
2013-10-03 18:02:23 +00:00
|
|
|
|
|
2016-08-05 21:15:48 +00:00
|
|
|
|
var task = SendNotifications(changes, CancellationToken.None);
|
2013-10-03 18:02:23 +00:00
|
|
|
|
|
|
|
|
|
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)
|
2013-10-03 18:02:23 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
2013-10-03 18:02:23 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
var dtoList = changedItems
|
|
|
|
|
.DistinctBy(i => i.Id)
|
|
|
|
|
.Select(i =>
|
2013-10-03 18:02:23 +00:00
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
|
var dto = _userDataManager.GetUserDataDto(i, user);
|
|
|
|
|
dto.ItemId = i.Id.ToString("N");
|
|
|
|
|
return dto;
|
|
|
|
|
})
|
|
|
|
|
.ToArray();
|
2013-10-03 18:02:23 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
var userIdString = userId.ToString("N");
|
2013-10-03 18:02:23 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
return new UserDataChangeInfo
|
|
|
|
|
{
|
|
|
|
|
UserId = userIdString,
|
2013-10-03 18:02:23 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
UserDataList = dtoList
|
|
|
|
|
};
|
2013-10-03 18:02:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (UpdateTimer != null)
|
|
|
|
|
{
|
|
|
|
|
UpdateTimer.Dispose();
|
|
|
|
|
UpdateTimer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_userDataManager.UserDataSaved -= _userDataManager_UserDataSaved;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|