aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Session/SessionManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations/Session/SessionManager.cs')
-rw-r--r--Emby.Server.Implementations/Session/SessionManager.cs431
1 files changed, 171 insertions, 260 deletions
diff --git a/Emby.Server.Implementations/Session/SessionManager.cs b/Emby.Server.Implementations/Session/SessionManager.cs
index 62df354fd..b4a622ccf 100644
--- a/Emby.Server.Implementations/Session/SessionManager.cs
+++ b/Emby.Server.Implementations/Session/SessionManager.cs
@@ -10,8 +10,11 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
+using Jellyfin.Data.Entities.Security;
using Jellyfin.Data.Enums;
using Jellyfin.Data.Events;
+using Jellyfin.Data.Queries;
+using Jellyfin.Extensions;
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller;
@@ -21,12 +24,11 @@ using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Events;
+using MediaBrowser.Controller.Events.Authentication;
using MediaBrowser.Controller.Events.Session;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
-using MediaBrowser.Controller.Security;
using MediaBrowser.Controller.Session;
-using MediaBrowser.Model.Devices;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Library;
@@ -54,13 +56,12 @@ namespace Emby.Server.Implementations.Session
private readonly IImageProcessor _imageProcessor;
private readonly IMediaSourceManager _mediaSourceManager;
private readonly IServerApplicationHost _appHost;
- private readonly IAuthenticationRepository _authRepo;
private readonly IDeviceManager _deviceManager;
/// <summary>
/// The active connections.
/// </summary>
- private readonly ConcurrentDictionary<string, SessionInfo> _activeConnections = new (StringComparer.OrdinalIgnoreCase);
+ private readonly ConcurrentDictionary<string, SessionInfo> _activeConnections = new(StringComparer.OrdinalIgnoreCase);
private Timer _idleTimer;
@@ -77,7 +78,6 @@ namespace Emby.Server.Implementations.Session
IDtoService dtoService,
IImageProcessor imageProcessor,
IServerApplicationHost appHost,
- IAuthenticationRepository authRepo,
IDeviceManager deviceManager,
IMediaSourceManager mediaSourceManager)
{
@@ -90,19 +90,12 @@ namespace Emby.Server.Implementations.Session
_dtoService = dtoService;
_imageProcessor = imageProcessor;
_appHost = appHost;
- _authRepo = authRepo;
_deviceManager = deviceManager;
_mediaSourceManager = mediaSourceManager;
_deviceManager.DeviceOptionsUpdated += OnDeviceManagerDeviceOptionsUpdated;
}
- /// <inheritdoc />
- public event EventHandler<GenericEventArgs<AuthenticationRequest>> AuthenticationFailed;
-
- /// <inheritdoc />
- public event EventHandler<GenericEventArgs<AuthenticationResult>> AuthenticationSucceeded;
-
/// <summary>
/// Occurs when playback has started.
/// </summary>
@@ -202,7 +195,7 @@ namespace Emby.Server.Implementations.Session
{
var capabilities = _deviceManager.GetCapabilities(info.DeviceId);
- if (capabilities != null)
+ if (capabilities is not null)
{
ReportCapabilities(info, capabilities, false);
}
@@ -237,12 +230,12 @@ namespace Emby.Server.Implementations.Session
}
/// <inheritdoc />
- public void UpdateDeviceName(string sessionId, string deviceName)
+ public void UpdateDeviceName(string sessionId, string reportedDeviceName)
{
var session = GetSession(sessionId);
- if (session != null)
+ if (session is not null)
{
- session.DeviceName = deviceName;
+ session.DeviceName = reportedDeviceName;
}
}
@@ -256,7 +249,7 @@ namespace Emby.Server.Implementations.Session
/// <param name="remoteEndPoint">The remote end point.</param>
/// <param name="user">The user.</param>
/// <returns>SessionInfo.</returns>
- public SessionInfo LogSessionActivity(
+ public async Task<SessionInfo> LogSessionActivity(
string appName,
string appVersion,
string deviceId,
@@ -266,27 +259,16 @@ namespace Emby.Server.Implementations.Session
{
CheckDisposed();
- if (string.IsNullOrEmpty(appName))
- {
- throw new ArgumentNullException(nameof(appName));
- }
-
- if (string.IsNullOrEmpty(appVersion))
- {
- throw new ArgumentNullException(nameof(appVersion));
- }
-
- if (string.IsNullOrEmpty(deviceId))
- {
- throw new ArgumentNullException(nameof(deviceId));
- }
+ ArgumentException.ThrowIfNullOrEmpty(appName);
+ ArgumentException.ThrowIfNullOrEmpty(appVersion);
+ ArgumentException.ThrowIfNullOrEmpty(deviceId);
var activityDate = DateTime.UtcNow;
- var session = GetSessionInfo(appName, appVersion, deviceId, deviceName, remoteEndPoint, user);
+ var session = await GetSessionInfo(appName, appVersion, deviceId, deviceName, remoteEndPoint, user).ConfigureAwait(false);
var lastActivityDate = session.LastActivityDate;
session.LastActivityDate = activityDate;
- if (user != null)
+ if (user is not null)
{
var userLastActivityDate = user.LastActivityDate ?? DateTime.MinValue;
@@ -295,7 +277,7 @@ namespace Emby.Server.Implementations.Session
try
{
user.LastActivityDate = activityDate;
- _userManager.UpdateUser(user);
+ await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
}
catch (DbUpdateConcurrencyException e)
{
@@ -318,26 +300,30 @@ namespace Emby.Server.Implementations.Session
}
/// <inheritdoc />
- public void OnSessionControllerConnected(SessionInfo info)
+ public void OnSessionControllerConnected(SessionInfo session)
{
EventHelper.QueueEventIfNotNull(
SessionControllerConnected,
this,
new SessionEventArgs
{
- SessionInfo = info
+ SessionInfo = session
},
_logger);
}
/// <inheritdoc />
- public void CloseIfNeeded(SessionInfo session)
+ public async Task CloseIfNeededAsync(SessionInfo session)
{
if (!session.SessionControllers.Any(i => i.IsSessionActive))
{
var key = GetSessionKey(session.Client, session.DeviceId);
_activeConnections.TryRemove(key, out _);
+ if (!string.IsNullOrEmpty(session.PlayState?.LiveStreamId))
+ {
+ await _mediaSourceManager.CloseLiveStream(session.PlayState.LiveStreamId).ConfigureAwait(false);
+ }
OnSessionEnded(session);
}
@@ -349,7 +335,7 @@ namespace Emby.Server.Implementations.Session
CheckDisposed();
var session = GetSession(sessionId, false);
- if (session != null)
+ if (session is not null)
{
var key = GetSessionKey(session.Client, session.DeviceId);
@@ -375,11 +361,11 @@ namespace Emby.Server.Implementations.Session
info.MediaSourceId = info.ItemId.ToString("N", CultureInfo.InvariantCulture);
}
- if (!info.ItemId.Equals(Guid.Empty) && info.Item == null && libraryItem != null)
+ if (!info.ItemId.Equals(default) && info.Item is null && libraryItem is not null)
{
var current = session.NowPlayingItem;
- if (current == null || !info.ItemId.Equals(current.Id))
+ if (current is null || !info.ItemId.Equals(current.Id))
{
var runtimeTicks = libraryItem.RunTimeTicks;
@@ -388,7 +374,7 @@ namespace Emby.Server.Implementations.Session
{
mediaSource = await GetMediaSource(libraryItem, info.MediaSourceId, info.LiveStreamId).ConfigureAwait(false);
- if (mediaSource != null)
+ if (mediaSource is not null)
{
runtimeTicks = mediaSource.RunTimeTicks;
}
@@ -415,6 +401,7 @@ namespace Emby.Server.Implementations.Session
session.PlayState.IsPaused = info.IsPaused;
session.PlayState.PositionTicks = info.PositionTicks;
session.PlayState.MediaSourceId = info.MediaSourceId;
+ session.PlayState.LiveStreamId = info.LiveStreamId;
session.PlayState.CanSeek = info.CanSeek;
session.PlayState.IsMuted = info.IsMuted;
session.PlayState.VolumeLevel = info.VolumeLevel;
@@ -426,9 +413,14 @@ namespace Emby.Server.Implementations.Session
var nowPlayingQueue = info.NowPlayingQueue;
- if (nowPlayingQueue != null)
+ if (nowPlayingQueue?.Length > 0)
{
session.NowPlayingQueue = nowPlayingQueue;
+
+ var itemIds = nowPlayingQueue.Select(queue => queue.Id).ToArray();
+ session.NowPlayingQueueFullItems = _dtoService.GetBaseItemDtos(
+ _libraryManager.GetItemList(new InternalItemsQuery { ItemIds = itemIds }),
+ new DtoOptions(true));
}
}
@@ -460,7 +452,7 @@ namespace Emby.Server.Implementations.Session
/// <param name="remoteEndPoint">The remote end point.</param>
/// <param name="user">The user.</param>
/// <returns>SessionInfo.</returns>
- private SessionInfo GetSessionInfo(
+ private async Task<SessionInfo> GetSessionInfo(
string appName,
string appVersion,
string deviceId,
@@ -470,22 +462,21 @@ namespace Emby.Server.Implementations.Session
{
CheckDisposed();
- if (string.IsNullOrEmpty(deviceId))
- {
- throw new ArgumentNullException(nameof(deviceId));
- }
+ ArgumentException.ThrowIfNullOrEmpty(deviceId);
var key = GetSessionKey(appName, deviceId);
CheckDisposed();
- var sessionInfo = _activeConnections.GetOrAdd(
- key,
- k => CreateSession(k, appName, appVersion, deviceId, deviceName, remoteEndPoint, user));
+ if (!_activeConnections.TryGetValue(key, out var sessionInfo))
+ {
+ _activeConnections[key] = await CreateSession(key, appName, appVersion, deviceId, deviceName, remoteEndPoint, user).ConfigureAwait(false);
+ sessionInfo = _activeConnections[key];
+ }
sessionInfo.UserId = user?.Id ?? Guid.Empty;
sessionInfo.UserName = user?.Username;
- sessionInfo.UserPrimaryImageTag = user?.ProfileImage == null ? null : GetImageCacheTag(user);
+ sessionInfo.UserPrimaryImageTag = user?.ProfileImage is null ? null : GetImageCacheTag(user);
sessionInfo.RemoteEndPoint = remoteEndPoint;
sessionInfo.Client = appName;
@@ -496,7 +487,7 @@ namespace Emby.Server.Implementations.Session
sessionInfo.ApplicationVersion = appVersion;
- if (user == null)
+ if (user is null)
{
sessionInfo.AdditionalUsers = Array.Empty<SessionUserInfo>();
}
@@ -504,7 +495,7 @@ namespace Emby.Server.Implementations.Session
return sessionInfo;
}
- private SessionInfo CreateSession(
+ private async Task<SessionInfo> CreateSession(
string key,
string appName,
string appVersion,
@@ -526,7 +517,7 @@ namespace Emby.Server.Implementations.Session
sessionInfo.UserId = user?.Id ?? Guid.Empty;
sessionInfo.UserName = username;
- sessionInfo.UserPrimaryImageTag = user?.ProfileImage == null ? null : GetImageCacheTag(user);
+ sessionInfo.UserPrimaryImageTag = user?.ProfileImage is null ? null : GetImageCacheTag(user);
sessionInfo.RemoteEndPoint = remoteEndPoint;
if (string.IsNullOrEmpty(deviceName))
@@ -534,7 +525,7 @@ namespace Emby.Server.Implementations.Session
deviceName = "Network Device";
}
- var deviceOptions = _deviceManager.GetDeviceOptions(deviceId);
+ var deviceOptions = await _deviceManager.GetDeviceOptions(deviceId).ConfigureAwait(false);
if (string.IsNullOrEmpty(deviceOptions.CustomName))
{
sessionInfo.DeviceName = deviceName;
@@ -553,22 +544,24 @@ namespace Emby.Server.Implementations.Session
{
var users = new List<User>();
- if (session.UserId != Guid.Empty)
+ if (session.UserId.Equals(default))
{
- var user = _userManager.GetUserById(session.UserId);
-
- if (user == null)
- {
- throw new InvalidOperationException("User not found");
- }
+ return users;
+ }
- users.Add(user);
+ var user = _userManager.GetUserById(session.UserId);
- users.AddRange(session.AdditionalUsers
- .Select(i => _userManager.GetUserById(i.UserId))
- .Where(i => i != null));
+ if (user is null)
+ {
+ throw new InvalidOperationException("User not found");
}
+ users.Add(user);
+
+ users.AddRange(session.AdditionalUsers
+ .Select(i => _userManager.GetUserById(i.UserId))
+ .Where(i => i is not null));
+
return users;
}
@@ -579,7 +572,7 @@ namespace Emby.Server.Implementations.Session
private void StopIdleCheckTimer()
{
- if (_idleTimer != null)
+ if (_idleTimer is not null)
{
_idleTimer.Dispose();
_idleTimer = null;
@@ -588,7 +581,7 @@ namespace Emby.Server.Implementations.Session
private async void CheckForIdlePlayback(object state)
{
- var playingSessions = Sessions.Where(i => i.NowPlayingItem != null)
+ var playingSessions = Sessions.Where(i => i.NowPlayingItem is not null)
.ToList();
if (playingSessions.Count > 0)
@@ -606,7 +599,7 @@ namespace Emby.Server.Implementations.Session
await OnPlaybackStopped(new PlaybackStopInfo
{
Item = session.NowPlayingItem,
- ItemId = session.NowPlayingItem == null ? Guid.Empty : session.NowPlayingItem.Id,
+ ItemId = session.NowPlayingItem is null ? Guid.Empty : session.NowPlayingItem.Id,
SessionId = session.Id,
MediaSourceId = session.PlayState?.MediaSourceId,
PositionTicks = session.PlayState?.PositionTicks
@@ -614,11 +607,11 @@ namespace Emby.Server.Implementations.Session
}
catch (Exception ex)
{
- _logger.LogDebug("Error calling OnPlaybackStopped", ex);
+ _logger.LogDebug(ex, "Error calling OnPlaybackStopped");
}
}
- playingSessions = Sessions.Where(i => i.NowPlayingItem != null)
+ playingSessions = Sessions.Where(i => i.NowPlayingItem is not null)
.ToList();
}
@@ -631,7 +624,7 @@ namespace Emby.Server.Implementations.Session
private BaseItem GetNowPlayingItem(SessionInfo session, Guid itemId)
{
var item = session.FullNowPlayingItem;
- if (item != null && item.Id.Equals(itemId))
+ if (item is not null && item.Id.Equals(itemId))
{
return item;
}
@@ -653,14 +646,11 @@ namespace Emby.Server.Implementations.Session
{
CheckDisposed();
- if (info == null)
- {
- throw new ArgumentNullException(nameof(info));
- }
+ ArgumentNullException.ThrowIfNull(info);
var session = GetSession(info.SessionId);
- var libraryItem = info.ItemId == Guid.Empty
+ var libraryItem = info.ItemId.Equals(default)
? null
: GetNowPlayingItem(session, info.ItemId);
@@ -675,7 +665,7 @@ namespace Emby.Server.Implementations.Session
var users = GetUsers(session);
- if (libraryItem != null)
+ if (libraryItem is not null)
{
foreach (var user in users)
{
@@ -692,7 +682,9 @@ namespace Emby.Server.Implementations.Session
DeviceName = session.DeviceName,
ClientName = session.Client,
DeviceId = session.DeviceId,
- Session = session
+ Session = session,
+ PlaybackPositionTicks = info.PositionTicks,
+ PlaySessionId = info.PlaySessionId
};
await _eventManager.PublishAsync(eventArgs).ConfigureAwait(false);
@@ -741,28 +733,32 @@ namespace Emby.Server.Implementations.Session
/// <summary>
/// Used to report playback progress for an item.
/// </summary>
+ /// <param name="info">The playback progress info.</param>
+ /// <param name="isAutomated">Whether this is an automated update.</param>
/// <returns>Task.</returns>
public async Task OnPlaybackProgress(PlaybackProgressInfo info, bool isAutomated)
{
CheckDisposed();
- if (info == null)
- {
- throw new ArgumentNullException(nameof(info));
- }
+ ArgumentNullException.ThrowIfNull(info);
var session = GetSession(info.SessionId);
- var libraryItem = info.ItemId.Equals(Guid.Empty)
+ var libraryItem = info.ItemId.Equals(default)
? null
: GetNowPlayingItem(session, info.ItemId);
await UpdateNowPlayingItem(session, info, libraryItem, !isAutomated).ConfigureAwait(false);
+ if (!string.IsNullOrEmpty(session.DeviceId) && info.PlayMethod != PlayMethod.Transcode)
+ {
+ ClearTranscodingInfo(session.DeviceId);
+ }
+
var users = GetUsers(session);
// only update saved user data on actual check-ins, not automated ones
- if (libraryItem != null && !isAutomated)
+ if (libraryItem is not null && !isAutomated)
{
foreach (var user in users)
{
@@ -876,10 +872,7 @@ namespace Emby.Server.Implementations.Session
{
CheckDisposed();
- if (info == null)
- {
- throw new ArgumentNullException(nameof(info));
- }
+ ArgumentNullException.ThrowIfNull(info);
if (info.PositionTicks.HasValue && info.PositionTicks.Value < 0)
{
@@ -890,7 +883,7 @@ namespace Emby.Server.Implementations.Session
session.StopAutomaticProgress();
- var libraryItem = info.ItemId.Equals(Guid.Empty)
+ var libraryItem = info.ItemId.Equals(default)
? null
: GetNowPlayingItem(session, info.ItemId);
@@ -900,11 +893,11 @@ namespace Emby.Server.Implementations.Session
info.MediaSourceId = info.ItemId.ToString("N", CultureInfo.InvariantCulture);
}
- if (!info.ItemId.Equals(Guid.Empty) && info.Item == null && libraryItem != null)
+ if (!info.ItemId.Equals(default) && info.Item is null && libraryItem is not null)
{
var current = session.NowPlayingItem;
- if (current == null || !info.ItemId.Equals(current.Id))
+ if (current is null || !info.ItemId.Equals(current.Id))
{
MediaSourceInfo mediaSource = null;
@@ -921,7 +914,7 @@ namespace Emby.Server.Implementations.Session
}
}
- if (info.Item != null)
+ if (info.Item is not null)
{
var msString = info.PositionTicks.HasValue ? (info.PositionTicks.Value / 10000).ToString(CultureInfo.InvariantCulture) : "unknown";
@@ -933,7 +926,7 @@ namespace Emby.Server.Implementations.Session
msString);
}
- if (info.NowPlayingQueue != null)
+ if (info.NowPlayingQueue is not null)
{
session.NowPlayingQueue = info.NowPlayingQueue;
}
@@ -945,7 +938,7 @@ namespace Emby.Server.Implementations.Session
var users = GetUsers(session);
var playedToCompletion = false;
- if (libraryItem != null)
+ if (libraryItem is not null)
{
foreach (var user in users)
{
@@ -961,7 +954,7 @@ namespace Emby.Server.Implementations.Session
}
catch (Exception ex)
{
- _logger.LogError("Error closing live stream", ex);
+ _logger.LogError(ex, "Error closing live stream");
}
}
@@ -976,7 +969,8 @@ namespace Emby.Server.Implementations.Session
DeviceName = session.DeviceName,
ClientName = session.Client,
DeviceId = session.DeviceId,
- Session = session
+ Session = session,
+ PlaySessionId = info.PlaySessionId
};
await _eventManager.PublishAsync(eventArgs).ConfigureAwait(false);
@@ -1024,7 +1018,7 @@ namespace Emby.Server.Implementations.Session
private SessionInfo GetSession(string sessionId, bool throwOnMissing = true)
{
var session = Sessions.FirstOrDefault(i => string.Equals(i.Id, sessionId, StringComparison.Ordinal));
- if (session == null && throwOnMissing)
+ if (session is null && throwOnMissing)
{
throw new ResourceNotFoundException(
string.Format(CultureInfo.InvariantCulture, "Session {0} not found.", sessionId));
@@ -1038,7 +1032,7 @@ namespace Emby.Server.Implementations.Session
// Accept either device id or session id
var session = Sessions.FirstOrDefault(i => string.Equals(i.Id, sessionId, StringComparison.Ordinal));
- if (session == null)
+ if (session is null)
{
throw new ResourceNotFoundException(
string.Format(CultureInfo.InvariantCulture, "Session {0} not found.", sessionId));
@@ -1120,7 +1114,7 @@ namespace Emby.Server.Implementations.Session
var session = GetSessionToRemoteControl(sessionId);
- var user = session.UserId == Guid.Empty ? null : _userManager.GetUserById(session.UserId);
+ var user = session.UserId.Equals(default) ? null : _userManager.GetUserById(session.UserId);
List<BaseItem> items;
@@ -1151,7 +1145,7 @@ namespace Emby.Server.Implementations.Session
command.ItemIds = items.Select(i => i.Id).ToArray();
- if (user != null)
+ if (user is not null)
{
if (items.Any(i => i.GetPlayAccess(user) != PlayAccess.Full))
{
@@ -1160,13 +1154,13 @@ namespace Emby.Server.Implementations.Session
}
}
- if (user != null
+ if (user is not null
&& command.ItemIds.Length == 1
&& user.EnableNextEpisodeAutoPlay
&& _libraryManager.GetItemById(command.ItemIds[0]) is Episode episode)
{
var series = episode.Series;
- if (series != null)
+ if (series is not null)
{
var episodes = series.GetEpisodes(
user,
@@ -1175,7 +1169,7 @@ namespace Emby.Server.Implementations.Session
EnableImages = false
})
.Where(i => !i.IsVirtualItem)
- .SkipWhile(i => i.Id != episode.Id)
+ .SkipWhile(i => !i.Id.Equals(episode.Id))
.ToList();
if (episodes.Count > 0)
@@ -1189,7 +1183,7 @@ namespace Emby.Server.Implementations.Session
{
var controllingSession = GetSession(controllingSessionId);
AssertCanControl(session, controllingSession);
- if (!controllingSession.UserId.Equals(Guid.Empty))
+ if (!controllingSession.UserId.Equals(default))
{
command.ControllingUserId = controllingSession.UserId;
}
@@ -1199,16 +1193,18 @@ namespace Emby.Server.Implementations.Session
}
/// <inheritdoc />
- public async Task SendSyncPlayCommand(SessionInfo session, SendCommand command, CancellationToken cancellationToken)
+ public async Task SendSyncPlayCommand(string sessionId, SendCommand command, CancellationToken cancellationToken)
{
CheckDisposed();
+ var session = GetSession(sessionId);
await SendMessageToSession(session, SessionMessageType.SyncPlayCommand, command, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc />
- public async Task SendSyncPlayGroupUpdate<T>(SessionInfo session, GroupUpdate<T> command, CancellationToken cancellationToken)
+ public async Task SendSyncPlayGroupUpdate<T>(string sessionId, GroupUpdate<T> command, CancellationToken cancellationToken)
{
CheckDisposed();
+ var session = GetSession(sessionId);
await SendMessageToSession(session, SessionMessageType.SyncPlayGroupUpdate, command, cancellationToken).ConfigureAwait(false);
}
@@ -1216,9 +1212,9 @@ namespace Emby.Server.Implementations.Session
{
var item = _libraryManager.GetItemById(id);
- if (item == null)
+ if (item is null)
{
- _logger.LogError("A non-existant item Id {0} was passed into TranslateItemForPlayback", id);
+ _logger.LogError("A non-existent item Id {0} was passed into TranslateItemForPlayback", id);
return Array.Empty<BaseItem>();
}
@@ -1269,7 +1265,7 @@ namespace Emby.Server.Implementations.Session
{
var item = _libraryManager.GetItemById(id);
- if (item == null)
+ if (item is null)
{
_logger.LogError("A non-existent item Id {0} was passed into TranslateItemForInstantMix", id);
return new List<BaseItem>();
@@ -1288,7 +1284,7 @@ namespace Emby.Server.Implementations.Session
{
["ItemId"] = command.ItemId,
["ItemName"] = command.ItemName,
- ["ItemType"] = command.ItemType
+ ["ItemType"] = command.ItemType.ToString()
}
};
@@ -1306,7 +1302,7 @@ namespace Emby.Server.Implementations.Session
{
var controllingSession = GetSession(controllingSessionId);
AssertCanControl(session, controllingSession);
- if (!controllingSession.UserId.Equals(Guid.Empty))
+ if (!controllingSession.UserId.Equals(default))
{
command.ControllingUserId = controllingSession.UserId.ToString("N", CultureInfo.InvariantCulture);
}
@@ -1317,15 +1313,9 @@ namespace Emby.Server.Implementations.Session
private static void AssertCanControl(SessionInfo session, SessionInfo controllingSession)
{
- if (session == null)
- {
- throw new ArgumentNullException(nameof(session));
- }
+ ArgumentNullException.ThrowIfNull(session);
- if (controllingSession == null)
- {
- throw new ArgumentNullException(nameof(controllingSession));
- }
+ ArgumentNullException.ThrowIfNull(controllingSession);
}
/// <summary>
@@ -1379,12 +1369,12 @@ namespace Emby.Server.Implementations.Session
var session = GetSession(sessionId);
- if (session.UserId == userId)
+ if (session.UserId.Equals(userId))
{
throw new ArgumentException("The requested user is already the primary user of the session.");
}
- if (session.AdditionalUsers.All(i => i.UserId != userId))
+ if (session.AdditionalUsers.All(i => !i.UserId.Equals(userId)))
{
var user = _userManager.GetUserById(userId);
@@ -1420,7 +1410,7 @@ namespace Emby.Server.Implementations.Session
var user = session.AdditionalUsers.FirstOrDefault(i => i.UserId.Equals(userId));
- if (user != null)
+ if (user is not null)
{
var list = session.AdditionalUsers.ToList();
list.Remove(user);
@@ -1432,38 +1422,20 @@ namespace Emby.Server.Implementations.Session
/// <summary>
/// Authenticates the new session.
/// </summary>
- /// <param name="request">The request.</param>
- /// <returns>Task{SessionInfo}.</returns>
+ /// <param name="request">The authenticationrequest.</param>
+ /// <returns>The authentication result.</returns>
public Task<AuthenticationResult> AuthenticateNewSession(AuthenticationRequest request)
{
return AuthenticateNewSessionInternal(request, true);
}
- public Task<AuthenticationResult> CreateNewSession(AuthenticationRequest request)
- {
- return AuthenticateNewSessionInternal(request, false);
- }
-
- public Task<AuthenticationResult> AuthenticateQuickConnect(AuthenticationRequest request, string token)
+ /// <summary>
+ /// Directly authenticates the session without enforcing password.
+ /// </summary>
+ /// <param name="request">The authentication request.</param>
+ /// <returns>The authentication result.</returns>
+ public Task<AuthenticationResult> AuthenticateDirect(AuthenticationRequest request)
{
- var result = _authRepo.Get(new AuthenticationInfoQuery()
- {
- AccessToken = token,
- DeviceId = _appHost.SystemId,
- Limit = 1
- });
-
- if (result.TotalRecordCount == 0)
- {
- throw new SecurityException("Unknown quick connect token");
- }
-
- var info = result.Items[0];
- request.UserId = info.UserId;
-
- // There's no need to keep the quick connect token in the database, as AuthenticateNewSessionInternal() issues a long lived token.
- _authRepo.Delete(info);
-
return AuthenticateNewSessionInternal(request, false);
}
@@ -1472,7 +1444,7 @@ namespace Emby.Server.Implementations.Session
CheckDisposed();
User user = null;
- if (request.UserId != Guid.Empty)
+ if (!request.UserId.Equals(default))
{
user = _userManager.GetUserById(request.UserId);
}
@@ -1489,9 +1461,9 @@ namespace Emby.Server.Implementations.Session
true).ConfigureAwait(false);
}
- if (user == null)
+ if (user is null)
{
- AuthenticationFailed?.Invoke(this, new GenericEventArgs<AuthenticationRequest>(request));
+ await _eventManager.PublishAsync(new AuthenticationRequestEventArgs(request)).ConfigureAwait(false);
throw new AuthenticationException("Invalid username or password entered.");
}
@@ -1509,15 +1481,15 @@ namespace Emby.Server.Implementations.Session
throw new SecurityException("User is at their maximum number of sessions.");
}
- var token = GetAuthorizationToken(user, request.DeviceId, request.App, request.AppVersion, request.DeviceName);
+ var token = await GetAuthorizationToken(user, request.DeviceId, request.App, request.AppVersion, request.DeviceName).ConfigureAwait(false);
- var session = LogSessionActivity(
+ var session = await LogSessionActivity(
request.App,
request.AppVersion,
request.DeviceId,
request.DeviceName,
request.RemoteEndPoint,
- user);
+ user).ConfigureAwait(false);
var returnResult = new AuthenticationResult
{
@@ -1527,106 +1499,69 @@ namespace Emby.Server.Implementations.Session
ServerId = _appHost.SystemId
};
- AuthenticationSucceeded?.Invoke(this, new GenericEventArgs<AuthenticationResult>(returnResult));
-
+ await _eventManager.PublishAsync(new AuthenticationResultEventArgs(returnResult)).ConfigureAwait(false);
return returnResult;
}
- private string GetAuthorizationToken(User user, string deviceId, string app, string appVersion, string deviceName)
+ private async Task<string> GetAuthorizationToken(User user, string deviceId, string app, string appVersion, string deviceName)
{
- var existing = _authRepo.Get(
- new AuthenticationInfoQuery
+ var existing = (await _deviceManager.GetDevices(
+ new DeviceQuery
{
DeviceId = deviceId,
- UserId = user.Id,
- Limit = 1
- }).Items.FirstOrDefault();
+ UserId = user.Id
+ }).ConfigureAwait(false)).Items;
- if (!string.IsNullOrEmpty(deviceId))
+ foreach (var auth in existing)
{
- var allExistingForDevice = _authRepo.Get(
- new AuthenticationInfoQuery
- {
- DeviceId = deviceId
- }).Items;
-
- foreach (var auth in allExistingForDevice)
+ try
{
- if (existing == null || !string.Equals(auth.AccessToken, existing.AccessToken, StringComparison.Ordinal))
- {
- try
- {
- Logout(auth);
- }
- catch (Exception ex)
- {
- _logger.LogError(ex, "Error while logging out.");
- }
- }
+ // Logout any existing sessions for the user on this device
+ await Logout(auth).ConfigureAwait(false);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Error while logging out existing session.");
}
}
- if (existing != null)
- {
- _logger.LogInformation("Reissuing access token: {Token}", existing.AccessToken);
- return existing.AccessToken;
- }
-
- var now = DateTime.UtcNow;
-
- var newToken = new AuthenticationInfo
- {
- AppName = app,
- AppVersion = appVersion,
- DateCreated = now,
- DateLastActivity = now,
- DeviceId = deviceId,
- DeviceName = deviceName,
- UserId = user.Id,
- AccessToken = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture),
- UserName = user.Username
- };
-
_logger.LogInformation("Creating new access token for user {0}", user.Id);
- _authRepo.Create(newToken);
+ var device = await _deviceManager.CreateDevice(new Device(user.Id, app, appVersion, deviceName, deviceId)).ConfigureAwait(false);
- return newToken.AccessToken;
+ return device.AccessToken;
}
/// <inheritdoc />
- public void Logout(string accessToken)
+ public async Task Logout(string accessToken)
{
CheckDisposed();
- if (string.IsNullOrEmpty(accessToken))
- {
- throw new ArgumentNullException(nameof(accessToken));
- }
+ ArgumentException.ThrowIfNullOrEmpty(accessToken);
- var existing = _authRepo.Get(
- new AuthenticationInfoQuery
+ var existing = (await _deviceManager.GetDevices(
+ new DeviceQuery
{
Limit = 1,
AccessToken = accessToken
- }).Items;
+ }).ConfigureAwait(false)).Items;
if (existing.Count > 0)
{
- Logout(existing[0]);
+ await Logout(existing[0]).ConfigureAwait(false);
}
}
/// <inheritdoc />
- public void Logout(AuthenticationInfo existing)
+ public async Task Logout(Device device)
{
CheckDisposed();
- _logger.LogInformation("Logging out access token {0}", existing.AccessToken);
+ _logger.LogInformation("Logging out access token {0}", device.AccessToken);
- _authRepo.Delete(existing);
+ await _deviceManager.DeleteDevice(device).ConfigureAwait(false);
var sessions = Sessions
- .Where(i => string.Equals(i.DeviceId, existing.DeviceId, StringComparison.OrdinalIgnoreCase))
+ .Where(i => string.Equals(i.DeviceId, device.DeviceId, StringComparison.OrdinalIgnoreCase))
.ToList();
foreach (var session in sessions)
@@ -1637,36 +1572,30 @@ namespace Emby.Server.Implementations.Session
}
catch (Exception ex)
{
- _logger.LogError("Error reporting session ended", ex);
+ _logger.LogError(ex, "Error reporting session ended");
}
}
}
/// <inheritdoc />
- public void RevokeUserTokens(Guid userId, string currentAccessToken)
+ public async Task RevokeUserTokens(Guid userId, string currentAccessToken)
{
CheckDisposed();
- var existing = _authRepo.Get(new AuthenticationInfoQuery
+ var existing = await _deviceManager.GetDevices(new DeviceQuery
{
UserId = userId
- });
+ }).ConfigureAwait(false);
foreach (var info in existing.Items)
{
if (!string.Equals(currentAccessToken, info.AccessToken, StringComparison.OrdinalIgnoreCase))
{
- Logout(info);
+ await Logout(info).ConfigureAwait(false);
}
}
}
- /// <inheritdoc />
- public void RevokeToken(string token)
- {
- Logout(token);
- }
-
/// <summary>
/// Reports the capabilities.
/// </summary>
@@ -1706,14 +1635,11 @@ namespace Emby.Server.Implementations.Session
/// </summary>
private BaseItemDto GetItemInfo(BaseItem item, MediaSourceInfo mediaSource)
{
- if (item == null)
- {
- throw new ArgumentNullException(nameof(item));
- }
+ ArgumentNullException.ThrowIfNull(item);
var dtoOptions = _itemInfoDtoOptions;
- if (_itemInfoDtoOptions == null)
+ if (_itemInfoDtoOptions is null)
{
dtoOptions = new DtoOptions
{
@@ -1756,7 +1682,7 @@ namespace Emby.Server.Implementations.Session
var info = _dtoService.GetBaseItemDto(item, dtoOptions);
- if (mediaSource != null)
+ if (mediaSource is not null)
{
info.MediaStreams = mediaSource.MediaStreams.ToArray();
}
@@ -1780,24 +1706,12 @@ namespace Emby.Server.Implementations.Session
/// <inheritdoc />
public void ReportNowViewingItem(string sessionId, string itemId)
{
- if (string.IsNullOrEmpty(itemId))
- {
- throw new ArgumentNullException(nameof(itemId));
- }
+ ArgumentException.ThrowIfNullOrEmpty(itemId);
var item = _libraryManager.GetItemById(new Guid(itemId));
-
- var info = GetItemInfo(item, null);
-
- ReportNowViewingItem(sessionId, info);
- }
-
- /// <inheritdoc />
- public void ReportNowViewingItem(string sessionId, BaseItemDto item)
- {
var session = GetSession(sessionId);
- session.NowViewingItem = item;
+ session.NowViewingItem = GetItemInfo(item, null);
}
/// <inheritdoc />
@@ -1806,7 +1720,7 @@ namespace Emby.Server.Implementations.Session
var session = Sessions.FirstOrDefault(i =>
string.Equals(i.DeviceId, deviceId, StringComparison.OrdinalIgnoreCase));
- if (session != null)
+ if (session is not null)
{
session.TranscodingInfo = info;
}
@@ -1827,14 +1741,11 @@ namespace Emby.Server.Implementations.Session
}
/// <inheritdoc />
- public SessionInfo GetSessionByAuthenticationToken(AuthenticationInfo info, string deviceId, string remoteEndpoint, string appVersion)
+ public Task<SessionInfo> GetSessionByAuthenticationToken(Device info, string deviceId, string remoteEndpoint, string appVersion)
{
- if (info == null)
- {
- throw new ArgumentNullException(nameof(info));
- }
+ ArgumentNullException.ThrowIfNull(info);
- var user = info.UserId == Guid.Empty
+ var user = info.UserId.Equals(default)
? null
: _userManager.GetUserById(info.UserId);
@@ -1860,20 +1771,20 @@ namespace Emby.Server.Implementations.Session
}
/// <inheritdoc />
- public SessionInfo GetSessionByAuthenticationToken(string token, string deviceId, string remoteEndpoint)
+ public async Task<SessionInfo> GetSessionByAuthenticationToken(string token, string deviceId, string remoteEndpoint)
{
- var items = _authRepo.Get(new AuthenticationInfoQuery
+ var items = (await _deviceManager.GetDevices(new DeviceQuery
{
AccessToken = token,
Limit = 1
- }).Items;
+ }).ConfigureAwait(false)).Items;
if (items.Count == 0)
{
return null;
}
- return GetSessionByAuthenticationToken(items[0], deviceId, remoteEndpoint, null);
+ return await GetSessionByAuthenticationToken(items[0], deviceId, remoteEndpoint, null).ConfigureAwait(false);
}
/// <inheritdoc />