Edit, delete current status

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

View File

@@ -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
}