github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/maps/trackstorage.go (about) 1 package maps 2 3 import ( 4 "context" 5 6 "github.com/keybase/client/go/chat/globals" 7 "github.com/keybase/client/go/chat/storage" 8 "github.com/keybase/client/go/encrypteddb" 9 "github.com/keybase/client/go/libkb" 10 "github.com/keybase/client/go/protocol/chat1" 11 "github.com/keybase/client/go/protocol/gregor1" 12 ) 13 14 const diskTrackStorageVersion = 1 15 16 type diskLocationTrack struct { 17 ConvID chat1.ConversationID `codec:"C"` 18 MsgID chat1.MessageID `codec:"M"` 19 EndTime gregor1.Time `codec:"E"` 20 Coords []chat1.Coordinate `codec:"O"` 21 GetCurrentPosition bool `codec:"P"` 22 MaxCoords int `codec:"MC"` 23 Stopped bool `codec:"S"` 24 } 25 26 type diskTrackStorage struct { 27 Version int `codec:"V"` 28 Trackers []diskLocationTrack `codec:"T"` 29 } 30 31 type trackStorage struct { 32 globals.Contextified 33 encryptedDB *encrypteddb.EncryptedDB 34 } 35 36 func newTrackStorage(g *globals.Context) *trackStorage { 37 keyFn := func(ctx context.Context) ([32]byte, error) { 38 return storage.GetSecretBoxKey(ctx, g.ExternalG()) 39 } 40 dbFn := func(g *libkb.GlobalContext) *libkb.JSONLocalDb { 41 return g.LocalChatDb 42 } 43 return &trackStorage{ 44 Contextified: globals.NewContextified(g), 45 encryptedDB: encrypteddb.New(g.ExternalG(), dbFn, keyFn), 46 } 47 } 48 49 func (t *trackStorage) dbKey() libkb.DbKey { 50 return libkb.DbKey{ 51 Typ: libkb.DBChatLocation, 52 Key: "tracks", 53 } 54 } 55 56 func (t *trackStorage) Save(ctx context.Context, trackers []*locationTrack) error { 57 var dat []diskLocationTrack 58 for _, t := range trackers { 59 dat = append(dat, t.ToDisk()) 60 } 61 return t.encryptedDB.Put(ctx, t.dbKey(), diskTrackStorage{ 62 Version: diskTrackStorageVersion, 63 Trackers: dat, 64 }) 65 } 66 67 func (t *trackStorage) Restore(ctx context.Context) (res []*locationTrack, err error) { 68 var dat diskTrackStorage 69 found, err := t.encryptedDB.Get(ctx, t.dbKey(), &dat) 70 if err != nil { 71 return res, err 72 } 73 if !found { 74 return nil, nil 75 } 76 if dat.Version != diskTrackStorageVersion { 77 return nil, nil 78 } 79 for _, dt := range dat.Trackers { 80 res = append(res, newLocationTrackFromDisk(dt)) 81 } 82 return res, nil 83 }