github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/engine/favorite_list.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  	"github.com/keybase/client/go/libkb"
     8  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
     9  )
    10  
    11  // FavoriteList is an engine.
    12  type FavoriteList struct {
    13  	libkb.Contextified
    14  	result FavoritesAPIResult
    15  }
    16  
    17  // NewFavoriteList creates a FavoriteList engine.
    18  func NewFavoriteList(g *libkb.GlobalContext) *FavoriteList {
    19  	return &FavoriteList{
    20  		Contextified: libkb.NewContextified(g),
    21  	}
    22  }
    23  
    24  // Name is the unique engine name.
    25  func (e *FavoriteList) Name() string {
    26  	return "FavoriteList"
    27  }
    28  
    29  // GetPrereqs returns the engine prereqs.
    30  func (e *FavoriteList) Prereqs() Prereqs {
    31  	return Prereqs{
    32  		Device: true,
    33  	}
    34  }
    35  
    36  // RequiredUIs returns the required UIs.
    37  func (e *FavoriteList) RequiredUIs() []libkb.UIKind {
    38  	return []libkb.UIKind{}
    39  }
    40  
    41  // SubConsumers returns the other UI consumers for this engine.
    42  func (e *FavoriteList) SubConsumers() []libkb.UIConsumer {
    43  	return nil
    44  }
    45  
    46  type FavoritesAPIResult struct {
    47  	Status    libkb.AppStatus   `json:"status"`
    48  	Favorites []keybase1.Folder `json:"favorites"`
    49  	Ignored   []keybase1.Folder `json:"ignored"`
    50  	New       []keybase1.Folder `json:"new"`
    51  }
    52  
    53  func (f *FavoritesAPIResult) GetAppStatus() *libkb.AppStatus {
    54  	return &f.Status
    55  }
    56  
    57  func (e *FavoriteList) cacheFolder(m libkb.MetaContext, folder keybase1.Folder) {
    58  	if folder.FolderType != keybase1.FolderType_TEAM || folder.TeamID == nil || folder.TeamID.IsNil() {
    59  		return
    60  	}
    61  	name, err := keybase1.TeamNameFromString(folder.Name)
    62  	if err != nil {
    63  		m.Debug("cannot cache folder %+v: %s", folder, err)
    64  		return
    65  	}
    66  	m.G().Resolver.CacheTeamResolution(m, *folder.TeamID, name)
    67  }
    68  
    69  func (e *FavoriteList) cacheFolders(m libkb.MetaContext, folders []keybase1.Folder) {
    70  	for _, f := range folders {
    71  		e.cacheFolder(m, f)
    72  	}
    73  }
    74  
    75  // Run starts the engine.
    76  func (e *FavoriteList) Run(m libkb.MetaContext) error {
    77  	arg := libkb.NewRetryAPIArg("kbfs/favorite/list")
    78  	arg.SessionType = libkb.APISessionTypeREQUIRED
    79  	err := m.G().API.GetDecode(m, arg, &e.result)
    80  	if err != nil {
    81  		return err
    82  	}
    83  	e.cacheFolders(m, e.result.Favorites)
    84  	e.cacheFolders(m, e.result.New)
    85  	return nil
    86  }
    87  
    88  // Favorites returns the list of favorites that Run generated.
    89  func (e *FavoriteList) Result() keybase1.FavoritesResult {
    90  	return keybase1.FavoritesResult{
    91  		FavoriteFolders: e.result.Favorites,
    92  		IgnoredFolders:  e.result.Ignored,
    93  		NewFolders:      e.result.New,
    94  	}
    95  }