Passport/pkg/internal/services/statuses.go

106 lines
2.4 KiB
Go
Raw Normal View History

2024-06-26 12:05:28 +00:00
package services
import (
"context"
2024-06-26 12:05:28 +00:00
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
2024-07-10 09:38:39 +00:00
"time"
2024-06-26 12:05:28 +00:00
"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
)
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
}
2024-07-10 09:38:39 +00:00
func GetUserOnline(uid uint) bool {
pc := proto.NewStreamControllerClient(gap.H.GetDealerGrpcConn())
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := pc.CountStreamConnection(ctx, &proto.CountConnectionRequest{
UserId: uint64(uid),
})
if err != nil {
return false
}
return resp.Count > 0
2024-07-10 09:38:39 +00:00
}
2024-06-26 12:05:28 +00:00
func GetStatusDisturbable(uid uint) error {
status, err := GetStatus(uid)
2024-07-10 09:38:39 +00:00
isOnline := GetUserOnline(uid)
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 {
return nil
2024-06-26 12:05:28 +00:00
}
}
func GetStatusOnline(uid uint) error {
status, err := GetStatus(uid)
isOnline := GetUserOnline(uid)
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
}