github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/storage/outbox_basebox.go (about)

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	"github.com/keybase/client/go/chat/globals"
     8  	"github.com/keybase/client/go/chat/utils"
     9  	"github.com/keybase/client/go/libkb"
    10  	"github.com/keybase/client/go/protocol/gregor1"
    11  	context "golang.org/x/net/context"
    12  )
    13  
    14  type outboxBaseboxStorage struct {
    15  	globals.Contextified
    16  	*baseBox
    17  	utils.DebugLabeler
    18  
    19  	uid gregor1.UID
    20  }
    21  
    22  func newOutboxBaseboxStorage(g *globals.Context, uid gregor1.UID) *outboxBaseboxStorage {
    23  	return &outboxBaseboxStorage{
    24  		Contextified: globals.NewContextified(g),
    25  		DebugLabeler: utils.NewDebugLabeler(g.ExternalG(), "outboxBaseboxStorage", false),
    26  		baseBox:      newBaseBox(g),
    27  		uid:          uid,
    28  	}
    29  }
    30  
    31  func (s *outboxBaseboxStorage) dbKey() libkb.DbKey {
    32  	return libkb.DbKey{
    33  		Typ: libkb.DBChatOutbox,
    34  		Key: fmt.Sprintf("ob:%s", s.uid),
    35  	}
    36  }
    37  
    38  func (s *outboxBaseboxStorage) clear(ctx context.Context) Error {
    39  	err := s.G().LocalChatDb.Delete(s.dbKey())
    40  	if err != nil {
    41  		return NewInternalError(ctx, s.DebugLabeler, "error clearing outbox: uid: %s err: %s", s.uid, err)
    42  	}
    43  	return nil
    44  }
    45  
    46  func (s *outboxBaseboxStorage) readStorage(ctx context.Context) (res diskOutbox, err Error) {
    47  	defer func() { s.maybeNuke(err, s.dbKey()) }()
    48  
    49  	if memobox := outboxMemCache.Get(s.uid); memobox != nil {
    50  		s.Debug(ctx, "hit in memory cache")
    51  		res = memobox.DeepCopy()
    52  	} else {
    53  		found, ierr := s.readDiskBox(ctx, s.dbKey(), &res)
    54  		if ierr != nil {
    55  			if _, ok := ierr.(libkb.LoginRequiredError); ok {
    56  				return res, MiscError{Msg: ierr.Error()}
    57  			}
    58  			return res, NewInternalError(ctx, s.DebugLabeler, "failure to read chat outbox: %s", ierr)
    59  		}
    60  		if !found {
    61  			return res, MissError{}
    62  		}
    63  		outboxMemCache.Put(s.uid, &res)
    64  	}
    65  	if res.Version != outboxVersion {
    66  		s.Debug(ctx, "on disk version not equal to program version, clearing: disk :%d program: %d",
    67  			res.Version, outboxVersion)
    68  		if cerr := s.clear(ctx); cerr != nil {
    69  			return res, cerr
    70  		}
    71  		return diskOutbox{Version: outboxVersion}, nil
    72  	}
    73  	sort.Sort(ByCtimeOrder(res.Records))
    74  	return res, nil
    75  }
    76  
    77  func (s *outboxBaseboxStorage) writeStorage(ctx context.Context, obox diskOutbox) (err Error) {
    78  	defer func() { s.maybeNuke(err, s.dbKey()) }()
    79  	if ierr := s.writeDiskBox(ctx, s.dbKey(), obox); ierr != nil {
    80  		return NewInternalError(ctx, s.DebugLabeler, "error writing outbox: err: %s", ierr)
    81  	}
    82  	outboxMemCache.Put(s.uid, &obox)
    83  	return nil
    84  }
    85  
    86  func (s *outboxBaseboxStorage) name() string {
    87  	return "db"
    88  }