2024-06-22 05:04:21 +00:00
|
|
|
package api
|
2024-04-05 17:07:36 +00:00
|
|
|
|
|
|
|
import (
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/http/exts"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
|
2024-04-05 17:07:36 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-11-30 09:31:35 +00:00
|
|
|
"github.com/samber/lo"
|
2024-10-14 14:23:51 +00:00
|
|
|
"strconv"
|
2024-11-30 09:31:35 +00:00
|
|
|
"strings"
|
2024-04-05 17:07:36 +00:00
|
|
|
)
|
|
|
|
|
2024-07-15 16:02:28 +00:00
|
|
|
func listRelationship(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-11-30 09:31:35 +00:00
|
|
|
statusQuery := c.Query("status")
|
|
|
|
|
|
|
|
status := lo.Map(strings.Split(statusQuery, ","), func(s string, _ int) models.RelationshipStatus {
|
|
|
|
num, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return models.RelationshipStatus(num)
|
|
|
|
})
|
2024-04-05 17:07:36 +00:00
|
|
|
|
2024-04-25 14:08:49 +00:00
|
|
|
var err error
|
2024-07-15 16:02:28 +00:00
|
|
|
var friends []models.AccountRelationship
|
2024-11-30 09:31:35 +00:00
|
|
|
if len(status) < 0 {
|
2024-10-30 16:17:53 +00:00
|
|
|
if friends, err = services.ListAllRelationship(user); err != nil {
|
2024-04-25 14:08:49 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
2024-04-05 17:07:36 +00:00
|
|
|
} else {
|
2024-11-30 09:31:35 +00:00
|
|
|
if friends, err = services.ListRelationshipWithFilter(user, status...); err != nil {
|
2024-04-25 14:08:49 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
2024-04-05 17:07:36 +00:00
|
|
|
}
|
2024-04-25 14:08:49 +00:00
|
|
|
|
|
|
|
return c.JSON(friends)
|
2024-04-05 17:07:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-15 16:02:28 +00:00
|
|
|
func getRelationship(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-04-05 17:07:36 +00:00
|
|
|
relatedId, _ := c.ParamsInt("relatedId", 0)
|
|
|
|
|
|
|
|
related, err := services.GetAccount(uint(relatedId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-07-15 16:02:28 +00:00
|
|
|
if friend, err := services.GetRelationWithTwoNode(user.ID, related.ID); err != nil {
|
2024-04-05 17:07:36 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.JSON(friend)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-23 15:50:05 +00:00
|
|
|
func editRelationship(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-07-23 15:50:05 +00:00
|
|
|
relatedId, _ := c.ParamsInt("relatedId", 0)
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Status uint8 `json:"status"`
|
|
|
|
PermNodes map[string]any `json:"perm_nodes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
relationship, err := services.GetRelationWithTwoNode(user.ID, uint(relatedId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
relationship.Status = models.RelationshipStatus(data.Status)
|
|
|
|
relationship.PermNodes = data.PermNodes
|
|
|
|
|
|
|
|
if friendship, err := services.EditRelationship(relationship); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
2024-10-14 14:23:51 +00:00
|
|
|
services.AddEvent(user.ID, "relationships.edit", strconv.Itoa(int(relationship.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-07-23 15:50:05 +00:00
|
|
|
return c.JSON(friendship)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteRelationship(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-07-23 15:50:05 +00:00
|
|
|
relatedId, _ := c.ParamsInt("relatedId", 0)
|
|
|
|
|
|
|
|
related, err := services.GetAccount(uint(relatedId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
relationship, err := services.GetRelationWithTwoNode(user.ID, related.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := services.DeleteRelationship(relationship); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
2024-10-14 14:23:51 +00:00
|
|
|
services.AddEvent(user.ID, "relationships.delete", strconv.Itoa(int(relationship.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-07-23 15:50:05 +00:00
|
|
|
return c.JSON(relationship)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Friends stuff
|
|
|
|
|
2024-04-05 17:07:36 +00:00
|
|
|
func makeFriendship(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-11-29 16:35:11 +00:00
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Related string `json:"related" validate:"required"`
|
|
|
|
}
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-05 17:07:36 +00:00
|
|
|
|
2024-04-05 17:40:01 +00:00
|
|
|
var err error
|
|
|
|
var related models.Account
|
2024-11-29 16:35:11 +00:00
|
|
|
if numericId, err := strconv.Atoi(data.Related); err == nil {
|
|
|
|
related, err = services.GetAccount(uint(numericId))
|
2024-04-05 17:40:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
2024-11-29 16:35:11 +00:00
|
|
|
} else {
|
|
|
|
related, err = services.LookupAccount(data.Related)
|
2024-04-05 17:40:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
2024-04-05 17:07:36 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
friend, err := services.NewFriend(user, related)
|
2024-04-05 17:07:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
2024-11-29 16:35:11 +00:00
|
|
|
services.AddEvent(user.ID, "relationships.friends.new", strconv.Itoa(int(related.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-04-05 17:07:36 +00:00
|
|
|
return c.JSON(friend)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-26 14:09:39 +00:00
|
|
|
func makeBlockship(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-11-29 16:35:11 +00:00
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Related string `json:"related" validate:"required"`
|
|
|
|
}
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-26 14:09:39 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
var related models.Account
|
2024-11-29 16:35:11 +00:00
|
|
|
if numericId, err := strconv.Atoi(data.Related); err == nil {
|
|
|
|
related, err = services.GetAccount(uint(numericId))
|
2024-09-26 14:09:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
2024-11-29 16:35:11 +00:00
|
|
|
} else {
|
|
|
|
related, err = services.LookupAccount(data.Related)
|
2024-09-26 14:09:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
friend, err := services.NewBlockship(user, related)
|
2024-09-26 14:09:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
2024-11-29 16:35:11 +00:00
|
|
|
services.AddEvent(user.ID, "relationships.blocks.new", strconv.Itoa(int(related.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-09-26 14:09:39 +00:00
|
|
|
return c.JSON(friend)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-23 15:50:05 +00:00
|
|
|
func acceptFriend(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-04-05 17:07:36 +00:00
|
|
|
relatedId, _ := c.ParamsInt("relatedId", 0)
|
|
|
|
|
2024-07-23 15:50:05 +00:00
|
|
|
related, err := services.GetAccount(uint(relatedId))
|
2024-04-05 17:07:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
if err := services.HandleFriend(user, related, true); err != nil {
|
2024-04-05 17:07:36 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
2024-10-14 14:23:51 +00:00
|
|
|
services.AddEvent(user.ID, "relationships.friends.accept", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-07-23 15:50:05 +00:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
2024-04-05 17:07:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-23 15:50:05 +00:00
|
|
|
func declineFriend(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-04-05 17:07:36 +00:00
|
|
|
relatedId, _ := c.ParamsInt("relatedId", 0)
|
|
|
|
|
|
|
|
related, err := services.GetAccount(uint(relatedId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
if err := services.HandleFriend(user, related, false); err != nil {
|
2024-04-05 17:07:36 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
2024-10-14 14:23:51 +00:00
|
|
|
services.AddEvent(user.ID, "relationships.friends.decline", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-07-23 15:50:05 +00:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
2024-04-05 17:07:36 +00:00
|
|
|
}
|
|
|
|
}
|