Able to lookup publisher via user / realm

This commit is contained in:
2024-12-11 22:17:18 +08:00
parent c125565896
commit 2543e1d125
4 changed files with 57 additions and 162 deletions

View File

@ -9,7 +9,8 @@ func MapAPIs(app *fiber.App, baseURL string) {
{
publishers := api.Group("/publishers").Name("Publisher API")
{
publishers.Get("/", listOwnedPublisher)
publishers.Get("/", listRelatedPublisher)
publishers.Get("/me", listOwnedPublisher)
publishers.Post("/personal", createPersonalPublisher)
publishers.Post("/organization", createOrganizationPublisher)
publishers.Get("/:name/pins", listPinnedPost)

View File

@ -47,6 +47,32 @@ func getPublisher(c *fiber.Ctx) error {
return c.JSON(publisher)
}
func listRelatedPublisher(c *fiber.Ctx) error {
tx := database.C
if len(c.Query("user")) > 0 {
user, err := authkit.GetUserByName(gap.Nx, c.Query("user"))
if err != nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("unable to find user: %v", err))
}
tx = tx.Where("account_id = ? AND type = ?", user.ID, models.PublisherTypePersonal)
} else if len(c.Query("realm")) > 0 {
realm, err := authkit.GetRealmByAlias(gap.Nx, c.Query("realm"))
if err != nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("unable to find realm: %v", err))
}
tx = tx.Where("realm_id = ? AND type = ?", realm.ID, models.PublisherTypeOrganization)
} else {
return fiber.NewError(fiber.StatusBadRequest, "missing user or realm in query string")
}
var publishers []models.Publisher
if err := database.C.Find(&publishers).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
return c.JSON(publishers)
}
func listOwnedPublisher(c *fiber.Ctx) error {
if err := sec.EnsureAuthenticated(c); err != nil {
return err