astral-api/Astral.ApiServer/Controllers/V1/UserApiController.cs

134 lines
3.9 KiB
C#
Raw Normal View History

2024-12-14 18:48:03 +01:00
// <copyright file="UserApiController.cs" company="alveus.dev">
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
// </copyright>
using Astral.ApiServer.Models;
2024-12-15 17:06:14 +01:00
using Astral.ApiServer.Models.Requests;
using Astral.ApiServer.Models.Responses;
2024-12-14 18:48:03 +01:00
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")]
2024-12-15 17:06:14 +01:00
[Consumes("application/json")]
2024-12-14 18:48:03 +01:00
[Authorize]
public class UserApiController : BaseApiController
{
private readonly IIdentityProvider _identityProvider;
2024-12-15 17:06:14 +01:00
private readonly IUserPresenceService _userPresenceService;
2024-12-14 18:48:03 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="UserApiController"/> class.
/// </summary>
/// <param name="identityProvider">Instance of <see cref="IIdentityProvider"/>.</param>
2024-12-15 17:06:14 +01:00
/// <param name="userPresenceService">Instance of <see cref="IUserPresenceService"/>.</param>
public UserApiController(
IIdentityProvider identityProvider,
IUserPresenceService userPresenceService)
2024-12-14 18:48:03 +01:00
{
_identityProvider = identityProvider;
2024-12-15 17:06:14 +01:00
_userPresenceService = userPresenceService;
2024-12-14 18:48:03 +01:00
}
/// <summary>
/// Get the user's profile.
/// </summary>
[HttpGet("profile")]
public IActionResult GetUserProfile()
{
var userId = _identityProvider.GetUserId();
var userName = _identityProvider.GetUserName();
2024-12-15 17:06:14 +01:00
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>
2024-12-15 21:20:27 +01:00
/// <param name="heartbeatModel">Instance of <see cref="HeartbeatRootRequestModel"/>.</param>
2024-12-15 17:06:14 +01:00
[Authorize]
[HttpPut("location")]
public async Task<IActionResult> ReceiveLocation([FromBody] HeartbeatRootRequestModel heartbeatModel)
{
2024-12-15 21:20:27 +01:00
if (heartbeatModel?.Location != null)
{
var heartbeatDto = heartbeatModel.Location.ToUserLocationHeartbeat();
await _userPresenceService.ConsumeLocationHeartbeat(heartbeatDto);
}
2024-12-15 17:06:14 +01:00
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)
2024-12-14 18:48:03 +01:00
{
2024-12-15 17:06:14 +01:00
return FailureResult();
}
await using (var stream = cert.OpenReadStream())
{
await _userPresenceService.ConsumePublicKey(stream);
}
return SuccessResult();
2024-12-14 18:48:03 +01:00
}
/// <summary>
2024-12-15 17:06:14 +01:00
/// Receive the user settings from the client.
/// TODO: Investigate this functionality.
2024-12-14 18:48:03 +01:00
/// </summary>
2024-12-15 17:06:14 +01:00
[Consumes("application/json")]
2024-12-14 18:48:03 +01:00
[HttpPost("locker")]
2024-12-15 17:06:14 +01:00
public IActionResult PostLockerContents()
2024-12-14 18:48:03 +01:00
{
return SuccessResult();
}
/// <summary>
2024-12-15 17:06:14 +01:00
/// Return user settings to the client.
/// TODO: Investigate this functionality.
2024-12-14 18:48:03 +01:00
/// </summary>
2024-12-15 17:06:14 +01:00
[Produces("application/json")]
2024-12-14 18:48:03 +01:00
[HttpGet("locker")]
2024-12-15 17:06:14 +01:00
public IActionResult GetLockerContents()
2024-12-14 18:48:03 +01:00
{
2024-12-15 17:06:14 +01:00
var req = Request;
return SuccessResult(new object());
2024-12-14 18:48:03 +01:00
}
}