🐛 Fix destination loading
This commit is contained in:
		| @@ -2,14 +2,14 @@ package api | ||||
|  | ||||
| import ( | ||||
| 	"git.solsynth.dev/hypernet/paperclip/pkg/internal/models" | ||||
| 	"git.solsynth.dev/hypernet/paperclip/pkg/internal/services" | ||||
| 	"github.com/gofiber/fiber/v2" | ||||
| 	jsoniter "github.com/json-iterator/go" | ||||
| 	"github.com/spf13/viper" | ||||
| ) | ||||
|  | ||||
| func listDestination(c *fiber.Ctx) error { | ||||
| 	var destinations []models.BaseDestination | ||||
| 	for _, value := range viper.GetStringSlice("destinations") { | ||||
| 	for _, value := range services.DestinationsByIndex { | ||||
| 		var parsed models.BaseDestination | ||||
| 		raw, _ := jsoniter.Marshal(value) | ||||
| 		_ = jsoniter.Unmarshal(raw, &parsed) | ||||
|   | ||||
| @@ -64,7 +64,7 @@ func CreateBoost(user *sec.UserInfo, source models.Attachment, destination int) | ||||
| 		AccountID:    user.ID, | ||||
| 	} | ||||
|  | ||||
| 	if des, ok := destinationsByIndex[destination]; !ok { | ||||
| 	if des, ok := DestinationsByIndex[destination]; !ok { | ||||
| 		return boost, fmt.Errorf("invalid destination: %d", destination) | ||||
| 	} else { | ||||
| 		var destBase models.BaseDestination | ||||
|   | ||||
| @@ -1,6 +1,8 @@ | ||||
| package services | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	"git.solsynth.dev/hypernet/paperclip/pkg/internal/models" | ||||
| 	jsoniter "github.com/json-iterator/go" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| @@ -13,15 +15,16 @@ type destinationMapping struct { | ||||
| } | ||||
|  | ||||
| var ( | ||||
| 	destinationsByIndex  = make(map[int]destinationMapping) | ||||
| 	destinationsByRegion = make(map[string]destinationMapping) | ||||
| 	DestinationsByIndex  = make(map[int]destinationMapping) | ||||
| 	DestinationsByRegion = make(map[string]destinationMapping) | ||||
| ) | ||||
|  | ||||
| func BuildDestinationMapping() { | ||||
| 	count := 0 | ||||
| 	for idx, value := range viper.GetStringSlice("destinations") { | ||||
| 	count := len(viper.GetStringSlice("destinations")) | ||||
| 	for idx := 0; idx < count; idx++ { | ||||
| 		destMap := viper.GetStringMap(fmt.Sprintf("destinations.%d", idx)) | ||||
| 		var parsed models.BaseDestination | ||||
| 		raw, _ := jsoniter.Marshal(value) | ||||
| 		raw, _ := jsoniter.Marshal(destMap) | ||||
| 		_ = jsoniter.Unmarshal(raw, &parsed) | ||||
|  | ||||
| 		mapping := destinationMapping{ | ||||
| @@ -30,8 +33,8 @@ func BuildDestinationMapping() { | ||||
| 		} | ||||
|  | ||||
| 		if len(parsed.Region) > 0 { | ||||
| 			destinationsByIndex[idx] = mapping | ||||
| 			destinationsByRegion[parsed.Region] = mapping | ||||
| 			DestinationsByIndex[idx] = mapping | ||||
| 			DestinationsByRegion[parsed.Region] = mapping | ||||
| 		} | ||||
|  | ||||
| 		count++ | ||||
|   | ||||
| @@ -77,7 +77,7 @@ func OpenAttachmentByRID(rid string, region ...string) (url string, mimetype str | ||||
| 	var rawDest []byte | ||||
|  | ||||
| 	if len(region) > 0 { | ||||
| 		if des, ok := destinationsByRegion[region[0]]; ok { | ||||
| 		if des, ok := DestinationsByRegion[region[0]]; ok { | ||||
| 			for _, boost := range result.Boosts { | ||||
| 				if boost.Destination == des.Index { | ||||
| 					rawDest = des.Raw | ||||
| @@ -89,12 +89,12 @@ func OpenAttachmentByRID(rid string, region ...string) (url string, mimetype str | ||||
| 	if rawDest == nil { | ||||
| 		if len(result.Boosts) > 0 { | ||||
| 			randomIdx := rand.IntN(len(result.Boosts)) | ||||
| 			if des, ok := destinationsByIndex[randomIdx]; ok { | ||||
| 			if des, ok := DestinationsByIndex[randomIdx]; ok { | ||||
| 				rawDest = des.Raw | ||||
| 				json.Unmarshal(rawDest, &dest) | ||||
| 			} | ||||
| 		} else { | ||||
| 			if des, ok := destinationsByIndex[result.Attachment.Destination]; ok { | ||||
| 			if des, ok := DestinationsByIndex[result.Attachment.Destination]; ok { | ||||
| 				rawDest = des.Raw | ||||
| 				json.Unmarshal(rawDest, &dest) | ||||
| 			} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user