chat: add initial GuildsController

This commit is contained in:
sam 2024-05-21 20:14:52 +02:00
parent 7b4cbd4fb7
commit 727f2f6ba2
23 changed files with 248 additions and 38 deletions

View file

@ -0,0 +1,47 @@
using Foxchat.Chat.Database;
using Foxchat.Chat.Database.Models;
using Foxchat.Chat.Middleware;
using Foxchat.Chat.Services;
using Foxchat.Core.Models;
using Foxchat.Core.Models.Http;
using Microsoft.AspNetCore.Mvc;
using ApiError = Foxchat.Core.ApiError;
namespace Foxchat.Chat.Controllers.Api;
[ApiController]
[Route("/_fox/chat/guilds")]
public class GuildsController(ILogger logger, ChatContext db, UserResolverService userResolverService) : ControllerBase
{
[HttpPost]
public async Task<IActionResult> CreateGuild([FromBody] GuildsApi.CreateGuildRequest req)
{
var (instance, sig) = HttpContext.GetSignatureOrThrow();
if (sig.UserId == null) throw new ApiError.IncomingFederationError("This endpoint requires a user ID.");
var user = await userResolverService.ResolveUserAsync(instance, sig.UserId);
var guild = new Guild
{
Name = req.Name,
Owner = user,
};
db.Add(guild);
guild.Users.Add(user);
var defaultChannel = new Channel
{
Guild = guild,
Name = "general"
};
db.Add(defaultChannel);
await db.SaveChangesAsync();
return Ok(new Guilds.Guild(
guild.Id.ToString(),
guild.Name,
[user.Id.ToString()],
[new Channels.PartialChannel(defaultChannel.Id.ToString(), defaultChannel.Name)])
);
}
}

View file

@ -10,7 +10,7 @@ using ApiError = Foxchat.Core.ApiError;
namespace Foxchat.Chat.Controllers;
[ApiController]
[Unauthenticated]
[ServerUnauthenticated]
[Route("/_fox/chat/hello")]
public class HelloController(
ILogger logger,
@ -27,6 +27,8 @@ public class HelloController(
if (!HttpContext.ExtractRequestData(out var signature, out var domain, out var signatureData))
throw new ApiError.IncomingFederationError("This endpoint requires signed requests.");
if (domain != req.Host)
throw new ApiError.IncomingFederationError("Host is invalid.");
if (!requestSigningService.VerifySignature(node.PublicKey, signature, signatureData))
throw new ApiError.IncomingFederationError("Signature is not valid.");