diff --git a/Foxchat.Chat/Controllers/Api/GuildsController.cs b/Foxchat.Chat/Controllers/Api/GuildsController.cs index ab99a6d..bd8d0bb 100644 --- a/Foxchat.Chat/Controllers/Api/GuildsController.cs +++ b/Foxchat.Chat/Controllers/Api/GuildsController.cs @@ -5,6 +5,7 @@ using Foxchat.Chat.Services; using Foxchat.Core.Models; using Foxchat.Core.Models.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using ApiError = Foxchat.Core.ApiError; namespace Foxchat.Chat.Controllers.Api; @@ -16,10 +17,9 @@ public class GuildsController(ILogger logger, ChatContext db, UserResolverServic [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 (instance, _, userId) = HttpContext.GetSignatureWithUser(); - var user = await userResolverService.ResolveUserAsync(instance, sig.UserId); + var user = await userResolverService.ResolveUserAsync(instance, userId); var guild = new Guild { @@ -38,10 +38,47 @@ public class GuildsController(ILogger logger, ChatContext db, UserResolverServic await db.SaveChangesAsync(); return Ok(new Guilds.Guild( - guild.Id.ToString(), - guild.Name, + guild.Id.ToString(), + guild.Name, [user.Id.ToString()], [new Channels.PartialChannel(defaultChannel.Id.ToString(), defaultChannel.Name)]) ); } + + [HttpGet("{id}")] + public async Task GetGuild(Ulid id) + { + var (instance, _, userId) = HttpContext.GetSignatureWithUser(); + var guild = await db.Guilds + .Include(g => g.Channels) + .FirstOrDefaultAsync(g => + g.Id == id && g.Users.Any(u => u.RemoteUserId == userId && u.InstanceId == instance.Id)); + if (guild == null) throw new ApiError.NotFound("Guild not found"); + + return Ok(new Guilds.Guild( + guild.Id.ToString(), + guild.Name, + [guild.OwnerId.ToString()], + guild.Channels.Select(c => new Channels.PartialChannel(c.Id.ToString(), c.Name)) + )); + } + + [HttpGet("@me")] + public async Task GetUserGuilds() + { + var (instance, _, userId) = HttpContext.GetSignatureWithUser(); + var guilds = await db.Guilds + .Include(g => g.Channels) + .Where(g => g.Users.Any(u => u.RemoteUserId == userId && u.InstanceId == instance.Id)) + .ToListAsync(); + + var guildResponses = guilds.Select(g => new Guilds.Guild( + g.Id.ToString(), + g.Name, + [g.OwnerId.ToString()], + g.Channels.Select(c => new Channels.PartialChannel(c.Id.ToString(), c.Name)) + )); + + return Ok(guildResponses); + } } \ No newline at end of file diff --git a/Foxchat.Chat/Middleware/ServerAuthenticationMiddleware.cs b/Foxchat.Chat/Middleware/ServerAuthenticationMiddleware.cs index daea8dc..4db585a 100644 --- a/Foxchat.Chat/Middleware/ServerAuthenticationMiddleware.cs +++ b/Foxchat.Chat/Middleware/ServerAuthenticationMiddleware.cs @@ -76,4 +76,11 @@ public static class HttpContextExtensions return ((IdentityInstance, SignatureData))obj!; } + + public static (IdentityInstance, SignatureData, string) GetSignatureWithUser(this HttpContext ctx) + { + var (instance, sig) = ctx.GetSignatureOrThrow(); + if (sig.UserId == null) throw new ApiError.IncomingFederationError("This endpoint requires a user ID."); + return (instance, sig, sig.UserId); + } } \ No newline at end of file