You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Api/ConnectService.cs

205 lines
7.7 KiB

using System;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Connect;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Connect;
using MediaBrowser.Model.Dto;
using ServiceStack;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
[Route("/Users/{Id}/Connect/Link", "POST", Summary = "Creates a Connect link for a user")]
[Authenticated(Roles = "Admin")]
public class CreateConnectLink : IReturn<UserLinkResult>
{
[ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Id { get; set; }
[ApiMember(Name = "ConnectUsername", Description = "Connect username", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ConnectUsername { get; set; }
}
[Route("/Users/{Id}/Connect/Link", "DELETE", Summary = "Removes a Connect link for a user")]
[Authenticated(Roles = "Admin")]
public class DeleteConnectLink : IReturnVoid
{
[ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string Id { get; set; }
}
[Route("/Connect/Invite", "POST", Summary = "Creates a Connect link for a user")]
[Authenticated(Roles = "Admin")]
public class CreateConnectInvite : IReturn<UserLinkResult>
{
[ApiMember(Name = "ConnectUsername", Description = "Connect username", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public string ConnectUsername { get; set; }
[ApiMember(Name = "SendingUserId", Description = "Sending User Id", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public string SendingUserId { get; set; }
[ApiMember(Name = "EnabledLibraries", Description = "EnabledLibraries", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public string EnabledLibraries { get; set; }
[ApiMember(Name = "EnabledChannels", Description = "EnabledChannels", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public string EnabledChannels { get; set; }
[ApiMember(Name = "EnableLiveTv", Description = "EnableLiveTv", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public bool EnableLiveTv { get; set; }
}
[Route("/Connect/Pending", "GET", Summary = "Creates a Connect link for a user")]
[Authenticated(Roles = "Admin")]
public class GetPendingGuests : IReturn<List<ConnectAuthorization>>
{
}
[Route("/Connect/Pending", "DELETE", Summary = "Deletes a Connect link for a user")]
[Authenticated(Roles = "Admin")]
public class DeleteAuthorization : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Authorization Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string Id { get; set; }
}
[Route("/Connect/Exchange", "GET", Summary = "Gets the corresponding local user from a connect user id")]
[Authenticated]
public class GetLocalUser : IReturn<ConnectAuthenticationExchangeResult>
{
[ApiMember(Name = "ConnectUserId", Description = "ConnectUserId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ConnectUserId { get; set; }
}
[Route("/Connect/Supporters", "GET")]
[Authenticated(Roles = "Admin")]
public class GetConnectSupporterSummary : IReturn<ConnectSupporterSummary>
{
}
[Route("/Connect/Supporters", "DELETE")]
[Authenticated(Roles = "Admin")]
public class RemoveConnectSupporter : IReturnVoid
{
[ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string Id { get; set; }
}
[Route("/Connect/Supporters", "POST")]
[Authenticated(Roles = "Admin")]
public class AddConnectSupporter : IReturnVoid
{
[ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Id { get; set; }
}
public class ConnectService : BaseApiService
{
private readonly IConnectManager _connectManager;
private readonly IUserManager _userManager;
public ConnectService(IConnectManager connectManager, IUserManager userManager)
{
_connectManager = connectManager;
_userManager = userManager;
}
public async Task<object> Get(GetConnectSupporterSummary request)
{
var result = await _connectManager.GetConnectSupporterSummary().ConfigureAwait(false);
var existingConnectUserIds = result.Users.Select(i => i.Id).ToList();
result.EligibleUsers = _userManager.Users
.Where(i => !string.IsNullOrWhiteSpace(i.ConnectUserId))
.Where(i => !existingConnectUserIds.Contains(i.ConnectUserId, StringComparer.OrdinalIgnoreCase))
.OrderBy(i => i.Name)
.Select(i => _userManager.GetUserDto(i))
.ToList();
return ToOptimizedResult(result);
}
public void Delete(RemoveConnectSupporter request)
{
var task = _connectManager.RemoveConnectSupporter(request.Id);
Task.WaitAll(task);
}
public void Post(AddConnectSupporter request)
{
var task = _connectManager.AddConnectSupporter(request.Id);
Task.WaitAll(task);
}
public object Post(CreateConnectLink request)
{
return _connectManager.LinkUser(request.Id, request.ConnectUsername);
}
public object Post(CreateConnectInvite request)
{
var enabledLibraries = (request.EnabledLibraries ?? string.Empty)
.Split(',')
.Where(i => !string.IsNullOrWhiteSpace(i))
.ToArray();
var enabledChannels = (request.EnabledChannels ?? string.Empty)
.Split(',')
.Where(i => !string.IsNullOrWhiteSpace(i))
.ToArray();
return _connectManager.InviteUser(new ConnectAuthorizationRequest
{
ConnectUserName = request.ConnectUsername,
SendingUserId = request.SendingUserId,
EnabledLibraries = enabledLibraries,
EnabledChannels = enabledChannels,
EnableLiveTv = request.EnableLiveTv
});
}
public void Delete(DeleteConnectLink request)
{
var task = _connectManager.RemoveConnect(request.Id);
Task.WaitAll(task);
}
public async Task<object> Get(GetPendingGuests request)
{
var result = await _connectManager.GetPendingGuests().ConfigureAwait(false);
return ToOptimizedResult(result);
}
public void Delete(DeleteAuthorization request)
{
var task = _connectManager.CancelAuthorization(request.Id);
Task.WaitAll(task);
}
public async Task<object> Get(GetLocalUser request)
{
var user = await _connectManager.GetLocalUser(request.ConnectUserId).ConfigureAwait(false);
if (user == null)
{
throw new ResourceNotFoundException();
}
return ToOptimizedResult(new ConnectAuthenticationExchangeResult
{
AccessToken = user.ConnectAccessKey,
LocalUserId = user.Id.ToString("N")
});
}
}
}