-
Notifications
You must be signed in to change notification settings - Fork 137
/
sponsors.go
101 lines (90 loc) · 2.24 KB
/
sponsors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package instance
import (
"path/filepath"
"github.com/cozy/cozy-stack/pkg/config/config"
"github.com/go-viper/mapstructure/v2"
)
type AppLogos struct {
Light []LogoItem `json:"light,omitempty"`
Dark []LogoItem `json:"dark,omitempty"`
}
type LogoItem struct {
Src string `json:"src"`
Alt string `json:"alt"`
Type string `json:"type,omitempty"`
}
func (i *Instance) GetContextWithSponsorships() map[string]interface{} {
context, ok := i.SettingsContext()
if !ok {
context = map[string]interface{}{}
}
if len(i.Sponsorships) == 0 {
return context
}
// Avoid changing the global config
clone := map[string]interface{}{}
for k, v := range context {
clone[k] = v
}
context = clone
var logos map[string]AppLogos
if err := mapstructure.Decode(context["logos"], &logos); err != nil || logos == nil {
logos = make(map[string]AppLogos)
}
context["logos"] = logos
contexts := config.GetConfig().Contexts
if contexts == nil {
return context
}
for _, sponsor := range i.Sponsorships {
if sponsorCtx, ok := contexts[sponsor].(map[string]interface{}); ok {
addHomeLogosForSponsor(context, sponsorCtx, sponsor)
}
}
return context
}
func addHomeLogosForSponsor(context, sponsorContext map[string]interface{}, sponsor string) {
sponsorLogos, ok := sponsorContext["logos"].(map[string]interface{})
if !ok {
return
}
var newLogos AppLogos
if err := mapstructure.Decode(sponsorLogos["home"], &newLogos); err != nil {
return
}
contextLogos := context["logos"].(map[string]AppLogos)
homeLogos := contextLogos["home"]
for _, logo := range newLogos.Light {
if logo.Type == "main" {
continue
}
found := false
for _, item := range homeLogos.Light {
if filepath.Base(item.Src) == filepath.Base(logo.Src) {
found = true
}
}
if found {
continue
}
logo.Src = "/ext/" + sponsor + logo.Src
homeLogos.Light = append(homeLogos.Light, logo)
}
for _, logo := range newLogos.Dark {
if logo.Type == "main" {
continue
}
found := false
for _, item := range homeLogos.Dark {
if filepath.Base(item.Src) == filepath.Base(logo.Src) {
found = true
}
}
if found {
continue
}
logo.Src = "/ext/" + sponsor + logo.Src
homeLogos.Dark = append(homeLogos.Dark, logo)
}
contextLogos["home"] = homeLogos
}