54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
)
|
|
|
|
const adminUIDKeyConfigGroup = "im"
|
|
|
|
var adminUIDKeyConfigKeys = []string{"uid_encryption_key", "UID_ENCRYPTION_KEY", "uid_key"}
|
|
|
|
func storeConfigWithAdminUIDKey(raw json.RawMessage, client *adminInternalClient) (json.RawMessage, bool, error) {
|
|
if client == nil {
|
|
return raw, false, nil
|
|
}
|
|
values, err := client.ThirdPartyConfig(adminUIDKeyConfigGroup, adminUIDKeyConfigKeys...)
|
|
if err != nil {
|
|
return raw, false, err
|
|
}
|
|
uidKey := firstAdminUIDKeyValue(values)
|
|
if uidKey == "" {
|
|
return raw, false, nil
|
|
}
|
|
merged, err := mergeStoreUIDKey(raw, uidKey)
|
|
if err != nil {
|
|
return raw, false, err
|
|
}
|
|
return merged, true, nil
|
|
}
|
|
|
|
func firstAdminUIDKeyValue(values map[string]string) string {
|
|
for _, key := range adminUIDKeyConfigKeys {
|
|
if value := strings.TrimSpace(values[key]); value != "" {
|
|
return value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func mergeStoreUIDKey(raw json.RawMessage, uidKey string) (json.RawMessage, error) {
|
|
config := map[string]json.RawMessage{}
|
|
if len(raw) > 0 && string(raw) != "null" {
|
|
if err := json.Unmarshal(raw, &config); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
encoded, err := json.Marshal(strings.TrimSpace(uidKey))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
config["uid_key"] = encoded
|
|
return json.Marshal(config)
|
|
}
|