2024-09-16 16:12:09 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-31 14:41:32 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
|
|
|
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-10-14 13:30:02 +00:00
|
|
|
"strconv"
|
2024-09-16 16:12:09 +00:00
|
|
|
|
2024-11-02 05:41:51 +00:00
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
|
2024-09-16 16:12:09 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getSubscriptionOnUser(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-09-16 16:12:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-09-16 16:12:09 +00:00
|
|
|
|
|
|
|
otherUserId, err := c.ParamsInt("userId", 0)
|
|
|
|
otherUser, err := services.GetAccountWithID(uint(otherUserId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("unable to get user: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription, err := services.GetSubscriptionOnUser(user, otherUser)
|
|
|
|
if err != nil {
|
2024-09-16 18:06:04 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get subscription: %v", err))
|
|
|
|
} else if subscription == nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, "subscription does not exist")
|
2024-09-16 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(subscription)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSubscriptionOnTag(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-09-16 16:12:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-09-16 16:12:09 +00:00
|
|
|
|
|
|
|
tagId, err := c.ParamsInt("tagId", 0)
|
|
|
|
tag, err := services.GetTagWithID(uint(tagId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("unable to get tag: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription, err := services.GetSubscriptionOnTag(user, tag)
|
|
|
|
if err != nil {
|
2024-09-16 18:06:04 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get subscription: %v", err))
|
|
|
|
} else if subscription == nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, "subscription does not exist")
|
2024-09-16 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(subscription)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSubscriptionOnCategory(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-09-16 16:12:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-09-16 16:12:09 +00:00
|
|
|
|
|
|
|
categoryId, err := c.ParamsInt("categoryId", 0)
|
|
|
|
category, err := services.GetCategoryWithID(uint(categoryId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("unable to get category: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription, err := services.GetSubscriptionOnCategory(user, category)
|
|
|
|
if err != nil {
|
2024-09-16 18:06:04 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get subscription: %v", err))
|
|
|
|
} else if subscription == nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, "subscription does not exist")
|
2024-09-16 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(subscription)
|
|
|
|
}
|
|
|
|
|
|
|
|
func subscribeToUser(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-09-16 16:12:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-09-16 16:12:09 +00:00
|
|
|
|
|
|
|
otherUserId, err := c.ParamsInt("userId", 0)
|
|
|
|
otherUser, err := services.GetAccountWithID(uint(otherUserId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("unable to get user: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription, err := services.SubscribeToUser(user, otherUser)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to subscribe to user: %v", err))
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
_ = authkit.AddEventExt(
|
|
|
|
gap.Nx,
|
2024-10-14 13:30:02 +00:00
|
|
|
"posts.subscribe.users",
|
|
|
|
strconv.Itoa(int(otherUser.ID)),
|
2024-10-31 14:41:32 +00:00
|
|
|
c,
|
2024-10-14 13:30:02 +00:00
|
|
|
)
|
|
|
|
|
2024-09-16 16:12:09 +00:00
|
|
|
return c.JSON(subscription)
|
|
|
|
}
|
|
|
|
|
|
|
|
func subscribeToTag(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-09-16 16:12:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-09-16 16:12:09 +00:00
|
|
|
|
|
|
|
tagId, err := c.ParamsInt("tagId", 0)
|
|
|
|
tag, err := services.GetTagWithID(uint(tagId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("unable to get tag: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription, err := services.SubscribeToTag(user, tag)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to subscribe to tag: %v", err))
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
_ = authkit.AddEventExt(
|
|
|
|
gap.Nx,
|
2024-10-14 13:30:02 +00:00
|
|
|
"posts.subscribe.tags",
|
|
|
|
strconv.Itoa(int(tag.ID)),
|
2024-10-31 14:41:32 +00:00
|
|
|
c,
|
2024-10-14 13:30:02 +00:00
|
|
|
)
|
|
|
|
|
2024-09-16 16:12:09 +00:00
|
|
|
return c.JSON(subscription)
|
|
|
|
}
|
|
|
|
|
|
|
|
func subscribeToCategory(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-09-16 16:12:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-09-16 16:12:09 +00:00
|
|
|
|
|
|
|
categoryId, err := c.ParamsInt("categoryId", 0)
|
|
|
|
category, err := services.GetCategoryWithID(uint(categoryId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("unable to get category: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription, err := services.SubscribeToCategory(user, category)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to subscribe to category: %v", err))
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
_ = authkit.AddEventExt(
|
|
|
|
gap.Nx,
|
2024-10-14 13:30:02 +00:00
|
|
|
"posts.subscribe.categories",
|
|
|
|
strconv.Itoa(int(category.ID)),
|
2024-10-31 14:41:32 +00:00
|
|
|
c,
|
2024-10-14 13:40:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return c.JSON(subscription)
|
|
|
|
}
|
|
|
|
|
2024-09-16 16:12:09 +00:00
|
|
|
func unsubscribeFromUser(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-09-16 16:12:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-09-16 16:12:09 +00:00
|
|
|
|
|
|
|
otherUserId, err := c.ParamsInt("userId", 0)
|
|
|
|
otherUser, err := services.GetAccountWithID(uint(otherUserId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("unable to get user: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = services.UnsubscribeFromUser(user, otherUser)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to unsubscribe from user: %v", err))
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
_ = authkit.AddEventExt(
|
|
|
|
gap.Nx,
|
2024-10-14 13:30:02 +00:00
|
|
|
"posts.unsubscribe.users",
|
|
|
|
strconv.Itoa(int(otherUser.ID)),
|
2024-10-31 14:41:32 +00:00
|
|
|
c,
|
2024-10-14 13:30:02 +00:00
|
|
|
)
|
|
|
|
|
2024-09-16 16:12:09 +00:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unsubscribeFromTag(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-09-16 16:12:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-09-16 16:12:09 +00:00
|
|
|
|
|
|
|
tagId, err := c.ParamsInt("tagId", 0)
|
|
|
|
tag, err := services.GetTagWithID(uint(tagId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("unable to get tag: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = services.UnsubscribeFromTag(user, tag)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to unsubscribe from tag: %v", err))
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
_ = authkit.AddEventExt(
|
|
|
|
gap.Nx,
|
2024-10-14 13:30:02 +00:00
|
|
|
"posts.unsubscribe.tags",
|
|
|
|
strconv.Itoa(int(tag.ID)),
|
2024-10-31 14:41:32 +00:00
|
|
|
c,
|
2024-10-14 13:30:02 +00:00
|
|
|
)
|
|
|
|
|
2024-09-16 16:12:09 +00:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unsubscribeFromCategory(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-09-16 16:12:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-09-16 16:12:09 +00:00
|
|
|
|
|
|
|
categoryId, err := c.ParamsInt("categoryId", 0)
|
|
|
|
category, err := services.GetCategoryWithID(uint(categoryId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("unable to get category: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = services.UnsubscribeFromCategory(user, category)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to unsubscribe from category: %v", err))
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
_ = authkit.AddEventExt(
|
|
|
|
gap.Nx,
|
2024-10-14 13:30:02 +00:00
|
|
|
"posts.unsubscribe.categories",
|
|
|
|
strconv.Itoa(int(category.ID)),
|
2024-10-31 14:41:32 +00:00
|
|
|
c,
|
2024-10-14 13:40:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|