🐛 Fix destination loading

This commit is contained in:
LittleSheep 2024-12-29 00:03:00 +08:00
parent cd0141f5b1
commit 3a00a06541
4 changed files with 16 additions and 13 deletions

View File

@ -2,14 +2,14 @@ package api
import ( import (
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/spf13/viper"
) )
func listDestination(c *fiber.Ctx) error { func listDestination(c *fiber.Ctx) error {
var destinations []models.BaseDestination var destinations []models.BaseDestination
for _, value := range viper.GetStringSlice("destinations") { for _, value := range services.DestinationsByIndex {
var parsed models.BaseDestination var parsed models.BaseDestination
raw, _ := jsoniter.Marshal(value) raw, _ := jsoniter.Marshal(value)
_ = jsoniter.Unmarshal(raw, &parsed) _ = jsoniter.Unmarshal(raw, &parsed)

View File

@ -64,7 +64,7 @@ func CreateBoost(user *sec.UserInfo, source models.Attachment, destination int)
AccountID: user.ID, AccountID: user.ID,
} }
if des, ok := destinationsByIndex[destination]; !ok { if des, ok := DestinationsByIndex[destination]; !ok {
return boost, fmt.Errorf("invalid destination: %d", destination) return boost, fmt.Errorf("invalid destination: %d", destination)
} else { } else {
var destBase models.BaseDestination var destBase models.BaseDestination

View File

@ -1,6 +1,8 @@
package services package services
import ( import (
"fmt"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -13,15 +15,16 @@ type destinationMapping struct {
} }
var ( var (
destinationsByIndex = make(map[int]destinationMapping) DestinationsByIndex = make(map[int]destinationMapping)
destinationsByRegion = make(map[string]destinationMapping) DestinationsByRegion = make(map[string]destinationMapping)
) )
func BuildDestinationMapping() { func BuildDestinationMapping() {
count := 0 count := len(viper.GetStringSlice("destinations"))
for idx, value := range viper.GetStringSlice("destinations") { for idx := 0; idx < count; idx++ {
destMap := viper.GetStringMap(fmt.Sprintf("destinations.%d", idx))
var parsed models.BaseDestination var parsed models.BaseDestination
raw, _ := jsoniter.Marshal(value) raw, _ := jsoniter.Marshal(destMap)
_ = jsoniter.Unmarshal(raw, &parsed) _ = jsoniter.Unmarshal(raw, &parsed)
mapping := destinationMapping{ mapping := destinationMapping{
@ -30,8 +33,8 @@ func BuildDestinationMapping() {
} }
if len(parsed.Region) > 0 { if len(parsed.Region) > 0 {
destinationsByIndex[idx] = mapping DestinationsByIndex[idx] = mapping
destinationsByRegion[parsed.Region] = mapping DestinationsByRegion[parsed.Region] = mapping
} }
count++ count++

View File

@ -77,7 +77,7 @@ func OpenAttachmentByRID(rid string, region ...string) (url string, mimetype str
var rawDest []byte var rawDest []byte
if len(region) > 0 { if len(region) > 0 {
if des, ok := destinationsByRegion[region[0]]; ok { if des, ok := DestinationsByRegion[region[0]]; ok {
for _, boost := range result.Boosts { for _, boost := range result.Boosts {
if boost.Destination == des.Index { if boost.Destination == des.Index {
rawDest = des.Raw rawDest = des.Raw
@ -89,12 +89,12 @@ func OpenAttachmentByRID(rid string, region ...string) (url string, mimetype str
if rawDest == nil { if rawDest == nil {
if len(result.Boosts) > 0 { if len(result.Boosts) > 0 {
randomIdx := rand.IntN(len(result.Boosts)) randomIdx := rand.IntN(len(result.Boosts))
if des, ok := destinationsByIndex[randomIdx]; ok { if des, ok := DestinationsByIndex[randomIdx]; ok {
rawDest = des.Raw rawDest = des.Raw
json.Unmarshal(rawDest, &dest) json.Unmarshal(rawDest, &dest)
} }
} else { } else {
if des, ok := destinationsByIndex[result.Attachment.Destination]; ok { if des, ok := DestinationsByIndex[result.Attachment.Destination]; ok {
rawDest = des.Raw rawDest = des.Raw
json.Unmarshal(rawDest, &dest) json.Unmarshal(rawDest, &dest)
} }