< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.DevicesController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/DevicesController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 142
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetDevices(...)100%210%
GetDeviceInfo(...)0%620%
GetDeviceOptions(...)0%620%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/DevicesController.cs

#LineLine coverage
 1using System;
 2using System.ComponentModel.DataAnnotations;
 3using System.Threading.Tasks;
 4using Jellyfin.Api.Constants;
 5using Jellyfin.Api.Helpers;
 6using Jellyfin.Data.Dtos;
 7using Jellyfin.Data.Entities.Security;
 8using Jellyfin.Data.Queries;
 9using MediaBrowser.Common.Api;
 10using MediaBrowser.Controller.Devices;
 11using MediaBrowser.Controller.Session;
 12using MediaBrowser.Model.Devices;
 13using MediaBrowser.Model.Querying;
 14using Microsoft.AspNetCore.Authorization;
 15using Microsoft.AspNetCore.Http;
 16using Microsoft.AspNetCore.Mvc;
 17
 18namespace Jellyfin.Api.Controllers;
 19
 20/// <summary>
 21/// Devices Controller.
 22/// </summary>
 23[Authorize(Policy = Policies.RequiresElevation)]
 24public class DevicesController : BaseJellyfinApiController
 25{
 26    private readonly IDeviceManager _deviceManager;
 27    private readonly ISessionManager _sessionManager;
 28
 29    /// <summary>
 30    /// Initializes a new instance of the <see cref="DevicesController"/> class.
 31    /// </summary>
 32    /// <param name="deviceManager">Instance of <see cref="IDeviceManager"/> interface.</param>
 33    /// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
 034    public DevicesController(
 035        IDeviceManager deviceManager,
 036        ISessionManager sessionManager)
 37    {
 038        _deviceManager = deviceManager;
 039        _sessionManager = sessionManager;
 040    }
 41
 42    /// <summary>
 43    /// Get Devices.
 44    /// </summary>
 45    /// <param name="userId">Gets or sets the user identifier.</param>
 46    /// <response code="200">Devices retrieved.</response>
 47    /// <returns>An <see cref="OkResult"/> containing the list of devices.</returns>
 48    [HttpGet]
 49    [ProducesResponseType(StatusCodes.Status200OK)]
 50    public ActionResult<QueryResult<DeviceInfo>> GetDevices([FromQuery] Guid? userId)
 51    {
 052        userId = RequestHelpers.GetUserId(User, userId);
 053        return _deviceManager.GetDevicesForUser(userId);
 54    }
 55
 56    /// <summary>
 57    /// Get info for a device.
 58    /// </summary>
 59    /// <param name="id">Device Id.</param>
 60    /// <response code="200">Device info retrieved.</response>
 61    /// <response code="404">Device not found.</response>
 62    /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if t
 63    [HttpGet("Info")]
 64    [ProducesResponseType(StatusCodes.Status200OK)]
 65    [ProducesResponseType(StatusCodes.Status404NotFound)]
 66    public ActionResult<DeviceInfo> GetDeviceInfo([FromQuery, Required] string id)
 67    {
 068        var deviceInfo = _deviceManager.GetDevice(id);
 069        if (deviceInfo is null)
 70        {
 071            return NotFound();
 72        }
 73
 074        return deviceInfo;
 75    }
 76
 77    /// <summary>
 78    /// Get options for a device.
 79    /// </summary>
 80    /// <param name="id">Device Id.</param>
 81    /// <response code="200">Device options retrieved.</response>
 82    /// <response code="404">Device not found.</response>
 83    /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if t
 84    [HttpGet("Options")]
 85    [ProducesResponseType(StatusCodes.Status200OK)]
 86    [ProducesResponseType(StatusCodes.Status404NotFound)]
 87    public ActionResult<DeviceOptions> GetDeviceOptions([FromQuery, Required] string id)
 88    {
 089        var deviceInfo = _deviceManager.GetDeviceOptions(id);
 090        if (deviceInfo is null)
 91        {
 092            return NotFound();
 93        }
 94
 095        return deviceInfo;
 96    }
 97
 98    /// <summary>
 99    /// Update device options.
 100    /// </summary>
 101    /// <param name="id">Device Id.</param>
 102    /// <param name="deviceOptions">Device Options.</param>
 103    /// <response code="204">Device options updated.</response>
 104    /// <returns>A <see cref="NoContentResult"/>.</returns>
 105    [HttpPost("Options")]
 106    [ProducesResponseType(StatusCodes.Status204NoContent)]
 107    public async Task<ActionResult> UpdateDeviceOptions(
 108        [FromQuery, Required] string id,
 109        [FromBody, Required] DeviceOptionsDto deviceOptions)
 110    {
 111        await _deviceManager.UpdateDeviceOptions(id, deviceOptions.CustomName).ConfigureAwait(false);
 112        return NoContent();
 113    }
 114
 115    /// <summary>
 116    /// Deletes a device.
 117    /// </summary>
 118    /// <param name="id">Device Id.</param>
 119    /// <response code="204">Device deleted.</response>
 120    /// <response code="404">Device not found.</response>
 121    /// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not b
 122    [HttpDelete]
 123    [ProducesResponseType(StatusCodes.Status204NoContent)]
 124    [ProducesResponseType(StatusCodes.Status404NotFound)]
 125    public async Task<ActionResult> DeleteDevice([FromQuery, Required] string id)
 126    {
 127        var existingDevice = _deviceManager.GetDevice(id);
 128        if (existingDevice is null)
 129        {
 130            return NotFound();
 131        }
 132
 133        var sessions = _deviceManager.GetDevices(new DeviceQuery { DeviceId = id });
 134
 135        foreach (var session in sessions.Items)
 136        {
 137            await _sessionManager.Logout(session).ConfigureAwait(false);
 138        }
 139
 140        return NoContent();
 141    }
 142}