2020-04-20 01:33:55 +00:00
using System ;
2020-08-06 14:17:45 +00:00
using System.ComponentModel.DataAnnotations ;
2021-04-10 20:17:36 +00:00
using System.Threading.Tasks ;
2020-05-19 16:05:23 +00:00
using Jellyfin.Api.Constants ;
2021-07-13 23:30:11 +00:00
using Jellyfin.Data.Dtos ;
2021-04-10 20:17:36 +00:00
using Jellyfin.Data.Entities.Security ;
2021-05-21 03:56:59 +00:00
using Jellyfin.Data.Queries ;
2020-04-20 01:33:55 +00:00
using MediaBrowser.Controller.Devices ;
using MediaBrowser.Controller.Session ;
using MediaBrowser.Model.Devices ;
2020-05-19 16:06:25 +00:00
using MediaBrowser.Model.Querying ;
2020-05-19 16:05:23 +00:00
using Microsoft.AspNetCore.Authorization ;
2020-04-20 01:33:55 +00:00
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.Controllers ;
/// <summary>
/// Devices Controller.
/// </summary>
[Authorize(Policy = Policies.RequiresElevation)]
public class DevicesController : BaseJellyfinApiController
2020-04-20 01:33:55 +00:00
{
2023-01-31 11:18:10 +00:00
private readonly IDeviceManager _deviceManager ;
private readonly ISessionManager _sessionManager ;
2020-04-20 01:33:55 +00:00
/// <summary>
2023-01-31 11:18:10 +00:00
/// Initializes a new instance of the <see cref="DevicesController"/> class.
2020-04-20 01:33:55 +00:00
/// </summary>
2023-01-31 11:18:10 +00:00
/// <param name="deviceManager">Instance of <see cref="IDeviceManager"/> interface.</param>
/// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
public DevicesController (
IDeviceManager deviceManager ,
ISessionManager sessionManager )
2020-04-20 01:33:55 +00:00
{
2023-01-31 11:18:10 +00:00
_deviceManager = deviceManager ;
_sessionManager = sessionManager ;
}
2020-04-20 01:33:55 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Get Devices.
/// </summary>
/// <param name="supportsSync">Gets or sets a value indicating whether [supports synchronize].</param>
/// <param name="userId">Gets or sets the user identifier.</param>
/// <response code="200">Devices retrieved.</response>
/// <returns>An <see cref="OkResult"/> containing the list of devices.</returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task < ActionResult < QueryResult < DeviceInfo > > > GetDevices ( [ FromQuery ] bool? supportsSync , [ FromQuery ] Guid ? userId )
{
return await _deviceManager . GetDevicesForUser ( userId , supportsSync ) . ConfigureAwait ( false ) ;
}
2020-04-20 01:33:55 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Get info for a device.
/// </summary>
/// <param name="id">Device Id.</param>
/// <response code="200">Device info retrieved.</response>
/// <response code="404">Device not found.</response>
/// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
[HttpGet("Info")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task < ActionResult < DeviceInfo > > GetDeviceInfo ( [ FromQuery , Required ] string id )
{
var deviceInfo = await _deviceManager . GetDevice ( id ) . ConfigureAwait ( false ) ;
if ( deviceInfo is null )
2020-04-20 01:33:55 +00:00
{
2023-01-31 11:18:10 +00:00
return NotFound ( ) ;
2020-04-20 01:33:55 +00:00
}
2023-01-31 11:18:10 +00:00
return deviceInfo ;
}
2020-04-21 13:55:01 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Get options for a device.
/// </summary>
/// <param name="id">Device Id.</param>
/// <response code="200">Device options retrieved.</response>
/// <response code="404">Device not found.</response>
/// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
[HttpGet("Options")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task < ActionResult < DeviceOptions > > GetDeviceOptions ( [ FromQuery , Required ] string id )
{
var deviceInfo = await _deviceManager . GetDeviceOptions ( id ) . ConfigureAwait ( false ) ;
if ( deviceInfo is null )
{
return NotFound ( ) ;
2020-04-20 01:33:55 +00:00
}
2023-01-31 11:18:10 +00:00
return deviceInfo ;
}
2020-04-21 13:55:01 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Update device options.
/// </summary>
/// <param name="id">Device Id.</param>
/// <param name="deviceOptions">Device Options.</param>
/// <response code="204">Device options updated.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Options")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task < ActionResult > UpdateDeviceOptions (
[FromQuery, Required] string id ,
[FromBody, Required] DeviceOptionsDto deviceOptions )
{
await _deviceManager . UpdateDeviceOptions ( id , deviceOptions . CustomName ) . ConfigureAwait ( false ) ;
return NoContent ( ) ;
}
2020-04-20 01:33:55 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Deletes a device.
/// </summary>
/// <param name="id">Device Id.</param>
/// <response code="204">Device deleted.</response>
/// <response code="404">Device not found.</response>
/// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
[HttpDelete]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task < ActionResult > DeleteDevice ( [ FromQuery , Required ] string id )
{
var existingDevice = await _deviceManager . GetDevice ( id ) . ConfigureAwait ( false ) ;
if ( existingDevice is null )
2020-04-20 01:33:55 +00:00
{
2023-01-31 11:18:10 +00:00
return NotFound ( ) ;
2020-04-20 01:33:55 +00:00
}
2023-01-31 11:18:10 +00:00
var sessions = await _deviceManager . GetDevices ( new DeviceQuery { DeviceId = id } ) . ConfigureAwait ( false ) ;
2020-04-21 13:55:01 +00:00
2023-01-31 11:18:10 +00:00
foreach ( var session in sessions . Items )
{
await _sessionManager . Logout ( session ) . ConfigureAwait ( false ) ;
2020-04-20 01:33:55 +00:00
}
2023-01-31 11:18:10 +00:00
return NoContent ( ) ;
2020-04-20 01:33:55 +00:00
}
}