Passport/pkg/internal/http/api/statuses_api.go

160 lines
4.6 KiB
Go
Raw Normal View History

2024-06-26 12:05:28 +00:00
package api
import (
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
2024-10-14 14:23:51 +00:00
"strconv"
"time"
2024-06-26 12:28:12 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
2024-10-27 04:50:07 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
2024-06-26 12:05:28 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
)
func getStatus(c *fiber.Ctx) error {
alias := c.Params("alias")
2024-06-26 12:28:12 +00:00
var user models.Account
if err := database.C.Where(models.Account{
Name: alias,
}).Preload("Profile").First(&user).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("account not found: %s", err))
2024-06-26 12:05:28 +00:00
}
status, err := services.GetStatus(user.ID)
disturbable := services.GetStatusDisturbable(user.ID) == nil
online := services.GetStatusOnline(user.ID) == nil
return c.JSON(fiber.Map{
"status": lo.Ternary(err == nil, &status, nil),
2024-06-26 12:28:12 +00:00
"last_seen_at": user.Profile.LastSeenAt,
2024-06-26 12:05:28 +00:00
"is_disturbable": disturbable,
"is_online": online,
})
}
func getMyselfStatus(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(*sec.UserInfo)
status, err := services.GetStatus(user.ID)
disturbable := services.GetStatusDisturbable(user.ID) == nil
online := services.GetStatusOnline(user.ID) == nil
var account models.Account
if err := database.C.Where(&models.Account{
BaseModel: models.BaseModel{ID: user.ID},
}).Preload("Profile").First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
return c.JSON(fiber.Map{
"status": lo.Ternary(err == nil, &status, nil),
"last_seen_at": account.Profile.LastSeenAt,
"is_disturbable": disturbable,
"is_online": online,
})
}
2024-06-26 12:05:28 +00:00
func setStatus(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(*sec.UserInfo)
2024-06-26 12:05:28 +00:00
var req struct {
Type string `json:"type" validate:"required"`
Label string `json:"label" validate:"required"`
2024-06-26 17:01:47 +00:00
Attitude uint `json:"attitude"`
2024-06-26 12:05:28 +00:00
IsNoDisturb bool `json:"is_no_disturb"`
IsInvisible bool `json:"is_invisible"`
ClearAt *time.Time `json:"clear_at"`
}
if err := exts.BindAndValidate(c, &req); err != nil {
return err
}
// End the status already exists
if status, err := services.GetStatus(user.ID); err == nil {
status.ClearAt = lo.ToPtr(time.Now())
database.C.Save(&status)
}
2024-06-26 12:05:28 +00:00
status := models.Status{
Type: req.Type,
Label: req.Label,
Attitude: models.StatusAttitude(req.Attitude),
IsNoDisturb: req.IsNoDisturb,
IsInvisible: req.IsInvisible,
ClearAt: req.ClearAt,
AccountID: user.ID,
}
if status, err := services.NewStatus(user.ID, status); err != nil {
2024-06-26 12:05:28 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
2024-10-14 14:23:51 +00:00
services.AddEvent(user.ID, "statuses.set", strconv.Itoa(int(status.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
2024-06-26 12:05:28 +00:00
return c.JSON(status)
}
}
2024-06-26 12:41:20 +00:00
func editStatus(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(*sec.UserInfo)
2024-06-26 12:41:20 +00:00
var req struct {
Type string `json:"type" validate:"required"`
Label string `json:"label" validate:"required"`
2024-06-26 17:01:47 +00:00
Attitude uint `json:"attitude"`
2024-06-26 12:41:20 +00:00
IsNoDisturb bool `json:"is_no_disturb"`
IsInvisible bool `json:"is_invisible"`
ClearAt *time.Time `json:"clear_at"`
}
if err := exts.BindAndValidate(c, &req); err != nil {
return err
}
status, err := services.GetStatus(user.ID)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "you must set a status first and then can edit it")
}
status.Type = req.Type
status.Label = req.Label
status.Attitude = models.StatusAttitude(req.Attitude)
status.IsNoDisturb = req.IsNoDisturb
status.IsInvisible = req.IsInvisible
status.ClearAt = req.ClearAt
if status, err := services.EditStatus(user.ID, status); err != nil {
2024-06-26 12:41:20 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
2024-10-14 14:23:51 +00:00
services.AddEvent(user.ID, "statuses.edit", strconv.Itoa(int(status.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
2024-06-26 12:41:20 +00:00
return c.JSON(status)
}
}
func clearStatus(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(*sec.UserInfo)
2024-06-26 12:41:20 +00:00
if err := services.ClearStatus(user.ID); err != nil {
2024-06-26 12:41:20 +00:00
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
2024-10-14 14:23:51 +00:00
} else {
services.AddEvent(user.ID, "statuses.clear", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
2024-06-26 12:41:20 +00:00
}
return c.SendStatus(fiber.StatusOK)
}