github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/gregor/utils/utils.go (about)

     1  package utils
     2  
     3  import (
     4  	"github.com/keybase/client/go/gregor"
     5  	"github.com/keybase/client/go/libkb"
     6  	"github.com/keybase/client/go/protocol/gregor1"
     7  	"golang.org/x/net/context"
     8  )
     9  
    10  func NewGregorMsgID() (gregor1.MsgID, error) {
    11  	r, err := libkb.RandBytes(16)
    12  	if err != nil {
    13  		return nil, err
    14  	}
    15  	return gregor1.MsgID(r), nil
    16  }
    17  
    18  func TemplateMessage(uid gregor1.UID) (gregor1.Message, error) {
    19  	newMsgID, err := NewGregorMsgID()
    20  	if err != nil {
    21  		return gregor1.Message{}, err
    22  	}
    23  	return gregor1.Message{
    24  		Ibm_: &gregor1.InBandMessage{
    25  			StateUpdate_: &gregor1.StateUpdateMessage{
    26  				Md_: gregor1.Metadata{
    27  					Uid_:   uid,
    28  					MsgID_: newMsgID,
    29  				},
    30  			},
    31  		},
    32  	}, nil
    33  }
    34  
    35  func FormMessageForInjectItem(ctx context.Context, uid gregor1.UID, cat string, body []byte,
    36  	dtime gregor1.TimeOrOffset) (gregor.Message, error) {
    37  	creation, err := TemplateMessage(uid)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	creation.Ibm_.StateUpdate_.Creation_ = &gregor1.Item{
    42  		Category_: gregor1.Category(cat),
    43  		Body_:     gregor1.Body(body),
    44  		Dtime_:    dtime,
    45  	}
    46  	return creation, nil
    47  }
    48  
    49  func FormMessageForDismissItem(ctx context.Context, uid gregor1.UID, id gregor.MsgID) (gregor.Message, error) {
    50  	dismissal, err := TemplateMessage(uid)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	dismissal.Ibm_.StateUpdate_.Dismissal_ = &gregor1.Dismissal{
    55  		MsgIDs_: []gregor1.MsgID{gregor1.MsgID(id.Bytes())},
    56  	}
    57  	return dismissal, nil
    58  }
    59  
    60  func FormMessageForDismissCategory(ctx context.Context, uid gregor1.UID, cat gregor1.Category) (gregor.Message, error) {
    61  	dismissal, err := TemplateMessage(uid)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	dismissal.Ibm_.StateUpdate_.Dismissal_ = &gregor1.Dismissal{
    66  		Ranges_: []gregor1.MsgRange{
    67  			{
    68  				Category_: cat,
    69  			}},
    70  	}
    71  	return dismissal, nil
    72  }