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

     1  package engine
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/keybase/client/go/libkb"
     7  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
     8  )
     9  
    10  type UserBlocksSet struct {
    11  	keybase1.SetUserBlocksArg
    12  	libkb.Contextified
    13  
    14  	uids []keybase1.UID
    15  }
    16  
    17  func NewUserBlocksSet(g *libkb.GlobalContext, args keybase1.SetUserBlocksArg) *UserBlocksSet {
    18  	return &UserBlocksSet{
    19  		SetUserBlocksArg: args,
    20  		Contextified:     libkb.NewContextified(g),
    21  	}
    22  }
    23  
    24  // Name is the unique engine name.
    25  func (e *UserBlocksSet) Name() string {
    26  	return "UserBlocksSet"
    27  }
    28  
    29  // GetPrereqs returns the engine prereqs.
    30  func (e *UserBlocksSet) Prereqs() Prereqs {
    31  	return Prereqs{}
    32  }
    33  
    34  // RequiredUIs returns the required UIs.
    35  func (e *UserBlocksSet) RequiredUIs() []libkb.UIKind {
    36  	return []libkb.UIKind{}
    37  }
    38  
    39  // SubConsumers returns the other UI consumers for this engine.
    40  func (e *UserBlocksSet) SubConsumers() []libkb.UIConsumer {
    41  	return nil
    42  }
    43  
    44  // Run starts the engine.
    45  func (e *UserBlocksSet) Run(mctx libkb.MetaContext) (err error) {
    46  	defer mctx.Trace(
    47  		fmt.Sprintf("UserBlocksSet#Run(len=%d)", len(e.Blocks)),
    48  		&err)()
    49  
    50  	type setBlockArg struct {
    51  		BlockUID string `json:"block_uid"`
    52  		Chat     *bool  `json:"chat,omitempty"`
    53  		Follow   *bool  `json:"follow,omitempty"`
    54  	}
    55  
    56  	for _, block := range e.Blocks {
    57  		mctx.Debug("SetUserBlocks: adding block: %+v", block)
    58  	}
    59  	payloadBlocks := make([]setBlockArg, len(e.Blocks))
    60  	e.uids = make([]keybase1.UID, len(e.Blocks))
    61  	for i, v := range e.Blocks {
    62  		uid := libkb.GetUIDByUsername(mctx.G(), v.Username)
    63  		payloadBlocks[i] = setBlockArg{
    64  			BlockUID: uid.String(),
    65  			Chat:     v.SetChatBlock,
    66  			Follow:   v.SetFollowBlock,
    67  		}
    68  		e.uids[i] = uid
    69  	}
    70  
    71  	payload := make(libkb.JSONPayload)
    72  	payload["blocks"] = payloadBlocks
    73  
    74  	apiArg := libkb.APIArg{
    75  		Endpoint:    "user/set_blocks",
    76  		JSONPayload: payload,
    77  		SessionType: libkb.APISessionTypeREQUIRED,
    78  	}
    79  
    80  	_, err = mctx.G().API.Post(mctx, apiArg)
    81  	return err
    82  }
    83  
    84  func (e *UserBlocksSet) UIDs() []keybase1.UID {
    85  	return e.uids
    86  }