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

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/keybase/client/go/chat/globals"
     7  	"github.com/keybase/client/go/chat/utils"
     8  	"github.com/keybase/client/go/libkb"
     9  	"github.com/keybase/client/go/protocol/chat1"
    10  	"github.com/keybase/client/go/protocol/gregor1"
    11  	"golang.org/x/net/context"
    12  )
    13  
    14  type msgIDTracker struct {
    15  	globals.Contextified
    16  	utils.DebugLabeler
    17  }
    18  
    19  func newMsgIDTracker(g *globals.Context) *msgIDTracker {
    20  	return &msgIDTracker{
    21  		Contextified: globals.NewContextified(g),
    22  		DebugLabeler: utils.NewDebugLabeler(g.ExternalG(), "MsgIDTracker", false),
    23  	}
    24  }
    25  
    26  func (t *msgIDTracker) makeDbKey(convID chat1.ConversationID, uid gregor1.UID) libkb.DbKey {
    27  	return libkb.DbKey{
    28  		Typ: libkb.DBChatBlocks,
    29  		Key: fmt.Sprintf("maxMsgID:%s:%s", uid, convID),
    30  	}
    31  }
    32  
    33  func (t *msgIDTracker) bumpMaxMessageID(
    34  	ctx context.Context, convID chat1.ConversationID, uid gregor1.UID, msgID chat1.MessageID) Error {
    35  
    36  	// No need to use transaction here since the Storage class takes lock.
    37  
    38  	maxMsgIDKey := t.makeDbKey(convID, uid)
    39  
    40  	raw, found, err := t.G().LocalChatDb.GetRaw(maxMsgIDKey)
    41  	if err != nil {
    42  		return NewInternalError(ctx, t.DebugLabeler, "GetRaw error: %s", err.Error())
    43  	}
    44  	if found {
    45  		var maxMsgID chat1.MessageID
    46  		if err = decode(raw, &maxMsgID); err != nil {
    47  			return NewInternalError(ctx, t.DebugLabeler, "decode error: %s", err.Error())
    48  		}
    49  		if maxMsgID >= msgID {
    50  			return nil
    51  		}
    52  	}
    53  
    54  	dat, err := encode(msgID)
    55  	if err != nil {
    56  		return NewInternalError(ctx, t.DebugLabeler, "encode error: %s", err.Error())
    57  	}
    58  	if err = t.G().LocalChatDb.PutRaw(maxMsgIDKey, dat); err != nil {
    59  		return NewInternalError(ctx, t.DebugLabeler, "PutRaw error: %s", err.Error())
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func (t *msgIDTracker) getMaxMessageID(
    66  	ctx context.Context, convID chat1.ConversationID, uid gregor1.UID) (chat1.MessageID, Error) {
    67  
    68  	maxMsgIDKey := t.makeDbKey(convID, uid)
    69  
    70  	raw, found, err := t.G().LocalChatDb.GetRaw(maxMsgIDKey)
    71  	if err != nil {
    72  		return 0, NewInternalError(ctx, t.DebugLabeler, "GetRaw error: %s", err.Error())
    73  	}
    74  	if !found {
    75  		return 0, MissError{}
    76  	}
    77  
    78  	var maxMsgID chat1.MessageID
    79  	if err = decode(raw, &maxMsgID); err != nil {
    80  		return 0, NewInternalError(ctx, t.DebugLabeler, "decode error: %s", err.Error())
    81  	}
    82  
    83  	return maxMsgID, nil
    84  }
    85  
    86  func (t *msgIDTracker) clear(convID chat1.ConversationID, uid gregor1.UID) error {
    87  	return t.G().LocalChatDb.Delete(t.makeDbKey(convID, uid))
    88  }