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"
|
2024-06-26 12:41:20 +00:00
|
|
|
"github.com/samber/lo"
|
2024-06-26 12:05:28 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var statusCache = make(map[uint]models.Status)
|
|
|
|
|
|
|
|
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).
|
2024-06-26 17:29:51 +00:00
|
|
|
Where("clear_at > ?", time.Now()).
|
2024-06-26 12:05:28 +00:00
|
|
|
First(&status).Error; err != nil {
|
|
|
|
return status, err
|
|
|
|
} else {
|
|
|
|
statusCache[uid] = status
|
|
|
|
}
|
|
|
|
return status, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetStatusDisturbable(uid uint) error {
|
|
|
|
status, err := GetStatus(uid)
|
2024-06-26 12:13:34 +00:00
|
|
|
isOnline := wsConn[uid] != nil && len(wsConn[uid]) > 0
|
2024-06-26 12:05:28 +00:00
|
|
|
if isOnline && err != nil {
|
|
|
|
return nil
|
|
|
|
} else if err == nil && status.IsNoDisturb {
|
|
|
|
return fmt.Errorf("do not disturb")
|
2024-06-27 04:20:42 +00:00
|
|
|
} else if !isOnline {
|
2024-06-26 12:05:28 +00:00
|
|
|
return fmt.Errorf("offline")
|
2024-06-27 04:20:42 +00:00
|
|
|
} else {
|
|
|
|
return nil
|
2024-06-26 12:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetStatusOnline(uid uint) error {
|
|
|
|
status, err := GetStatus(uid)
|
2024-06-26 12:13:34 +00:00
|
|
|
isOnline := wsConn[uid] != nil && len(wsConn[uid]) > 0
|
2024-06-26 12:05:28 +00:00
|
|
|
if isOnline && err != nil {
|
|
|
|
return nil
|
|
|
|
} else if err == nil && status.IsInvisible {
|
|
|
|
return fmt.Errorf("invisible")
|
2024-06-26 17:10:57 +00:00
|
|
|
} else if !isOnline {
|
2024-06-26 12:05:28 +00:00
|
|
|
return fmt.Errorf("offline")
|
2024-06-26 17:10:57 +00:00
|
|
|
} else {
|
|
|
|
return nil
|
2024-06-26 12:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-26 12:41:20 +00:00
|
|
|
|
|
|
|
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).
|
2024-06-26 17:29:51 +00:00
|
|
|
Where("clear_at > ?", time.Now()).
|
2024-06-26 12:41:20 +00:00
|
|
|
Updates(models.Status{ClearAt: lo.ToPtr(time.Now())}).Error; err != nil {
|
|
|
|
return err
|
2024-06-26 17:10:57 +00:00
|
|
|
} else {
|
|
|
|
delete(statusCache, user.ID)
|
2024-06-26 12:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|