github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/teams/storage/supports_hidden_flag.go (about) 1 package storage 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/keybase/client/go/libkb" 8 "github.com/keybase/client/go/protocol/keybase1" 9 ) 10 11 // SupportsHiddenFlagStorage stores the supports_hidden_chain flag to disk and memory. 12 type SupportsHiddenFlagStorage struct { 13 *storageGeneric 14 } 15 16 // Increment to invalidate the disk cache. 17 const supportsHiddenFlagDiskStorageVersion = 1 18 const supportsHiddenFlagCacheLRUSize = 5000 19 20 // HiddenChainSupportState describes whether a team supports the hidden chain or 21 // not. This information is fetched from the server and cached. 22 type HiddenChainSupportState struct { 23 TeamID keybase1.TeamID 24 State bool 25 CacheUntil time.Time 26 } 27 28 type supportsHiddenFlagDiskStorageItem struct { 29 Version int `codec:"V"` 30 State *HiddenChainSupportState `codec:"S"` 31 } 32 33 var _ teamDataGeneric = (*HiddenChainSupportState)(nil) 34 var _ diskItemGeneric = (*supportsHiddenFlagDiskStorageItem)(nil) 35 36 func (ss *HiddenChainSupportState) IsPublic() bool { 37 return ss.TeamID.IsPublic() 38 } 39 40 func (ss *HiddenChainSupportState) ID() keybase1.TeamID { 41 return ss.TeamID 42 } 43 44 func (d *supportsHiddenFlagDiskStorageItem) version() int { 45 return d.Version 46 } 47 func (d *supportsHiddenFlagDiskStorageItem) value() teamDataGeneric { 48 return d.State 49 } 50 func (d *supportsHiddenFlagDiskStorageItem) setVersion(i int) { 51 d.Version = i 52 } 53 54 func (d *supportsHiddenFlagDiskStorageItem) setValue(v teamDataGeneric) error { 55 typed, ok := v.(*HiddenChainSupportState) 56 if !ok { 57 return fmt.Errorf("teams/storage.supportsHiddenFlagDiskStorageItem#setValue: Bad object; got type %T", v) 58 } 59 d.State = typed 60 return nil 61 } 62 63 func NewSupportsHiddenFlagStorage(g *libkb.GlobalContext) *SupportsHiddenFlagStorage { 64 s := newStorageGeneric( 65 g, 66 supportsHiddenFlagCacheLRUSize, 67 supportsHiddenFlagDiskStorageVersion, 68 libkb.DBSupportsHiddenFlagStorage, 69 libkb.EncryptionReasonTeamsHiddenLocalStorage, 70 "hidden flag", 71 func() diskItemGeneric { return &supportsHiddenFlagDiskStorageItem{} }, 72 ) 73 return &SupportsHiddenFlagStorage{s} 74 } 75 76 func (s *SupportsHiddenFlagStorage) Put(mctx libkb.MetaContext, state *HiddenChainSupportState) { 77 s.storageGeneric.put(mctx, state) 78 } 79 80 // Can return nil. 81 func (s *SupportsHiddenFlagStorage) Get(mctx libkb.MetaContext, teamID keybase1.TeamID) (state *HiddenChainSupportState) { 82 vp := s.storageGeneric.get(mctx, teamID, teamID.IsPublic()) 83 if vp == nil { 84 return nil 85 } 86 ret, ok := vp.(*HiddenChainSupportState) 87 if !ok { 88 mctx.Debug("teams.storage/SupportsHiddenFlagStorage#Get cast error: %T is wrong type", vp) 89 return nil 90 } 91 return ret 92 } 93 94 func (s *SupportsHiddenFlagStorage) ClearEntryIfFalse(mctx libkb.MetaContext, teamID keybase1.TeamID) { 95 if currentState := s.Get(mctx, teamID); currentState != nil && !currentState.State { 96 // put an expired cache state, with 0 CacheUntil 97 s.Put(mctx, &HiddenChainSupportState{TeamID: teamID, State: false, CacheUntil: time.Time{}}) 98 } 99 }