2024-06-22 05:04:21 +00:00
|
|
|
package api
|
2024-04-05 17:07:36 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
2024-06-22 05:04:21 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
2024-04-05 17:07:36 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-10-14 14:23:51 +00:00
|
|
|
"strconv"
|
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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
2024-04-25 14:08:49 +00:00
|
|
|
status := c.QueryInt("status", -1)
|
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-04-25 14:08:49 +00:00
|
|
|
if status < 0 {
|
2024-07-15 16:02:28 +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-07-15 16:02:28 +00:00
|
|
|
if friends, err = services.ListRelationshipWithFilter(user, models.RelationshipStatus(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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
2024-04-05 17:40:01 +00:00
|
|
|
relatedName := c.Query("related")
|
2024-04-05 17:07:36 +00:00
|
|
|
relatedId, _ := c.ParamsInt("relatedId", 0)
|
|
|
|
|
2024-04-05 17:40:01 +00:00
|
|
|
var err error
|
|
|
|
var related models.Account
|
|
|
|
if relatedId > 0 {
|
|
|
|
related, err = services.GetAccount(uint(relatedId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
} else if len(relatedName) > 0 {
|
|
|
|
related, err = services.LookupAccount(relatedName)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "must one of username or user id")
|
2024-04-05 17:07:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-15 16:02:28 +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-10-14 14:23:51 +00:00
|
|
|
services.AddEvent(user.ID, "relationships.friends.new", strconv.Itoa(relatedId), 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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
relatedName := c.Query("related")
|
|
|
|
relatedId, _ := c.ParamsInt("relatedId", 0)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var related models.Account
|
|
|
|
if relatedId > 0 {
|
|
|
|
related, err = services.GetAccount(uint(relatedId))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
} else if len(relatedName) > 0 {
|
|
|
|
related, err = services.LookupAccount(relatedName)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "must one of username or user id")
|
|
|
|
}
|
|
|
|
|
|
|
|
friend, err := services.NewBlockship(user, related)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
2024-10-14 14:23:51 +00:00
|
|
|
services.AddEvent(user.ID, "relationships.blocks.new", strconv.Itoa(relatedId), 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
|
|
|
|
}
|
|
|
|
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-07-23 15:50:05 +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
|
|
|
|
}
|
|
|
|
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-23 15:50:05 +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
|
|
|
}
|
|
|
|
}
|