diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 63da3dc..7091d53 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,12 +4,11 @@
-
+
-
+
-
-
+
@@ -155,7 +154,6 @@
-
@@ -180,7 +178,8 @@
-
+
+
true
diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go
index d1be88b..0e97601 100644
--- a/pkg/internal/server/api/index.go
+++ b/pkg/internal/server/api/index.go
@@ -36,6 +36,8 @@ func MapAPIs(app *fiber.App) {
me.Post("/confirm", doRegisterConfirm)
me.Post("/status", setStatus)
+ me.Put("/status", editStatus)
+ me.Delete("/status", clearStatus)
friends := me.Group("/friends").Name("Friends")
{
diff --git a/pkg/internal/server/api/statuses_api.go b/pkg/internal/server/api/statuses_api.go
index 5649d89..e503046 100644
--- a/pkg/internal/server/api/statuses_api.go
+++ b/pkg/internal/server/api/statuses_api.go
@@ -68,3 +68,54 @@ func setStatus(c *fiber.Ctx) error {
return c.JSON(status)
}
}
+
+func editStatus(c *fiber.Ctx) error {
+ user := c.Locals("user").(models.Account)
+ if err := exts.EnsureAuthenticated(c); err != nil {
+ return err
+ }
+
+ var req struct {
+ Type string `json:"type" validate:"required"`
+ Label string `json:"label" validate:"required"`
+ Attitude uint `json:"attitude" validate:"required"`
+ 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, status); err != nil {
+ return fiber.NewError(fiber.StatusBadRequest, err.Error())
+ } else {
+ return c.JSON(status)
+ }
+}
+
+func clearStatus(c *fiber.Ctx) error {
+ user := c.Locals("user").(models.Account)
+ if err := exts.EnsureAuthenticated(c); err != nil {
+ return err
+ }
+
+ if err := services.ClearStatus(user); err != nil {
+ return fiber.NewError(fiber.StatusInternalServerError, err.Error())
+ }
+
+ return c.SendStatus(fiber.StatusOK)
+}
diff --git a/pkg/internal/services/statuses.go b/pkg/internal/services/statuses.go
index f860d9b..e843aa9 100644
--- a/pkg/internal/services/statuses.go
+++ b/pkg/internal/services/statuses.go
@@ -4,20 +4,12 @@ import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
+ "github.com/samber/lo"
"time"
)
var statusCache = make(map[uint]models.Status)
-func NewStatus(user models.Account, status models.Status) (models.Status, error) {
- if err := database.C.Save(&status).Error; err != nil {
- return status, err
- } else {
- statusCache[user.ID] = status
- }
- return status, nil
-}
-
func GetStatus(uid uint) (models.Status, error) {
if status, ok := statusCache[uid]; ok {
if status.ClearAt != nil && status.ClearAt.Before(time.Now()) {
@@ -61,3 +53,31 @@ func GetStatusOnline(uid uint) error {
return fmt.Errorf("offline")
}
}
+
+func NewStatus(user models.Account, status models.Status) (models.Status, error) {
+ if err := database.C.Save(&status).Error; err != nil {
+ return status, err
+ } else {
+ statusCache[user.ID] = status
+ }
+ return status, nil
+}
+
+func EditStatus(user models.Account, status models.Status) (models.Status, error) {
+ if err := database.C.Save(&status).Error; err != nil {
+ return status, err
+ } else {
+ statusCache[user.ID] = status
+ }
+ return status, nil
+}
+
+func ClearStatus(user models.Account) error {
+ if err := database.C.
+ Where("account_id = ?", user.ID).
+ Updates(models.Status{ClearAt: lo.ToPtr(time.Now())}).Error; err != nil {
+ return err
+ }
+
+ return nil
+}