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

     1  package storage
     2  
     3  import (
     4  	"crypto/rand"
     5  	"sort"
     6  
     7  	"github.com/keybase/client/go/chat/types"
     8  	"github.com/keybase/client/go/protocol/chat1"
     9  	"github.com/keybase/client/go/protocol/gregor1"
    10  )
    11  
    12  type dummyContextFactory struct{}
    13  
    14  func (d dummyContextFactory) NewKeyFinder() types.KeyFinder {
    15  	return nil
    16  }
    17  
    18  func (d dummyContextFactory) NewUPAKFinder() types.UPAKFinder {
    19  	return nil
    20  }
    21  
    22  func MakeEdit(id chat1.MessageID, supersedes chat1.MessageID) chat1.MessageUnboxed {
    23  	msg := chat1.MessageUnboxedValid{
    24  		ServerHeader: chat1.MessageServerHeader{
    25  			MessageID: id,
    26  		},
    27  		ClientHeader: chat1.MessageClientHeaderVerified{
    28  			MessageType: chat1.MessageType_EDIT,
    29  		},
    30  		MessageBody: chat1.NewMessageBodyWithEdit(chat1.MessageEdit{
    31  			MessageID: supersedes,
    32  			Body:      "edit",
    33  		}),
    34  	}
    35  	return chat1.NewMessageUnboxedWithValid(msg)
    36  }
    37  
    38  func MakeEphemeralEdit(id chat1.MessageID, supersedes chat1.MessageID, ephemeralMetadata *chat1.MsgEphemeralMetadata, now gregor1.Time) chat1.MessageUnboxed {
    39  	msg := MakeEdit(id, supersedes)
    40  	mvalid := msg.Valid()
    41  	mvalid.ServerHeader.Ctime = now
    42  	mvalid.ServerHeader.Now = now
    43  	mvalid.ClientHeader.Rtime = now
    44  	mvalid.ClientHeader.EphemeralMetadata = ephemeralMetadata
    45  	return chat1.NewMessageUnboxedWithValid(mvalid)
    46  }
    47  
    48  func MakeDelete(id chat1.MessageID, originalMessage chat1.MessageID, allEdits []chat1.MessageID) chat1.MessageUnboxed {
    49  	msg := chat1.MessageUnboxedValid{
    50  		ServerHeader: chat1.MessageServerHeader{
    51  			MessageID: id,
    52  		},
    53  		ClientHeader: chat1.MessageClientHeaderVerified{
    54  			MessageType: chat1.MessageType_DELETE,
    55  		},
    56  		MessageBody: chat1.NewMessageBodyWithDelete(chat1.MessageDelete{
    57  			MessageIDs: append([]chat1.MessageID{originalMessage}, allEdits...),
    58  		}),
    59  	}
    60  	return chat1.NewMessageUnboxedWithValid(msg)
    61  }
    62  
    63  func MakeText(id chat1.MessageID, text string) chat1.MessageUnboxed {
    64  	msg := chat1.MessageUnboxedValid{
    65  		ServerHeader: chat1.MessageServerHeader{
    66  			MessageID: id,
    67  		},
    68  		ClientHeader: chat1.MessageClientHeaderVerified{
    69  			MessageType: chat1.MessageType_TEXT,
    70  		},
    71  		MessageBody: chat1.NewMessageBodyWithText(chat1.MessageText{
    72  			Body: text,
    73  		}),
    74  	}
    75  	return chat1.NewMessageUnboxedWithValid(msg)
    76  }
    77  
    78  func MakeEphemeralText(id chat1.MessageID, text string, ephemeralMetadata *chat1.MsgEphemeralMetadata, now gregor1.Time) chat1.MessageUnboxed {
    79  	msg := MakeText(id, text)
    80  	mvalid := msg.Valid()
    81  	mvalid.ServerHeader.Ctime = now
    82  	mvalid.ServerHeader.Now = now
    83  	mvalid.ClientHeader.Rtime = now
    84  	mvalid.ClientHeader.EphemeralMetadata = ephemeralMetadata
    85  	return chat1.NewMessageUnboxedWithValid(mvalid)
    86  }
    87  
    88  func MakeHeadlineMessage(id chat1.MessageID) chat1.MessageUnboxed {
    89  	msg := chat1.MessageUnboxedValid{
    90  		ServerHeader: chat1.MessageServerHeader{
    91  			MessageID: id,
    92  		},
    93  		ClientHeader: chat1.MessageClientHeaderVerified{
    94  			MessageType: chat1.MessageType_HEADLINE,
    95  		},
    96  		MessageBody: chat1.NewMessageBodyWithHeadline(chat1.MessageHeadline{
    97  			Headline: "discus discuss",
    98  		}),
    99  	}
   100  	return chat1.NewMessageUnboxedWithValid(msg)
   101  }
   102  
   103  func MakeDeleteHistory(id chat1.MessageID, upto chat1.MessageID) chat1.MessageUnboxed {
   104  	msg := chat1.MessageUnboxedValid{
   105  		ServerHeader: chat1.MessageServerHeader{
   106  			MessageID: id,
   107  		},
   108  		ClientHeader: chat1.MessageClientHeaderVerified{
   109  			MessageType: chat1.MessageType_DELETEHISTORY,
   110  		},
   111  		MessageBody: chat1.NewMessageBodyWithDeletehistory(chat1.MessageDeleteHistory{
   112  			Upto: upto,
   113  		}),
   114  	}
   115  	return chat1.NewMessageUnboxedWithValid(msg)
   116  }
   117  
   118  func MakeMsgWithType(id chat1.MessageID, typ chat1.MessageType) chat1.MessageUnboxed {
   119  	msg := chat1.MessageUnboxedValid{
   120  		ServerHeader: chat1.MessageServerHeader{
   121  			MessageID: id,
   122  		},
   123  		ClientHeader: chat1.MessageClientHeaderVerified{
   124  			MessageType: typ,
   125  		},
   126  	}
   127  	return chat1.NewMessageUnboxedWithValid(msg)
   128  }
   129  
   130  func MakeConversationAt(convID chat1.ConversationID, maxID chat1.MessageID) chat1.Conversation {
   131  	return chat1.Conversation{
   132  		Metadata: chat1.ConversationMetadata{
   133  			ConversationID: convID,
   134  		},
   135  		ReaderInfo: &chat1.ConversationReaderInfo{
   136  			MaxMsgid: maxID,
   137  		},
   138  	}
   139  }
   140  
   141  func randBytes(n int) []byte {
   142  	ret := make([]byte, n)
   143  	_, err := rand.Read(ret)
   144  	if err != nil {
   145  		panic(err)
   146  	}
   147  	return ret
   148  }
   149  
   150  func MakeConvID() chat1.ConversationID {
   151  	rbytes := randBytes(8)
   152  	return chat1.ConversationID(rbytes)
   153  }
   154  
   155  func MakeConversation(maxID chat1.MessageID) chat1.Conversation {
   156  	return MakeConversationAt(MakeConvID(), maxID)
   157  }
   158  
   159  // Sort messages by ID descending
   160  func SortMessagesDesc(msgs []chat1.MessageUnboxed) []chat1.MessageUnboxed {
   161  	res := make([]chat1.MessageUnboxed, len(msgs))
   162  	copy(res, msgs)
   163  	sort.SliceStable(res, func(i, j int) bool {
   164  		return res[j].GetMessageID() < res[i].GetMessageID()
   165  	})
   166  	return res
   167  }