Passport/pkg/internal/services/statuses.go

135 lines
3.3 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"
2024-09-22 05:13:05 +00:00
"time"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
2024-09-22 05:13:05 +00:00
localCache "git.solsynth.dev/hydrogen/passport/pkg/internal/cache"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
2024-07-10 09:38:39 +00:00
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-09-22 05:13:05 +00:00
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
"github.com/eko/gocache/lib/v4/store"
2024-06-26 12:41:20 +00:00
"github.com/samber/lo"
2024-06-26 12:05:28 +00:00
)
2024-09-22 05:13:05 +00:00
func GetStatusCacheKey(uid uint) string {
return fmt.Sprintf("user-status#%d", uid)
}
2024-06-26 12:05:28 +00:00
func GetStatus(uid uint) (models.Status, error) {
2024-09-22 05:13:05 +00:00
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
if val, err := marshal.Get(contx, GetStatusCacheKey(uid), new(models.Status)); err == nil {
status := val.(models.Status)
2024-06-26 12:08:18 +00:00
if status.ClearAt != nil && status.ClearAt.Before(time.Now()) {
2024-09-22 05:13:05 +00:00
marshal.Delete(contx, GetStatusCacheKey(uid))
2024-06-26 12:08:18 +00:00
} 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 {
2024-09-22 05:13:05 +00:00
CacheUserStatus(uid, status)
2024-06-26 12:05:28 +00:00
}
return status, nil
}
2024-09-22 05:13:05 +00:00
func CacheUserStatus(uid uint, status models.Status) {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
marshal.Set(
contx,
GetStatusCacheKey(uid),
status,
store.WithTags([]string{"user-status", fmt.Sprintf("user#%d", uid)}),
)
}
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 {
2024-09-22 05:13:05 +00:00
CacheUserStatus(user.ID, status)
2024-06-26 12:41:20 +00:00
}
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 {
2024-09-22 05:13:05 +00:00
CacheUserStatus(user.ID, status)
2024-06-26 12:41:20 +00:00
}
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 {
2024-09-22 05:13:05 +00:00
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
marshal.Delete(contx, GetStatusCacheKey(user.ID))
2024-06-26 12:41:20 +00:00
}
return nil
}