2019-11-01 10:38:54 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
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.Plugins;
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Session;
|
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
|
|
|
{
|
2020-02-06 07:20:23 -07:00
|
|
|
public sealed class UserDataChangeNotifier : IServerEntryPoint
|
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
|
|
|
|
2020-02-06 07:20:23 -07:00
|
|
|
private readonly Dictionary<Guid, List<BaseItem>> _changedItems = new Dictionary<Guid, List<BaseItem>>();
|
|
|
|
|
2013-10-03 11:02:23 -07:00
|
|
|
private readonly object _syncLock = new object();
|
2020-02-06 07:20:23 -07:00
|
|
|
private Timer _updateTimer;
|
2013-10-03 11:02:23 -07:00
|
|
|
|
2020-02-06 07:20:23 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-27 07:40:37 -07:00
|
|
|
public Task RunAsync()
|
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
|
|
|
}
|
|
|
|
|
2020-02-06 07:20:23 -07:00
|
|
|
void OnUserDataManagerUserDataSaved(object sender, UserDataSaveEventArgs e)
|
2013-10-03 11:02:23 -07:00
|
|
|
{
|
|
|
|
if (e.SaveReason == UserDataSaveReason.PlaybackProgress)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_syncLock)
|
|
|
|
{
|
2020-02-06 07:20:23 -07:00
|
|
|
if (_updateTimer == 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
|
|
|
}
|
|
|
|
|
2019-01-17 10:47:41 -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
|
|
|
|
if (baseItem != null)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
var parent = baseItem.GetOwner() ?? baseItem.GetParent();
|
2014-07-03 19:22:57 -07:00
|
|
|
|
|
|
|
if (parent != null)
|
|
|
|
{
|
|
|
|
keys.Add(parent);
|
|
|
|
}
|
|
|
|
}
|
2013-10-03 11:02:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateTimerCallback(object state)
|
|
|
|
{
|
|
|
|
lock (_syncLock)
|
|
|
|
{
|
|
|
|
// Remove dupes in case some were saved multiple times
|
2014-07-03 19:22:57 -07:00
|
|
|
var changes = _changedItems.ToList();
|
|
|
|
_changedItems.Clear();
|
2013-10-03 11:02:23 -07:00
|
|
|
|
2016-08-05 14:15:48 -07:00
|
|
|
var task = SendNotifications(changes, CancellationToken.None);
|
2013-10-03 11:02:23 -07:00
|
|
|
|
2020-02-06 07:20:23 -07:00
|
|
|
if (_updateTimer != 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
private async Task SendNotifications(List<KeyValuePair<Guid, List<BaseItem>>> changes, CancellationToken cancellationToken)
|
2013-10-03 11:02:23 -07:00
|
|
|
{
|
|
|
|
foreach (var pair in changes)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
await SendNotifications(pair.Key, pair.Value, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Task SendNotifications(Guid userId, List<BaseItem> changedItems, CancellationToken cancellationToken)
|
|
|
|
{
|
2020-09-25 00:25:59 -07:00
|
|
|
return _sessionManager.SendMessageToUserSessions(new List<Guid> { userId }, SessionMessageType.UserDataChanged, () => GetUserDataChangeInfo(userId, changedItems), cancellationToken);
|
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
|
|
|
var dtoList = changedItems
|
2019-02-02 04:27:06 -07:00
|
|
|
.GroupBy(x => x.Id)
|
|
|
|
.Select(x => x.First())
|
2018-09-12 10:26:21 -07:00
|
|
|
.Select(i =>
|
2013-10-03 11:02:23 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
var dto = _userDataManager.GetUserDataDto(i, user);
|
2019-02-28 15:22:57 -07:00
|
|
|
dto.ItemId = i.Id.ToString("N", CultureInfo.InvariantCulture);
|
2018-09-12 10:26:21 -07:00
|
|
|
return dto;
|
|
|
|
})
|
|
|
|
.ToArray();
|
2013-10-03 11:02:23 -07:00
|
|
|
|
2019-02-28 15:22:57 -07:00
|
|
|
var userIdString = userId.ToString("N", CultureInfo.InvariantCulture);
|
2013-10-03 11:02:23 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
return new UserDataChangeInfo
|
|
|
|
{
|
|
|
|
UserId = userIdString,
|
2013-10-03 11:02:23 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
UserDataList = dtoList
|
|
|
|
};
|
2013-10-03 11:02:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2020-02-06 07:20:23 -07:00
|
|
|
if (_updateTimer != 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
|
|
|
}
|
|
|
|
|
2020-02-06 07:20:23 -07:00
|
|
|
_userDataManager.UserDataSaved -= OnUserDataManagerUserDataSaved;
|
2013-10-03 11:02:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|