astral-api/Astral.ApiServer/Controllers/V1/UserApiController.cs
Mike 81aa0ec1c0
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful
WIP heartbeat and user presence
2024-12-15 16:06:14 +00:00

126 lines
3.5 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>
[Authorize]
[HttpPut("location")]
public async Task<IActionResult> ReceiveLocation([FromBody] HeartbeatRootRequestModel heartbeatModel)
{
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());
}
}