< 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: 140
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.Helpers;
 5using Jellyfin.Data.Dtos;
 6using Jellyfin.Data.Queries;
 7using MediaBrowser.Common.Api;
 8using MediaBrowser.Controller.Devices;
 9using MediaBrowser.Controller.Session;
 10using MediaBrowser.Model.Dto;
 11using MediaBrowser.Model.Querying;
 12using Microsoft.AspNetCore.Authorization;
 13using Microsoft.AspNetCore.Http;
 14using Microsoft.AspNetCore.Mvc;
 15
 16namespace Jellyfin.Api.Controllers;
 17
 18/// <summary>
 19/// Devices Controller.
 20/// </summary>
 21[Authorize(Policy = Policies.RequiresElevation)]
 22public class DevicesController : BaseJellyfinApiController
 23{
 24    private readonly IDeviceManager _deviceManager;
 25    private readonly ISessionManager _sessionManager;
 26
 27    /// <summary>
 28    /// Initializes a new instance of the <see cref="DevicesController"/> class.
 29    /// </summary>
 30    /// <param name="deviceManager">Instance of <see cref="IDeviceManager"/> interface.</param>
 31    /// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
 032    public DevicesController(
 033        IDeviceManager deviceManager,
 034        ISessionManager sessionManager)
 35    {
 036        _deviceManager = deviceManager;
 037        _sessionManager = sessionManager;
 038    }
 39
 40    /// <summary>
 41    /// Get Devices.
 42    /// </summary>
 43    /// <param name="userId">Gets or sets the user identifier.</param>
 44    /// <response code="200">Devices retrieved.</response>
 45    /// <returns>An <see cref="OkResult"/> containing the list of devices.</returns>
 46    [HttpGet]
 47    [ProducesResponseType(StatusCodes.Status200OK)]
 48    public ActionResult<QueryResult<DeviceInfoDto>> GetDevices([FromQuery] Guid? userId)
 49    {
 050        userId = RequestHelpers.GetUserId(User, userId);
 051        return _deviceManager.GetDevicesForUser(userId);
 52    }
 53
 54    /// <summary>
 55    /// Get info for a device.
 56    /// </summary>
 57    /// <param name="id">Device Id.</param>
 58    /// <response code="200">Device info retrieved.</response>
 59    /// <response code="404">Device not found.</response>
 60    /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if t
 61    [HttpGet("Info")]
 62    [ProducesResponseType(StatusCodes.Status200OK)]
 63    [ProducesResponseType(StatusCodes.Status404NotFound)]
 64    public ActionResult<DeviceInfoDto> GetDeviceInfo([FromQuery, Required] string id)
 65    {
 066        var deviceInfo = _deviceManager.GetDevice(id);
 067        if (deviceInfo is null)
 68        {
 069            return NotFound();
 70        }
 71
 072        return deviceInfo;
 73    }
 74
 75    /// <summary>
 76    /// Get options for a device.
 77    /// </summary>
 78    /// <param name="id">Device Id.</param>
 79    /// <response code="200">Device options retrieved.</response>
 80    /// <response code="404">Device not found.</response>
 81    /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if t
 82    [HttpGet("Options")]
 83    [ProducesResponseType(StatusCodes.Status200OK)]
 84    [ProducesResponseType(StatusCodes.Status404NotFound)]
 85    public ActionResult<DeviceOptionsDto> GetDeviceOptions([FromQuery, Required] string id)
 86    {
 087        var deviceInfo = _deviceManager.GetDeviceOptions(id);
 088        if (deviceInfo is null)
 89        {
 090            return NotFound();
 91        }
 92
 093        return deviceInfo;
 94    }
 95
 96    /// <summary>
 97    /// Update device options.
 98    /// </summary>
 99    /// <param name="id">Device Id.</param>
 100    /// <param name="deviceOptions">Device Options.</param>
 101    /// <response code="204">Device options updated.</response>
 102    /// <returns>A <see cref="NoContentResult"/>.</returns>
 103    [HttpPost("Options")]
 104    [ProducesResponseType(StatusCodes.Status204NoContent)]
 105    public async Task<ActionResult> UpdateDeviceOptions(
 106        [FromQuery, Required] string id,
 107        [FromBody, Required] DeviceOptionsDto deviceOptions)
 108    {
 109        await _deviceManager.UpdateDeviceOptions(id, deviceOptions.CustomName).ConfigureAwait(false);
 110        return NoContent();
 111    }
 112
 113    /// <summary>
 114    /// Deletes a device.
 115    /// </summary>
 116    /// <param name="id">Device Id.</param>
 117    /// <response code="204">Device deleted.</response>
 118    /// <response code="404">Device not found.</response>
 119    /// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not b
 120    [HttpDelete]
 121    [ProducesResponseType(StatusCodes.Status204NoContent)]
 122    [ProducesResponseType(StatusCodes.Status404NotFound)]
 123    public async Task<ActionResult> DeleteDevice([FromQuery, Required] string id)
 124    {
 125        var existingDevice = _deviceManager.GetDevice(id);
 126        if (existingDevice is null)
 127        {
 128            return NotFound();
 129        }
 130
 131        var sessions = _deviceManager.GetDevices(new DeviceQuery { DeviceId = id });
 132
 133        foreach (var session in sessions.Items)
 134        {
 135            await _sessionManager.Logout(session).ConfigureAwait(false);
 136        }
 137
 138        return NoContent();
 139    }
 140}