diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/Session/SessionManager.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/Session/SessionManager.cs | 97 |
1 files changed, 57 insertions, 40 deletions
diff --git a/MediaBrowser.Server.Implementations/Session/SessionManager.cs b/MediaBrowser.Server.Implementations/Session/SessionManager.cs index 88f11c368..84aab5e1f 100644 --- a/MediaBrowser.Server.Implementations/Session/SessionManager.cs +++ b/MediaBrowser.Server.Implementations/Session/SessionManager.cs @@ -404,6 +404,10 @@ namespace MediaBrowser.Server.Implementations.Session /// <returns>SessionInfo.</returns> private async Task<SessionInfo> GetSessionInfo(string appName, string appVersion, string deviceId, string deviceName, string remoteEndPoint, User user) { + if (string.IsNullOrWhiteSpace(deviceId)) + { + throw new ArgumentNullException("deviceId"); + } var key = GetSessionKey(appName, deviceId); await _sessionLock.WaitAsync(CancellationToken.None).ConfigureAwait(false); @@ -601,11 +605,9 @@ namespace MediaBrowser.Server.Implementations.Session if (libraryItem != null) { - var key = libraryItem.GetUserDataKey(); - foreach (var user in users) { - await OnPlaybackStart(user.Id, key, libraryItem).ConfigureAwait(false); + await OnPlaybackStart(user.Id, libraryItem).ConfigureAwait(false); } } @@ -632,12 +634,11 @@ namespace MediaBrowser.Server.Implementations.Session /// Called when [playback start]. /// </summary> /// <param name="userId">The user identifier.</param> - /// <param name="userDataKey">The user data key.</param> /// <param name="item">The item.</param> /// <returns>Task.</returns> - private async Task OnPlaybackStart(Guid userId, string userDataKey, IHasUserData item) + private async Task OnPlaybackStart(Guid userId, IHasUserData item) { - var data = _userDataRepository.GetUserData(userId, userDataKey); + var data = _userDataRepository.GetUserData(userId, item); data.PlayCount++; data.LastPlayedDate = DateTime.UtcNow; @@ -676,11 +677,9 @@ namespace MediaBrowser.Server.Implementations.Session if (libraryItem != null) { - var key = libraryItem.GetUserDataKey(); - foreach (var user in users) { - await OnPlaybackProgress(user, key, libraryItem, info).ConfigureAwait(false); + await OnPlaybackProgress(user, libraryItem, info).ConfigureAwait(false); } } @@ -714,9 +713,9 @@ namespace MediaBrowser.Server.Implementations.Session StartIdleCheckTimer(); } - private async Task OnPlaybackProgress(User user, string userDataKey, BaseItem item, PlaybackProgressInfo info) + private async Task OnPlaybackProgress(User user, BaseItem item, PlaybackProgressInfo info) { - var data = _userDataRepository.GetUserData(user.Id, userDataKey); + var data = _userDataRepository.GetUserData(user.Id, item); var positionTicks = info.PositionTicks; @@ -811,11 +810,9 @@ namespace MediaBrowser.Server.Implementations.Session if (libraryItem != null) { - var key = libraryItem.GetUserDataKey(); - foreach (var user in users) { - playedToCompletion = await OnPlaybackStopped(user.Id, key, libraryItem, info.PositionTicks, info.Failed).ConfigureAwait(false); + playedToCompletion = await OnPlaybackStopped(user.Id, libraryItem, info.PositionTicks, info.Failed).ConfigureAwait(false); } } @@ -848,14 +845,14 @@ namespace MediaBrowser.Server.Implementations.Session await SendPlaybackStoppedNotification(session, CancellationToken.None).ConfigureAwait(false); } - private async Task<bool> OnPlaybackStopped(Guid userId, string userDataKey, BaseItem item, long? positionTicks, bool playbackFailed) + private async Task<bool> OnPlaybackStopped(Guid userId, BaseItem item, long? positionTicks, bool playbackFailed) { bool playedToCompletion = false; if (!playbackFailed) { - var data = _userDataRepository.GetUserData(userId, userDataKey); - + var data = _userDataRepository.GetUserData(userId, item); + if (positionTicks.HasValue) { playedToCompletion = _userDataRepository.UpdatePlayState(item, data, positionTicks.Value); @@ -935,7 +932,7 @@ namespace MediaBrowser.Server.Implementations.Session return session.SessionController.SendGeneralCommand(command, cancellationToken); } - public Task SendPlayCommand(string controllingSessionId, string sessionId, PlayRequest command, CancellationToken cancellationToken) + public async Task SendPlayCommand(string controllingSessionId, string sessionId, PlayRequest command, CancellationToken cancellationToken) { var session = GetSessionToRemoteControl(sessionId); @@ -953,7 +950,14 @@ namespace MediaBrowser.Server.Implementations.Session } else { - items = command.ItemIds.SelectMany(i => TranslateItemForPlayback(i, user)) + var list = new List<BaseItem>(); + foreach (var itemId in command.ItemIds) + { + var subItems = await TranslateItemForPlayback(itemId, user).ConfigureAwait(false); + list.AddRange(subItems); + } + + items = list .Where(i => i.LocationType != LocationType.Virtual) .ToList(); } @@ -1016,10 +1020,10 @@ namespace MediaBrowser.Server.Implementations.Session command.ControllingUserId = controllingSession.UserId.Value.ToString("N"); } - return session.SessionController.SendPlayCommand(command, cancellationToken); + await session.SessionController.SendPlayCommand(command, cancellationToken).ConfigureAwait(false); } - private IEnumerable<BaseItem> TranslateItemForPlayback(string id, User user) + private async Task<List<BaseItem>> TranslateItemForPlayback(string id, User user) { var item = _libraryManager.GetItemById(id); @@ -1033,29 +1037,34 @@ namespace MediaBrowser.Server.Implementations.Session if (byName != null) { - var itemFilter = byName.GetItemFilter(); - - var items = user == null ? - _libraryManager.RootFolder.GetRecursiveChildren(i => !i.IsFolder && itemFilter(i)) : - user.RootFolder.GetRecursiveChildren(user, i => !i.IsFolder && itemFilter(i)); + var items = byName.GetTaggedItems(new InternalItemsQuery(user) + { + IsFolder = false, + Recursive = true + }); return FilterToSingleMediaType(items) - .OrderBy(i => i.SortName); + .OrderBy(i => i.SortName) + .ToList(); } if (item.IsFolder) { var folder = (Folder)item; - var items = user == null ? - folder.GetRecursiveChildren(i => !i.IsFolder) : - folder.GetRecursiveChildren(user, i => !i.IsFolder); + var itemsResult = await folder.GetItems(new InternalItemsQuery(user) + { + Recursive = true, + IsFolder = false + + }).ConfigureAwait(false); - return FilterToSingleMediaType(items) - .OrderBy(i => i.SortName); + return FilterToSingleMediaType(itemsResult.Items) + .OrderBy(i => i.SortName) + .ToList(); } - return new[] { item }; + return new List<BaseItem> { item }; } private IEnumerable<BaseItem> FilterToSingleMediaType(IEnumerable<BaseItem> items) @@ -1125,11 +1134,11 @@ namespace MediaBrowser.Server.Implementations.Session /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> - public Task SendRestartRequiredNotification(CancellationToken cancellationToken) + public async Task SendRestartRequiredNotification(CancellationToken cancellationToken) { var sessions = Sessions.Where(i => i.IsActive && i.SessionController != null).ToList(); - var info = _appHost.GetSystemInfo(); + var info = await _appHost.GetSystemInfo().ConfigureAwait(false); var tasks = sessions.Select(session => Task.Run(async () => { @@ -1144,7 +1153,7 @@ namespace MediaBrowser.Server.Implementations.Session }, cancellationToken)); - return Task.WhenAll(tasks); + await Task.WhenAll(tasks).ConfigureAwait(false); } /// <summary> @@ -1374,8 +1383,8 @@ namespace MediaBrowser.Server.Implementations.Session ServerId = _appHost.SystemId }; } - - + + private async Task<string> GetAuthorizationToken(string userId, string deviceId, string app, string appVersion, string deviceName) { var existing = _authRepo.Get(new AuthenticationInfoQuery @@ -1451,7 +1460,7 @@ namespace MediaBrowser.Server.Implementations.Session } } - public async Task RevokeUserTokens(string userId) + public async Task RevokeUserTokens(string userId, string currentAccessToken) { var existing = _authRepo.Get(new AuthenticationInfoQuery { @@ -1461,7 +1470,10 @@ namespace MediaBrowser.Server.Implementations.Session foreach (var info in existing.Items) { - await Logout(info.AccessToken).ConfigureAwait(false); + if (!string.Equals(currentAccessToken, info.AccessToken, StringComparison.OrdinalIgnoreCase)) + { + await Logout(info.AccessToken).ConfigureAwait(false); + } } } @@ -1752,6 +1764,11 @@ namespace MediaBrowser.Server.Implementations.Session public void ReportNowViewingItem(string sessionId, string itemId) { + if (string.IsNullOrWhiteSpace(itemId)) + { + throw new ArgumentNullException("itemId"); + } + var item = _libraryManager.GetItemById(new Guid(itemId)); var info = GetItemInfo(item, null, null); |
