// Copyright Epic Games, Inc. All Rights Reserved.
using HordeServer.Api;
using HordeServer.Collections;
using HordeServer.Models;
using HordeServer.Services;
using HordeServer.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace HordeServer.Controllers
{
///
/// Controller for the /api/v1/user endpoint
///
[ApiController]
[Authorize]
[Route("[controller]")]
public class UserController : ControllerBase
{
///
/// The user collection instance
///
IUserCollection UserCollection { get; set; }
///
/// The avatar service
///
IAvatarService? AvatarService { get; set; }
///
/// Constructor
///
///
///
public UserController(IUserCollection UserCollection, IAvatarService? AvatarService)
{
this.UserCollection = UserCollection;
this.AvatarService = AvatarService;
}
///
/// Gets information about the logged in user
///
/// Http result code
[HttpGet]
[Route("/api/v1/user")]
[ProducesResponseType(typeof(List), 200)]
public async Task> GetUserAsync([FromQuery] PropertyFilter? Filter = null)
{
IUser? InternalUser = await UserCollection.GetUserAsync(User);
if (InternalUser == null)
{
return NotFound();
}
IAvatar? Avatar = (AvatarService == null)? (IAvatar?)null : await AvatarService.GetAvatarAsync(InternalUser);
IUserClaims Claims = await UserCollection.GetClaimsAsync(InternalUser.Id);
IUserSettings Settings = await UserCollection.GetSettingsAsync(InternalUser.Id);
return PropertyFilter.Apply(new GetUserResponse(InternalUser, Avatar, Claims, Settings), Filter);
}
///
/// Updates the logged in user
///
/// Http result code
[HttpPut]
[Route("/api/v1/user")]
public async Task UpdateUserAsync(UpdateUserRequest Request)
{
ObjectId? UserId = User.GetUserId();
if(UserId == null)
{
return BadRequest("Current user does not have a registered profile");
}
await UserCollection.UpdateSettingsAsync(UserId.Value, Request.EnableExperimentalFeatures, Request.EnableIssueNotifications, Request.DashboardSettings?.ToBsonValue(), Request.AddPinnedJobIds?.Select(x => new ObjectId(x)), Request.RemovePinnedJobIds?.Select(x => new ObjectId(x)));
return Ok();
}
}
}