Skip to content

Commit

Permalink
UnfurlURLsResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Sep 20, 2023
1 parent 5975b4a commit f34ec90
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
19 changes: 11 additions & 8 deletions protocol/messenger_linkpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
"github.com/status-im/status-go/protocol/common"
)

type LinkPreview struct {
common.LinkPreview
type UnfurlURLsResponse struct {
LinkPreviews []common.LinkPreview `json:"linkPreviews,omitempty"`
StatusLinkPreviews []common.StatusLinkPreview `json:"statusLinkPreviews,omitempty"`
}

func normalizeHostname(hostname string) string {
Expand Down Expand Up @@ -132,13 +133,15 @@ func NewDefaultHTTPClient() *http.Client {

// UnfurlURLs assumes clients pass URLs verbatim that were validated and
// processed by GetURLs.
func (m *Messenger) UnfurlURLs(httpClient *http.Client, urls []string) ([]common.LinkPreview, []common.StatusLinkPreview, error) {
func (m *Messenger) UnfurlURLs(httpClient *http.Client, urls []string) (UnfurlURLsResponse, error) {
if httpClient == nil {
httpClient = NewDefaultHTTPClient()
}

previews := make([]common.LinkPreview, 0, len(urls))
statusPreviews := make([]common.StatusLinkPreview, 0, len(urls))
r := UnfurlURLsResponse{
LinkPreviews: make([]common.LinkPreview, 0, len(urls)),
StatusLinkPreviews: make([]common.StatusLinkPreview, 0, len(urls)),
}

for _, url := range urls {
m.logger.Debug("unfurling", zap.String("url", url))
Expand All @@ -150,7 +153,7 @@ func (m *Messenger) UnfurlURLs(httpClient *http.Client, urls []string) ([]common
m.logger.Warn("failed to unfurl status link", zap.String("url", url), zap.Error(err))
continue
}
statusPreviews = append(statusPreviews, preview)
r.StatusLinkPreviews = append(r.StatusLinkPreviews, preview)
continue
}

Expand All @@ -159,8 +162,8 @@ func (m *Messenger) UnfurlURLs(httpClient *http.Client, urls []string) ([]common
m.logger.Warn("failed to unfurl", zap.String("url", url), zap.Error(err))
continue
}
previews = append(previews, p)
r.LinkPreviews = append(r.LinkPreviews, p)
}

return previews, statusPreviews, nil
return r, nil
}
56 changes: 28 additions & 28 deletions protocol/messenger_linkpreview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_YouTube() {
transport.AddURLMatcher(thumbnailURL, s.readAsset("1.jpg"), nil)
stubbedClient := http.Client{Transport: &transport}

previews, sPreviews, err := s.m.UnfurlURLs(&stubbedClient, []string{u})
response, err := s.m.UnfurlURLs(&stubbedClient, []string{u})
s.Require().NoError(err)
s.Require().Len(sPreviews, 0)
s.Require().Len(previews, 1)
preview := previews[0]
s.Require().Len(response.StatusLinkPreviews, 0)
s.Require().Len(response.LinkPreviews, 1)
preview := response.LinkPreviews[0]

s.Require().Equal(expected.Type, preview.Type)
s.Require().Equal(expected.URL, preview.URL)
Expand Down Expand Up @@ -261,11 +261,11 @@ func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_Reddit() {
)
stubbedClient := http.Client{Transport: &transport}

previews, sPreviews, err := s.m.UnfurlURLs(&stubbedClient, []string{u})
response, err := s.m.UnfurlURLs(&stubbedClient, []string{u})
s.Require().NoError(err)
s.Require().Len(sPreviews, 0)
s.Require().Len(previews, 1)
preview := previews[0]
s.Require().Len(response.StatusLinkPreviews, 0)
s.Require().Len(response.LinkPreviews, 1)
preview := response.LinkPreviews[0]

s.Require().Equal(expected.Type, preview.Type)
s.Require().Equal(expected.URL, preview.URL)
Expand All @@ -277,10 +277,10 @@ func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_Reddit() {

func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_Timeout() {
httpClient := http.Client{Timeout: time.Nanosecond}
previews, sPreviews, err := s.m.UnfurlURLs(&httpClient, []string{"https://status.im"})
response, err := s.m.UnfurlURLs(&httpClient, []string{"https://status.im"})
s.Require().NoError(err)
s.Require().Len(sPreviews, 0)
s.Require().Empty(previews)
s.Require().Len(response.StatusLinkPreviews, 0)
s.Require().Empty(response.LinkPreviews)
}

func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_CommonFailures() {
Expand All @@ -294,22 +294,22 @@ func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_CommonFailures() {
nil,
)
stubbedClient := http.Client{Transport: &transport}
previews, sPreviews, err := s.m.UnfurlURLs(&stubbedClient, []string{"https://wikipedia.org"})
response, err := s.m.UnfurlURLs(&stubbedClient, []string{"https://wikipedia.org"})
s.Require().NoError(err)
s.Require().Len(sPreviews, 0)
s.Require().Empty(previews)
s.Require().Len(response.StatusLinkPreviews, 0)
s.Require().Empty(response.LinkPreviews)

// Test 404.
previews, sPreviews, err = s.m.UnfurlURLs(&httpClient, []string{"https://github.com/status-im/i_do_not_exist"})
response, err = s.m.UnfurlURLs(&httpClient, []string{"https://github.com/status-im/i_do_not_exist"})
s.Require().NoError(err)
s.Require().Len(sPreviews, 0)
s.Require().Empty(previews)
s.Require().Len(response.StatusLinkPreviews, 0)
s.Require().Empty(response.LinkPreviews)

// Test no response when trying to get OpenGraph metadata.
previews, sPreviews, err = s.m.UnfurlURLs(&httpClient, []string{"https://wikipedia.o"})
response, err = s.m.UnfurlURLs(&httpClient, []string{"https://wikipedia.o"})
s.Require().NoError(err)
s.Require().Len(sPreviews, 0)
s.Require().Empty(previews)
s.Require().Len(response.StatusLinkPreviews, 0)
s.Require().Empty(response.LinkPreviews)
}

func (s *MessengerLinkPreviewsTestSuite) Test_isSupportedImageURL() {
Expand Down Expand Up @@ -356,11 +356,11 @@ func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_Image() {
transport.AddURLMatcher(u, s.readAsset("IMG_1205.HEIC.jpg"), nil)
stubbedClient := http.Client{Transport: &transport}

previews, sPreviews, err := s.m.UnfurlURLs(&stubbedClient, []string{u})
response, err := s.m.UnfurlURLs(&stubbedClient, []string{u})
s.Require().NoError(err)
s.Require().Len(sPreviews, 0)
s.Require().Len(previews, 1)
preview := previews[0]
s.Require().Len(response.StatusLinkPreviews, 0)
s.Require().Len(response.LinkPreviews, 1)
preview := response.LinkPreviews[0]

s.Require().Equal(expected.Type, preview.Type)
s.Require().Equal(expected.URL, preview.URL)
Expand Down Expand Up @@ -392,12 +392,12 @@ func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_StatusContact() {
s.Require().NoError(err)

//stubbedClient := http.Client{Transport: &StubTransport{}}
previews, sPreviews, err := s.m.UnfurlURLs(nil, []string{u})
r, err := s.m.UnfurlURLs(nil, []string{u})
s.Require().NoError(err)
s.Require().Len(sPreviews, 1)
s.Require().Len(previews, 0)
s.Require().Len(r.StatusLinkPreviews, 1)
s.Require().Len(r.LinkPreviews, 0)

preview := sPreviews[0]
preview := r.StatusLinkPreviews[0]
s.Require().NotNil(preview.Contact)
s.Require().Equal(preview.Contact.DisplayName, c.DisplayName)
s.Require().Equal(preview.Contact.Description, "")
Expand Down
2 changes: 1 addition & 1 deletion services/ext/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ func (api *PublicAPI) GetTextURLs(text string) []string {
// be removed from the response.
//
// This endpoint expects the client to send URLs normalized by GetTextURLs.
func (api *PublicAPI) UnfurlURLs(urls []string) ([]common.LinkPreview, []common.StatusLinkPreview, error) {
func (api *PublicAPI) UnfurlURLs(urls []string) (protocol.UnfurlURLsResponse, error) {
return api.service.messenger.UnfurlURLs(nil, urls)
}

Expand Down

0 comments on commit f34ec90

Please sign in to comment.