jellyfin/Jellyfin.Api/Controllers/SessionController.cs

501 lines
20 KiB
C#
Raw Normal View History

2020-06-12 12:54:25 -04:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
2021-04-10 16:57:25 -04:00
using System.Threading.Tasks;
2020-06-22 09:44:11 -04:00
using Jellyfin.Api.Constants;
using Jellyfin.Api.Extensions;
2020-06-12 12:54:25 -04:00
using Jellyfin.Api.Helpers;
using Jellyfin.Api.ModelBinders;
using Jellyfin.Api.Models.SessionDtos;
using Jellyfin.Data.Enums;
using Jellyfin.Extensions;
using MediaBrowser.Common.Api;
2020-06-12 12:54:25 -04:00
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Session;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2023-01-31 06:18:10 -05:00
namespace Jellyfin.Api.Controllers;
/// <summary>
/// The session controller.
/// </summary>
[Route("")]
public class SessionController : BaseJellyfinApiController
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
private readonly ISessionManager _sessionManager;
private readonly IUserManager _userManager;
private readonly IDeviceManager _deviceManager;
/// <summary>
/// Initializes a new instance of the <see cref="SessionController"/> class.
/// </summary>
/// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
/// <param name="userManager">Instance of <see cref="IUserManager"/> interface.</param>
/// <param name="deviceManager">Instance of <see cref="IDeviceManager"/> interface.</param>
public SessionController(
ISessionManager sessionManager,
IUserManager userManager,
IDeviceManager deviceManager)
{
_sessionManager = sessionManager;
_userManager = userManager;
_deviceManager = deviceManager;
}
2020-06-12 12:54:25 -04:00
/// <summary>
2023-01-31 06:18:10 -05:00
/// Gets a list of sessions.
2020-06-12 12:54:25 -04:00
/// </summary>
2023-01-31 06:18:10 -05:00
/// <param name="controllableByUserId">Filter by sessions that a given user is allowed to remote control.</param>
/// <param name="deviceId">Filter by device Id.</param>
/// <param name="activeWithinSeconds">Optional. Filter by sessions that were active in the last n seconds.</param>
/// <response code="200">List of sessions returned.</response>
/// <returns>An <see cref="IEnumerable{SessionInfo}"/> with the available sessions.</returns>
[HttpGet("Sessions")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<IEnumerable<SessionInfo>> GetSessions(
[FromQuery] Guid? controllableByUserId,
[FromQuery] string? deviceId,
[FromQuery] int? activeWithinSeconds)
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
var result = _sessionManager.Sessions;
if (!string.IsNullOrEmpty(deviceId))
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
result = result.Where(i => string.Equals(i.DeviceId, deviceId, StringComparison.OrdinalIgnoreCase));
2020-06-12 12:54:25 -04:00
}
if (!controllableByUserId.IsNullOrEmpty())
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
result = result.Where(i => i.SupportsRemoteControl);
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
var user = _userManager.GetUserById(controllableByUserId.Value);
if (user is null)
{
return NotFound();
}
2023-01-31 06:18:10 -05:00
if (!user.HasPermission(PermissionKind.EnableRemoteControlOfOtherUsers))
2020-06-12 12:54:25 -04:00
{
result = result.Where(i => i.UserId.IsEmpty() || i.ContainsUser(controllableByUserId.Value));
2020-06-12 12:54:25 -04:00
}
2023-01-31 06:18:10 -05:00
if (!user.HasPermission(PermissionKind.EnableSharedDeviceControl))
2020-06-12 12:54:25 -04:00
{
result = result.Where(i => !i.UserId.IsEmpty());
2023-01-31 06:18:10 -05:00
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
result = result.Where(i =>
{
if (!string.IsNullOrWhiteSpace(i.DeviceId))
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
if (!_deviceManager.CanAccessDevice(user, i.DeviceId))
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
return false;
2020-06-12 12:54:25 -04:00
}
2023-01-31 06:18:10 -05:00
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
return true;
});
2020-06-12 12:54:25 -04:00
}
if (activeWithinSeconds.HasValue && activeWithinSeconds.Value > 0)
{
var minActiveDate = DateTime.UtcNow.AddSeconds(0 - activeWithinSeconds.Value);
result = result.Where(i => i.LastActivityDate >= minActiveDate);
}
2023-01-31 06:18:10 -05:00
return Ok(result);
}
/// <summary>
/// Instructs a session to browse to an item or view.
/// </summary>
/// <param name="sessionId">The session Id.</param>
/// <param name="itemType">The type of item to browse to.</param>
/// <param name="itemId">The Id of the item.</param>
/// <param name="itemName">The name of the item.</param>
/// <response code="204">Instruction sent to session.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/{sessionId}/Viewing")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> DisplayContent(
[FromRoute, Required] string sessionId,
[FromQuery, Required] BaseItemKind itemType,
[FromQuery, Required] string itemId,
[FromQuery, Required] string itemName)
{
var command = new BrowseRequest
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
ItemId = itemId,
ItemName = itemName,
ItemType = itemType
};
await _sessionManager.SendBrowseCommand(
await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false),
sessionId,
command,
CancellationToken.None)
.ConfigureAwait(false);
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Instructs a session to play an item.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="playCommand">The type of play command to issue (PlayNow, PlayNext, PlayLast). Clients who have not yet implemented play next and play last may play now.</param>
/// <param name="itemIds">The ids of the items to play, comma delimited.</param>
/// <param name="startPositionTicks">The starting position of the first item.</param>
/// <param name="mediaSourceId">Optional. The media source id.</param>
/// <param name="audioStreamIndex">Optional. The index of the audio stream to play.</param>
/// <param name="subtitleStreamIndex">Optional. The index of the subtitle stream to play.</param>
/// <param name="startIndex">Optional. The start index.</param>
/// <response code="204">Instruction sent to session.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/{sessionId}/Playing")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> Play(
[FromRoute, Required] string sessionId,
[FromQuery, Required] PlayCommand playCommand,
[FromQuery, Required, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] itemIds,
[FromQuery] long? startPositionTicks,
[FromQuery] string? mediaSourceId,
[FromQuery] int? audioStreamIndex,
[FromQuery] int? subtitleStreamIndex,
[FromQuery] int? startIndex)
{
var playRequest = new PlayRequest
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
ItemIds = itemIds,
StartPositionTicks = startPositionTicks,
PlayCommand = playCommand,
MediaSourceId = mediaSourceId,
AudioStreamIndex = audioStreamIndex,
SubtitleStreamIndex = subtitleStreamIndex,
StartIndex = startIndex
};
await _sessionManager.SendPlayCommand(
await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false),
sessionId,
playRequest,
CancellationToken.None)
.ConfigureAwait(false);
return NoContent();
}
/// <summary>
/// Issues a playstate command to a client.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="command">The <see cref="PlaystateCommand"/>.</param>
/// <param name="seekPositionTicks">The optional position ticks.</param>
/// <param name="controllingUserId">The optional controlling user id.</param>
/// <response code="204">Playstate command sent to session.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/{sessionId}/Playing/{command}")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> SendPlaystateCommand(
[FromRoute, Required] string sessionId,
[FromRoute, Required] PlaystateCommand command,
[FromQuery] long? seekPositionTicks,
[FromQuery] string? controllingUserId)
{
await _sessionManager.SendPlaystateCommand(
await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false),
sessionId,
new PlaystateRequest()
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
Command = command,
ControllingUserId = controllingUserId,
SeekPositionTicks = seekPositionTicks,
},
CancellationToken.None)
.ConfigureAwait(false);
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Issues a system command to a client.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="command">The command to send.</param>
/// <response code="204">System command sent to session.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/{sessionId}/System/{command}")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> SendSystemCommand(
[FromRoute, Required] string sessionId,
[FromRoute, Required] GeneralCommandType command)
{
var currentSession = await RequestHelpers.GetSession(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
var generalCommand = new GeneralCommand
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
Name = command,
ControllingUserId = currentSession.UserId
};
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
await _sessionManager.SendGeneralCommand(currentSession.Id, sessionId, generalCommand, CancellationToken.None).ConfigureAwait(false);
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Issues a general command to a client.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="command">The command to send.</param>
/// <response code="204">General command sent to session.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/{sessionId}/Command/{command}")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> SendGeneralCommand(
[FromRoute, Required] string sessionId,
[FromRoute, Required] GeneralCommandType command)
{
var currentSession = await RequestHelpers.GetSession(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
var generalCommand = new GeneralCommand
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
Name = command,
ControllingUserId = currentSession.UserId
};
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
await _sessionManager.SendGeneralCommand(currentSession.Id, sessionId, generalCommand, CancellationToken.None)
.ConfigureAwait(false);
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Issues a full general command to a client.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="command">The <see cref="GeneralCommand"/>.</param>
/// <response code="204">Full general command sent to session.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/{sessionId}/Command")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> SendFullGeneralCommand(
[FromRoute, Required] string sessionId,
[FromBody, Required] GeneralCommand command)
{
var currentSession = await RequestHelpers.GetSession(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
ArgumentNullException.ThrowIfNull(command);
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
command.ControllingUserId = currentSession.UserId;
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
await _sessionManager.SendGeneralCommand(
currentSession.Id,
sessionId,
command,
CancellationToken.None)
.ConfigureAwait(false);
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Issues a command to a client to display a message to the user.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="command">The <see cref="MessageCommand" /> object containing Header, Message Text, and TimeoutMs.</param>
/// <response code="204">Message sent.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/{sessionId}/Message")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> SendMessageCommand(
[FromRoute, Required] string sessionId,
[FromBody, Required] MessageCommand command)
{
if (string.IsNullOrWhiteSpace(command.Header))
{
command.Header = "Message from Server";
2020-06-12 12:54:25 -04:00
}
2023-01-31 06:18:10 -05:00
await _sessionManager.SendMessageCommand(
await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false),
sessionId,
command,
CancellationToken.None)
.ConfigureAwait(false);
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Adds an additional user to a session.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="userId">The user id.</param>
/// <response code="204">User added to session.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/{sessionId}/User/{userId}")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult AddUserToSession(
[FromRoute, Required] string sessionId,
[FromRoute, Required] Guid userId)
{
_sessionManager.AddAdditionalUser(sessionId, userId);
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Removes an additional user from a session.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="userId">The user id.</param>
/// <response code="204">User removed from session.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpDelete("Sessions/{sessionId}/User/{userId}")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult RemoveUserFromSession(
[FromRoute, Required] string sessionId,
[FromRoute, Required] Guid userId)
{
_sessionManager.RemoveAdditionalUser(sessionId, userId);
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Updates capabilities for a device.
/// </summary>
/// <param name="id">The session id.</param>
/// <param name="playableMediaTypes">A list of playable media types, comma delimited. Audio, Video, Book, Photo.</param>
/// <param name="supportedCommands">A list of supported remote control commands, comma delimited.</param>
/// <param name="supportsMediaControl">Determines whether media can be played remotely..</param>
/// <param name="supportsPersistentIdentifier">Determines whether the device supports a unique identifier.</param>
/// <response code="204">Capabilities posted.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/Capabilities")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> PostCapabilities(
[FromQuery] string? id,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] MediaType[] playableMediaTypes,
2023-01-31 06:18:10 -05:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] GeneralCommandType[] supportedCommands,
[FromQuery] bool supportsMediaControl = false,
[FromQuery] bool supportsPersistentIdentifier = true)
{
if (string.IsNullOrWhiteSpace(id))
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
id = await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
2020-06-12 12:54:25 -04:00
}
2023-01-31 06:18:10 -05:00
_sessionManager.ReportCapabilities(id, new ClientCapabilities
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
PlayableMediaTypes = playableMediaTypes,
SupportedCommands = supportedCommands,
SupportsMediaControl = supportsMediaControl,
SupportsPersistentIdentifier = supportsPersistentIdentifier
});
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Updates capabilities for a device.
/// </summary>
/// <param name="id">The session id.</param>
/// <param name="capabilities">The <see cref="ClientCapabilities"/>.</param>
/// <response code="204">Capabilities updated.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/Capabilities/Full")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> PostFullCapabilities(
[FromQuery] string? id,
[FromBody, Required] ClientCapabilitiesDto capabilities)
{
if (string.IsNullOrWhiteSpace(id))
2020-06-12 12:54:25 -04:00
{
2023-01-31 06:18:10 -05:00
id = await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
_sessionManager.ReportCapabilities(id, capabilities.ToClientCapabilities());
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Reports that a session is viewing an item.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="itemId">The item id.</param>
/// <response code="204">Session reported to server.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/Viewing")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> ReportViewing(
[FromQuery] string? sessionId,
[FromQuery, Required] string? itemId)
{
string session = sessionId ?? await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
_sessionManager.ReportNowViewingItem(session, itemId);
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Reports that a session has ended.
/// </summary>
/// <response code="204">Session end reported to server.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Sessions/Logout")]
2023-02-08 17:55:26 -05:00
[Authorize]
2023-01-31 06:18:10 -05:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> ReportSessionEnded()
{
await _sessionManager.Logout(User.GetToken()).ConfigureAwait(false);
return NoContent();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Get all auth providers.
/// </summary>
/// <response code="200">Auth providers retrieved.</response>
/// <returns>An <see cref="IEnumerable{NameIdPair}"/> with the auth providers.</returns>
[HttpGet("Auth/Providers")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<IEnumerable<NameIdPair>> GetAuthProviders()
{
return _userManager.GetAuthenticationProviders();
}
2020-06-12 12:54:25 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Get all password reset providers.
/// </summary>
/// <response code="200">Password reset providers retrieved.</response>
/// <returns>An <see cref="IEnumerable{NameIdPair}"/> with the password reset providers.</returns>
[HttpGet("Auth/PasswordResetProviders")]
[ProducesResponseType(StatusCodes.Status200OK)]
[Authorize(Policy = Policies.RequiresElevation)]
public ActionResult<IEnumerable<NameIdPair>> GetPasswordResetProviders()
{
return _userManager.GetPasswordResetProviders();
2020-06-12 12:54:25 -04:00
}
}