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

     1  package storage
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     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/chat1"
    11  	"github.com/keybase/client/go/protocol/keybase1"
    12  )
    13  
    14  type breakTracker struct {
    15  	globals.Contextified
    16  	utils.DebugLabeler
    17  }
    18  
    19  func newBreakTracker(g *globals.Context) *breakTracker {
    20  	return &breakTracker{
    21  		Contextified: globals.NewContextified(g),
    22  		DebugLabeler: utils.NewDebugLabeler(g.ExternalG(), "BreakTracker", false),
    23  	}
    24  }
    25  
    26  func (b *breakTracker) makeDbKey(tlfID chat1.TLFID) libkb.DbKey {
    27  	return libkb.DbKey{
    28  		Typ: libkb.DBChatBlocks,
    29  		Key: fmt.Sprintf("breaks:%s", tlfID),
    30  	}
    31  }
    32  
    33  func (b *breakTracker) UpdateTLF(ctx context.Context, tlfID chat1.TLFID,
    34  	breaks []keybase1.TLFIdentifyFailure) (err error) {
    35  	defer b.Trace(ctx, &err, "UpdateTLF(%s)", tlfID)()
    36  	key := b.makeDbKey(tlfID)
    37  
    38  	dat, err := encode(breaks)
    39  	if err != nil {
    40  		return NewInternalError(ctx, b.DebugLabeler, "encode error: %s", err.Error())
    41  	}
    42  	if err = b.G().LocalChatDb.PutRaw(key, dat); err != nil {
    43  		return NewInternalError(ctx, b.DebugLabeler, "PutRaw error: %s", err.Error())
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func (b *breakTracker) IsTLFBroken(ctx context.Context, tlfID chat1.TLFID) (res bool, err error) {
    50  	defer b.Trace(ctx, &err, "IsTLFBroken(%s)", tlfID)()
    51  	key := b.makeDbKey(tlfID)
    52  	raw, found, err := b.G().LocalChatDb.GetRaw(key)
    53  	if err != nil {
    54  		return true, NewInternalError(ctx, b.DebugLabeler, "GetRaw error: %s", err.Error())
    55  	}
    56  
    57  	// Assume to be broken if we have no record
    58  	if !found {
    59  		return true, nil
    60  	}
    61  
    62  	var breaks []keybase1.TLFIdentifyFailure
    63  	if err = decode(raw, &breaks); err != nil {
    64  		return true, NewInternalError(ctx, b.DebugLabeler, "decode error: %s", err.Error())
    65  	}
    66  
    67  	return len(breaks) != 0, nil
    68  }