| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Threading; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using MediaBrowser.Controller.Entities; |
| | 7 | | using MediaBrowser.Controller.Library; |
| | 8 | | using MediaBrowser.Controller.Session; |
| | 9 | | using MediaBrowser.Model.Entities; |
| | 10 | | using MediaBrowser.Model.Session; |
| | 11 | | using Microsoft.Extensions.Hosting; |
| | 12 | |
|
| | 13 | | namespace Emby.Server.Implementations.EntryPoints |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// <see cref="IHostedService"/> responsible for notifying users when associated item data is updated. |
| | 17 | | /// </summary> |
| | 18 | | public sealed class UserDataChangeNotifier : IHostedService, IDisposable |
| | 19 | | { |
| | 20 | | private const int UpdateDuration = 500; |
| | 21 | |
|
| | 22 | | private readonly ISessionManager _sessionManager; |
| | 23 | | private readonly IUserDataManager _userDataManager; |
| | 24 | | private readonly IUserManager _userManager; |
| | 25 | |
|
| 21 | 26 | | private readonly Dictionary<Guid, List<BaseItem>> _changedItems = new(); |
| 21 | 27 | | private readonly Lock _syncLock = new(); |
| | 28 | |
|
| | 29 | | private Timer? _updateTimer; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Initializes a new instance of the <see cref="UserDataChangeNotifier"/> class. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="userDataManager">The <see cref="IUserDataManager"/>.</param> |
| | 35 | | /// <param name="sessionManager">The <see cref="ISessionManager"/>.</param> |
| | 36 | | /// <param name="userManager">The <see cref="IUserManager"/>.</param> |
| | 37 | | public UserDataChangeNotifier( |
| | 38 | | IUserDataManager userDataManager, |
| | 39 | | ISessionManager sessionManager, |
| | 40 | | IUserManager userManager) |
| | 41 | | { |
| 21 | 42 | | _userDataManager = userDataManager; |
| 21 | 43 | | _sessionManager = sessionManager; |
| 21 | 44 | | _userManager = userManager; |
| 21 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <inheritdoc /> |
| | 48 | | public Task StartAsync(CancellationToken cancellationToken) |
| | 49 | | { |
| 21 | 50 | | _userDataManager.UserDataSaved += OnUserDataManagerUserDataSaved; |
| | 51 | |
|
| 21 | 52 | | return Task.CompletedTask; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | /// <inheritdoc /> |
| | 56 | | public Task StopAsync(CancellationToken cancellationToken) |
| | 57 | | { |
| 21 | 58 | | _userDataManager.UserDataSaved -= OnUserDataManagerUserDataSaved; |
| | 59 | |
|
| 21 | 60 | | return Task.CompletedTask; |
| | 61 | | } |
| | 62 | |
|
| | 63 | | private void OnUserDataManagerUserDataSaved(object? sender, UserDataSaveEventArgs e) |
| | 64 | | { |
| 0 | 65 | | if (e.SaveReason == UserDataSaveReason.PlaybackProgress) |
| | 66 | | { |
| 0 | 67 | | return; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | lock (_syncLock) |
| | 71 | | { |
| 0 | 72 | | if (_updateTimer is null) |
| | 73 | | { |
| 0 | 74 | | _updateTimer = new Timer( |
| 0 | 75 | | UpdateTimerCallback, |
| 0 | 76 | | null, |
| 0 | 77 | | UpdateDuration, |
| 0 | 78 | | Timeout.Infinite); |
| | 79 | | } |
| | 80 | | else |
| | 81 | | { |
| 0 | 82 | | _updateTimer.Change(UpdateDuration, Timeout.Infinite); |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | if (!_changedItems.TryGetValue(e.UserId, out List<BaseItem>? keys)) |
| | 86 | | { |
| 0 | 87 | | keys = new List<BaseItem>(); |
| 0 | 88 | | _changedItems[e.UserId] = keys; |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | keys.Add(e.Item); |
| | 92 | |
|
| 0 | 93 | | var baseItem = e.Item; |
| | 94 | |
|
| | 95 | | // Go up one level for indicators |
| 0 | 96 | | if (baseItem is not null) |
| | 97 | | { |
| 0 | 98 | | var parent = baseItem.GetOwner() ?? baseItem.GetParent(); |
| | 99 | |
|
| 0 | 100 | | if (parent is not null) |
| | 101 | | { |
| 0 | 102 | | keys.Add(parent); |
| | 103 | | } |
| | 104 | | } |
| 0 | 105 | | } |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | private async void UpdateTimerCallback(object? state) |
| | 109 | | { |
| | 110 | | List<KeyValuePair<Guid, List<BaseItem>>> changes; |
| | 111 | | lock (_syncLock) |
| | 112 | | { |
| | 113 | | // Remove dupes in case some were saved multiple times |
| | 114 | | changes = _changedItems.ToList(); |
| | 115 | | _changedItems.Clear(); |
| | 116 | |
|
| | 117 | | if (_updateTimer is not null) |
| | 118 | | { |
| | 119 | | _updateTimer.Dispose(); |
| | 120 | | _updateTimer = null; |
| | 121 | | } |
| | 122 | | } |
| | 123 | |
|
| | 124 | | foreach (var (userId, changedItems) in changes) |
| | 125 | | { |
| | 126 | | await _sessionManager.SendMessageToUserSessions( |
| | 127 | | [userId], |
| | 128 | | SessionMessageType.UserDataChanged, |
| | 129 | | () => GetUserDataChangeInfo(userId, changedItems), |
| | 130 | | default).ConfigureAwait(false); |
| | 131 | | } |
| | 132 | | } |
| | 133 | |
|
| | 134 | | private UserDataChangeInfo GetUserDataChangeInfo(Guid userId, List<BaseItem> changedItems) |
| | 135 | | { |
| 0 | 136 | | var user = _userManager.GetUserById(userId) |
| 0 | 137 | | ?? throw new ArgumentException("Invalid user ID", nameof(userId)); |
| | 138 | |
|
| 0 | 139 | | return new UserDataChangeInfo |
| 0 | 140 | | { |
| 0 | 141 | | UserId = userId, |
| 0 | 142 | | UserDataList = changedItems |
| 0 | 143 | | .DistinctBy(x => x.Id) |
| 0 | 144 | | .Select(i => |
| 0 | 145 | | { |
| 0 | 146 | | var dto = _userDataManager.GetUserDataDto(i, user); |
| 0 | 147 | | if (dto is null) |
| 0 | 148 | | { |
| 0 | 149 | | return null!; |
| 0 | 150 | | } |
| 0 | 151 | |
|
| 0 | 152 | | dto.ItemId = i.Id; |
| 0 | 153 | | return dto; |
| 0 | 154 | | }) |
| 0 | 155 | | .Where(e => e is not null) |
| 0 | 156 | | .ToArray() |
| 0 | 157 | | }; |
| | 158 | | } |
| | 159 | |
|
| | 160 | | /// <inheritdoc /> |
| | 161 | | public void Dispose() |
| | 162 | | { |
| 21 | 163 | | _updateTimer?.Dispose(); |
| 21 | 164 | | _updateTimer = null; |
| 21 | 165 | | } |
| | 166 | | } |
| | 167 | | } |