github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/teams/storage/hidden.go (about) 1 package storage 2 3 import ( 4 "fmt" 5 "github.com/keybase/client/go/libkb" 6 "github.com/keybase/client/go/protocol/keybase1" 7 ) 8 9 // HiddenStorage stores Hidden state to disk or memory. 10 type HiddenStorage struct { 11 *storageGeneric 12 } 13 14 // Increment to invalidate the disk cache. 15 const hiddenDiskStorageVersion = 1 16 const hiddenMemCacheLRUSize = 200 17 18 type hiddenDiskStorageItem struct { 19 Version int `codec:"V"` 20 State *keybase1.HiddenTeamChain `codec:"S"` 21 } 22 23 var _ teamDataGeneric = (*keybase1.HiddenTeamChain)(nil) 24 var _ diskItemGeneric = (*hiddenDiskStorageItem)(nil) 25 26 func (d *hiddenDiskStorageItem) version() int { 27 return d.Version 28 } 29 func (d *hiddenDiskStorageItem) value() teamDataGeneric { 30 return d.State 31 } 32 func (d *hiddenDiskStorageItem) setVersion(i int) { 33 d.Version = i 34 } 35 func (d *hiddenDiskStorageItem) setValue(v teamDataGeneric) error { 36 typed, ok := v.(*keybase1.HiddenTeamChain) 37 if !ok { 38 return fmt.Errorf("teams/storage.Hidden#Put: Bad object for setValue; got type %T", v) 39 } 40 d.State = typed 41 return nil 42 } 43 44 func NewHiddenStorage(g *libkb.GlobalContext) *HiddenStorage { 45 s := newStorageGeneric(g, hiddenMemCacheLRUSize, hiddenDiskStorageVersion, libkb.DBHiddenChainStorage, libkb.EncryptionReasonTeamsHiddenLocalStorage, "hidden", func() diskItemGeneric { return &hiddenDiskStorageItem{} }) 46 return &HiddenStorage{s} 47 } 48 49 func (s *HiddenStorage) Put(mctx libkb.MetaContext, state *keybase1.HiddenTeamChain) { 50 s.storageGeneric.put(mctx, state) 51 } 52 53 // Can return nil. 54 func (s *HiddenStorage) Get(mctx libkb.MetaContext, teamID keybase1.TeamID, public bool) (state *keybase1.HiddenTeamChain, frozen bool, tombstoned bool) { 55 vp := s.storageGeneric.get(mctx, teamID, public) 56 if vp == nil { 57 return nil, false, false 58 } 59 ret, ok := vp.(*keybase1.HiddenTeamChain) 60 if !ok { 61 mctx.Debug("teams.storage/Hidden#Get cast error: %T is wrong type", vp) 62 } 63 if ret.Frozen { 64 mctx.Debug("returning frozen hidden team data") 65 } 66 if ret.Tombstoned { 67 mctx.Debug("returning tombstoned hidden team data") 68 } 69 s.upgrade(mctx, ret) 70 return ret, ret.Frozen, ret.Tombstoned 71 } 72 73 func (s *HiddenStorage) upgrade(mctx libkb.MetaContext, ret *keybase1.HiddenTeamChain) { 74 if ret == nil { 75 return 76 } 77 // We added LastFull after the fact, so go back and populate this field if it wasn't 78 // read in from the object. 79 if ret.Subversion < 1 { 80 mctx.Debug("HiddenStorage#upgrade: upgrading from 1.0 to 1.1 (running PopulateLastFull())") 81 ret.PopulateLastFull() 82 ret.Subversion = 1 83 } 84 }