133 lines
3.9 KiB
C#
133 lines
3.9 KiB
C#
// <copyright file="UserApiController.cs" company="alveus.dev">
|
|
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
|
|
// </copyright>
|
|
|
|
using Astral.ApiServer.Models;
|
|
using Astral.ApiServer.Models.Requests;
|
|
using Astral.ApiServer.Models.Responses;
|
|
using Astral.Services.Interfaces;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Astral.ApiServer.Controllers.V1;
|
|
|
|
/// <summary>
|
|
/// User api controller.
|
|
/// </summary>
|
|
[Route("api/v1/user")]
|
|
[Consumes("application/json")]
|
|
[Authorize]
|
|
public class UserApiController : BaseApiController
|
|
{
|
|
private readonly IIdentityProvider _identityProvider;
|
|
private readonly IUserPresenceService _userPresenceService;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="UserApiController"/> class.
|
|
/// </summary>
|
|
/// <param name="identityProvider">Instance of <see cref="IIdentityProvider"/>.</param>
|
|
/// <param name="userPresenceService">Instance of <see cref="IUserPresenceService"/>.</param>
|
|
public UserApiController(
|
|
IIdentityProvider identityProvider,
|
|
IUserPresenceService userPresenceService)
|
|
{
|
|
_identityProvider = identityProvider;
|
|
_userPresenceService = userPresenceService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the user's profile.
|
|
/// </summary>
|
|
[HttpGet("profile")]
|
|
public IActionResult GetUserProfile()
|
|
{
|
|
var userId = _identityProvider.GetUserId();
|
|
var userName = _identityProvider.GetUserName();
|
|
|
|
return SuccessResult(new UserProfileResponseModel()
|
|
{
|
|
User = new UserProfileModel()
|
|
{
|
|
AccountId = userId,
|
|
Username = userName,
|
|
XmppPassword = string.Empty,
|
|
DiscourseApiKey = string.Empty,
|
|
WalletId = Guid.Empty
|
|
}
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Receive a user's heartbeat.
|
|
/// </summary>
|
|
[Authorize]
|
|
[HttpPut("heartbeat")]
|
|
public async Task<IActionResult> ReceiveHeartbeat()
|
|
{
|
|
await _userPresenceService.HandleHeartbeat();
|
|
return SuccessResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Receive a user's location heartbeat.
|
|
/// </summary>
|
|
/// <param name="heartbeatModel">Instance of <see cref="HeartbeatRootRequestModel"/>.</param>
|
|
[Authorize]
|
|
[HttpPut("location")]
|
|
public async Task<IActionResult> ReceiveLocation([FromBody] HeartbeatRootRequestModel heartbeatModel)
|
|
{
|
|
if (heartbeatModel?.Location != null)
|
|
{
|
|
var heartbeatDto = heartbeatModel.Location.ToUserLocationHeartbeat();
|
|
await _userPresenceService.ConsumeLocationHeartbeat(heartbeatDto);
|
|
}
|
|
|
|
return SuccessResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Receive the user's public key.
|
|
/// </summary>
|
|
[HttpPut("public_key")]
|
|
[Authorize]
|
|
[Consumes("multipart/form-data")]
|
|
public async Task<IActionResult> PutPublicKey()
|
|
{
|
|
var cert = HttpContext.Request.Form.Files.GetFile("public_key");
|
|
|
|
if (cert is null)
|
|
{
|
|
return FailureResult();
|
|
}
|
|
|
|
await using (var stream = cert.OpenReadStream())
|
|
{
|
|
await _userPresenceService.ConsumePublicKey(stream);
|
|
}
|
|
|
|
return SuccessResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Receive the user settings from the client.
|
|
/// TODO: Investigate this functionality.
|
|
/// </summary>
|
|
[Consumes("application/json")]
|
|
[HttpPost("locker")]
|
|
public IActionResult PostLockerContents()
|
|
{
|
|
return SuccessResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return user settings to the client.
|
|
/// TODO: Investigate this functionality.
|
|
/// </summary>
|
|
[Produces("application/json")]
|
|
[HttpGet("locker")]
|
|
public IActionResult GetLockerContents()
|
|
{
|
|
var req = Request;
|
|
return SuccessResult(new object());
|
|
}
|
|
}
|