2024-06-26 12:05:28 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-07-14 16:01:17 +00:00
|
|
|
"context"
|
2024-06-26 12:05:28 +00:00
|
|
|
"fmt"
|
2024-10-26 16:06:23 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-09-22 05:13:05 +00:00
|
|
|
"time"
|
|
|
|
|
2024-10-31 12:38:50 +00:00
|
|
|
localCache "git.solsynth.dev/hypernet/passport/pkg/internal/cache"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/gap"
|
2024-07-10 09:38:39 +00:00
|
|
|
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
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 {
|
2024-10-26 16:06:23 +00:00
|
|
|
pc := proto.NewStreamServiceClient(gap.Nx.GetNexusGrpcConn())
|
2024-07-14 16:01:17 +00:00
|
|
|
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)
|
2024-07-14 16:01:17 +00:00
|
|
|
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
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
func NewStatus(user models.Account, status models.Status) (models.Status, error) {
|
2024-06-26 12:41:20 +00:00
|
|
|
if err := database.C.Save(&status).Error; err != nil {
|
|
|
|
return status, err
|
|
|
|
} else {
|
2024-10-30 16:17:53 +00:00
|
|
|
CacheUserStatus(user.ID, status)
|
2024-06-26 12:41:20 +00:00
|
|
|
}
|
|
|
|
return status, nil
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
func EditStatus(user models.Account, status models.Status) (models.Status, error) {
|
2024-06-26 12:41:20 +00:00
|
|
|
if err := database.C.Save(&status).Error; err != nil {
|
|
|
|
return status, err
|
|
|
|
} else {
|
2024-10-30 16:17:53 +00:00
|
|
|
CacheUserStatus(user.ID, status)
|
2024-06-26 12:41:20 +00:00
|
|
|
}
|
|
|
|
return status, nil
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
func ClearStatus(user models.Account) error {
|
2024-06-26 12:41:20 +00:00
|
|
|
if err := database.C.
|
2024-10-30 16:17:53 +00:00
|
|
|
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()
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
marshal.Delete(contx, GetStatusCacheKey(user.ID))
|
2024-06-26 12:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|