feat(chat): add /guilds/{id} and /guilds/@me endpoints
This commit is contained in:
parent
6aed05af06
commit
00a54f4f8b
2 changed files with 49 additions and 5 deletions
|
@ -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<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 (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<IActionResult> 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<IActionResult> 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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue