Edit, delete current status

This commit is contained in:
LittleSheep 2024-06-26 20:41:20 +08:00
parent b1f6cf8f6e
commit 9519497887
4 changed files with 87 additions and 15 deletions

11
.idea/workspace.xml generated
View File

@ -4,12 +4,11 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix online condition"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Last seen at">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/profiles.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/profiles.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/server/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/index.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/statuses_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/statuses_api.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/server/api/statuses_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/statuses_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/accounts.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/services/statuses.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/statuses.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/connections.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/connections.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -155,7 +154,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Drop direct connection and uses consul" />
<MESSAGE value=":technologist: Add the server side Hyper SDK" /> <MESSAGE value=":technologist: Add the server side Hyper SDK" />
<MESSAGE value=":recycle: Improve code structure and much easier to read&#10;:bug: Fix auth middleware" /> <MESSAGE value=":recycle: Improve code structure and much easier to read&#10;:bug: Fix auth middleware" />
<MESSAGE value=":bug: FIx cannot resolve service" /> <MESSAGE value=":bug: FIx cannot resolve service" />
@ -180,7 +178,8 @@
<MESSAGE value=":sparkles: Status system" /> <MESSAGE value=":sparkles: Status system" />
<MESSAGE value=":bug: Fix status expired in cache" /> <MESSAGE value=":bug: Fix status expired in cache" />
<MESSAGE value=":bug: Fix online condition" /> <MESSAGE value=":bug: Fix online condition" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix online condition" /> <MESSAGE value=":sparkles: Last seen at" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Last seen at" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<settings-migrated>true</settings-migrated> <settings-migrated>true</settings-migrated>

View File

@ -36,6 +36,8 @@ func MapAPIs(app *fiber.App) {
me.Post("/confirm", doRegisterConfirm) me.Post("/confirm", doRegisterConfirm)
me.Post("/status", setStatus) me.Post("/status", setStatus)
me.Put("/status", editStatus)
me.Delete("/status", clearStatus)
friends := me.Group("/friends").Name("Friends") friends := me.Group("/friends").Name("Friends")
{ {

View File

@ -68,3 +68,54 @@ func setStatus(c *fiber.Ctx) error {
return c.JSON(status) 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)
}

View File

@ -4,20 +4,12 @@ import (
"fmt" "fmt"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/samber/lo"
"time" "time"
) )
var statusCache = make(map[uint]models.Status) 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) { func GetStatus(uid uint) (models.Status, error) {
if status, ok := statusCache[uid]; ok { if status, ok := statusCache[uid]; ok {
if status.ClearAt != nil && status.ClearAt.Before(time.Now()) { if status.ClearAt != nil && status.ClearAt.Before(time.Now()) {
@ -61,3 +53,31 @@ func GetStatusOnline(uid uint) error {
return fmt.Errorf("offline") 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
}