github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/engine/devlist.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  	"sort"
     8  
     9  	"github.com/keybase/client/go/libkb"
    10  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    11  )
    12  
    13  // DevList is an engine that gets a list of all the user's
    14  // devices.
    15  type DevList struct {
    16  	devices []keybase1.Device
    17  	libkb.Contextified
    18  }
    19  
    20  // NewDevList creates a DevList engine.
    21  func NewDevList(g *libkb.GlobalContext) *DevList {
    22  	return &DevList{
    23  		Contextified: libkb.NewContextified(g),
    24  	}
    25  }
    26  
    27  func (d *DevList) Name() string {
    28  	return "DevList"
    29  }
    30  
    31  func (d *DevList) Prereqs() Prereqs {
    32  	return Prereqs{Device: true}
    33  }
    34  
    35  func (d *DevList) RequiredUIs() []libkb.UIKind {
    36  	return []libkb.UIKind{libkb.LogUIKind}
    37  }
    38  
    39  func (d *DevList) SubConsumers() []libkb.UIConsumer {
    40  	return nil
    41  }
    42  
    43  // Run starts the engine.
    44  func (d *DevList) Run(m libkb.MetaContext) (err error) {
    45  	defer m.Trace("DevList#Run", &err)()
    46  
    47  	var devs libkb.DeviceKeyMap
    48  	var ss *libkb.SecretSyncer
    49  
    50  	ss, err = m.ActiveDevice().SyncSecretsForce(m)
    51  	if err != nil {
    52  		return err
    53  	}
    54  	devs, err = ss.ActiveDevices(libkb.AllDeviceTypes)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	var pdevs []keybase1.Device
    60  	for k, v := range devs {
    61  		pdevs = append(pdevs, keybase1.Device{
    62  			Type:         v.Type,
    63  			Name:         v.Display(),
    64  			DeviceID:     k,
    65  			CTime:        keybase1.TimeFromSeconds(v.CTime),
    66  			MTime:        keybase1.TimeFromSeconds(v.MTime),
    67  			LastUsedTime: keybase1.TimeFromSeconds(v.LastUsedTime),
    68  		})
    69  	}
    70  	sort.Sort(dname(pdevs))
    71  	d.devices = pdevs
    72  
    73  	return nil
    74  }
    75  
    76  // List returns the devices for a user.
    77  func (d *DevList) List() []keybase1.Device {
    78  	return d.devices
    79  }
    80  
    81  type dname []keybase1.Device
    82  
    83  func (d dname) Len() int           { return len(d) }
    84  func (d dname) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }
    85  func (d dname) Less(i, j int) bool { return d[i].Name < d[j].Name }