2023-07-12 09:46:56 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
"github.com/status-im/status-go/protocol/tt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WaitOnMessengerResponse Wait until the condition is true or the timeout is reached.
|
|
|
|
func WaitOnMessengerResponse(m *Messenger, condition func(*MessengerResponse) bool, errorMessage string) (*MessengerResponse, error) {
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
err := tt.RetryWithBackOff(func() error {
|
|
|
|
var err error
|
|
|
|
r, err := m.RetrieveAll()
|
|
|
|
if err := response.Merge(r); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && !condition(response) {
|
|
|
|
err = errors.New(errorMessage)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
2023-08-04 10:41:24 +00:00
|
|
|
return response, err
|
2023-07-12 09:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FindFirstByContentType(messages []*common.Message, contentType protobuf.ChatMessage_ContentType) *common.Message {
|
|
|
|
for _, message := range messages {
|
|
|
|
if message.ContentType == contentType {
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|