Passport/pkg/internal/services/statuses.go

64 lines
1.5 KiB
Go
Raw Normal View History

2024-06-26 12:05:28 +00:00
package services
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"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 {
2024-06-26 12:08:18 +00:00
if status.ClearAt != nil && status.ClearAt.Before(time.Now()) {
delete(statusCache, uid)
} else {
return status, nil
}
2024-06-26 12:05:28 +00:00
}
var status models.Status
if err := database.C.
Where("account_id = ?", uid).
Where("clear_at < ?", time.Now()).
First(&status).Error; err != nil {
return status, err
} else {
statusCache[uid] = status
}
return status, nil
}
func GetStatusDisturbable(uid uint) error {
status, err := GetStatus(uid)
isOnline := wsConn[uid] == nil || len(wsConn[uid]) < 0
if isOnline && err != nil {
return nil
} else if err == nil && status.IsNoDisturb {
return fmt.Errorf("do not disturb")
} else {
return fmt.Errorf("offline")
}
}
func GetStatusOnline(uid uint) error {
status, err := GetStatus(uid)
isOnline := wsConn[uid] == nil || len(wsConn[uid]) < 0
if isOnline && err != nil {
return nil
} else if err == nil && status.IsInvisible {
return fmt.Errorf("invisible")
} else {
return fmt.Errorf("offline")
}
}