From d66b591f92a36b26de1564d6884762294b6255b1 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 6 May 2024 21:54:01 +0800 Subject: [PATCH] :sparkles: Can check channel availability with API --- pkg/server/channels_api.go | 18 ++++++++++++++++++ pkg/server/startup.go | 1 + 2 files changed, 19 insertions(+) diff --git a/pkg/server/channels_api.go b/pkg/server/channels_api.go index e909ea2..9d14eb3 100644 --- a/pkg/server/channels_api.go +++ b/pkg/server/channels_api.go @@ -25,6 +25,24 @@ func getChannel(c *fiber.Ctx) error { return c.JSON(channel) } +func getChannelAvailability(c *fiber.Ctx) error { + user := c.Locals("principal").(models.Account) + alias := c.Params("channel") + + var err error + var channel models.Channel + if val, ok := c.Locals("realm").(models.Realm); ok { + channel, _, err = services.GetAvailableChannelWithAlias(alias, user, val.ID) + } else { + channel, _, err = services.GetAvailableChannelWithAlias(alias, user) + } + if err != nil { + return fiber.NewError(fiber.StatusForbidden, err.Error()) + } + + return c.JSON(channel) +} + func listChannel(c *fiber.Ctx) error { var err error var channels []models.Channel diff --git a/pkg/server/startup.go b/pkg/server/startup.go index 6b17b7c..c56be90 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -79,6 +79,7 @@ func NewServer() { { channels.Get("/", listChannel) channels.Get("/:channel", getChannel) + channels.Get("/:channel/available", authMiddleware, getChannelAvailability) channels.Get("/me", authMiddleware, listOwnedChannel) channels.Get("/me/available", authMiddleware, listAvailableChannel)