github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/engine/user_config.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package engine
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/keybase/client/go/libkb"
    11  )
    12  
    13  type UserConfigEngineArg struct {
    14  	Key   string
    15  	Value string
    16  }
    17  
    18  type UserConfigEngine struct {
    19  	libkb.Contextified
    20  	arg *UserConfigEngineArg
    21  }
    22  
    23  func NewUserConfigEngine(g *libkb.GlobalContext, arg *UserConfigEngineArg) *UserConfigEngine {
    24  	return &UserConfigEngine{
    25  		arg:          arg,
    26  		Contextified: libkb.NewContextified(g),
    27  	}
    28  }
    29  
    30  func (e *UserConfigEngine) Name() string {
    31  	return "UserConfig"
    32  }
    33  
    34  func (e *UserConfigEngine) Prereqs() Prereqs {
    35  	return Prereqs{Device: true}
    36  }
    37  
    38  func (e *UserConfigEngine) RequiredUIs() []libkb.UIKind {
    39  	return []libkb.UIKind{}
    40  }
    41  
    42  func (e *UserConfigEngine) SubConsumers() []libkb.UIConsumer {
    43  	return nil
    44  }
    45  
    46  func (e *UserConfigEngine) Run(m libkb.MetaContext) (err error) {
    47  	keys := strings.SplitN(e.arg.Key, ".", 2)
    48  	if len(keys) < 2 {
    49  		return fmt.Errorf("Invalid key")
    50  	}
    51  
    52  	// Supports picture.source = "twitter" or "github".
    53  	// This maps to http endpoint image/set_preference.json?identify_src=100 (or 101).
    54  	// TODO We should clean this up.
    55  	if keys[0] == "picture" {
    56  		var key string
    57  		var value string
    58  		if keys[1] == "source" {
    59  			key = "identity_src"
    60  
    61  			switch e.arg.Value {
    62  			case "twitter":
    63  				value = "100"
    64  			case "github":
    65  				value = "101"
    66  			default:
    67  				return fmt.Errorf("Invalid picture.source")
    68  			}
    69  		}
    70  
    71  		var err error
    72  		_, err = m.G().API.Post(m, libkb.APIArg{
    73  			Endpoint:    "image/set_preference",
    74  			SessionType: libkb.APISessionTypeREQUIRED,
    75  			Args: libkb.HTTPArgs{
    76  				key: libkb.S{Val: value},
    77  			},
    78  		})
    79  		if err != nil {
    80  			return err
    81  		}
    82  	} else {
    83  		return fmt.Errorf("Invalid key")
    84  	}
    85  	return nil
    86  }