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 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)]) ); } }