2024-05-21 20:14:52 +02:00
|
|
|
using Foxchat.Core;
|
|
|
|
using Foxchat.Core.Models;
|
|
|
|
using Foxchat.Identity.Database;
|
2024-05-22 17:20:00 +02:00
|
|
|
using Foxchat.Identity.Database.Models;
|
|
|
|
using Foxchat.Identity.Middleware;
|
|
|
|
using Foxchat.Identity.Utils;
|
2024-05-21 20:14:52 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-05-22 17:20:00 +02:00
|
|
|
using Newtonsoft.Json;
|
2024-05-21 20:14:52 +02:00
|
|
|
|
|
|
|
namespace Foxchat.Identity.Controllers;
|
|
|
|
|
|
|
|
[ApiController]
|
2024-05-22 17:20:00 +02:00
|
|
|
[ClientAuthenticate]
|
2024-05-21 20:14:52 +02:00
|
|
|
[Route("/_fox/ident/users")]
|
|
|
|
public class UsersController(ILogger logger, InstanceConfig config, IdentityContext db) : ControllerBase
|
|
|
|
{
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
public async Task<IActionResult> GetUser(Ulid id)
|
|
|
|
{
|
|
|
|
var user = await db.Accounts.FirstOrDefaultAsync(a => a.Id == id);
|
|
|
|
if (user == null) throw new ApiError.NotFound("User not found.");
|
|
|
|
|
|
|
|
return Ok(new Users.User(user.Id.ToString(), user.Username, config.Domain, null));
|
|
|
|
}
|
2024-05-22 17:20:00 +02:00
|
|
|
|
|
|
|
[HttpGet("@me")]
|
|
|
|
[Authorize("identify")]
|
|
|
|
public IActionResult GetMe()
|
|
|
|
{
|
|
|
|
var acct = HttpContext.GetAccountOrThrow();
|
|
|
|
var token = HttpContext.GetToken()!;
|
|
|
|
var showEmail = token.Scopes.ExpandScopes().Contains("email");
|
|
|
|
|
|
|
|
return Ok(new MeUser(
|
|
|
|
acct.Id,
|
|
|
|
acct.Username,
|
|
|
|
acct.Role,
|
|
|
|
null,
|
|
|
|
showEmail ? acct.Email : null
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public record MeUser(
|
|
|
|
Ulid Id,
|
|
|
|
string Username,
|
|
|
|
Account.AccountRole Role,
|
|
|
|
string? AvatarUrl,
|
|
|
|
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
|
|
|
string? Email
|
|
|
|
);
|
2024-05-21 20:14:52 +02:00
|
|
|
}
|