Skip to content

Commit

Permalink
Use messenger logger coming from the endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmotta committed May 18, 2023
1 parent 4dd915c commit a4039c6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
11 changes: 7 additions & 4 deletions protocol/linkpreview/linkpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,13 @@ func GetURLs(text string) []string {

// UnfurlURLs assumes clients pass URLs verbatim that were validated and
// processed by GetURLs.
func UnfurlURLs(urls []string) ([]common.LinkPreview, error) {
logger, err := zap.NewDevelopment()
if err != nil {
return nil, fmt.Errorf("failed to create logger: %w", err)
func UnfurlURLs(logger *zap.Logger, urls []string) ([]common.LinkPreview, error) {
var err error
if logger == nil {
logger, err = zap.NewDevelopment()
if err != nil {
return nil, fmt.Errorf("failed to create logger: %w", err)
}
}

previews := make([]common.LinkPreview, 0, len(urls))
Expand Down
8 changes: 4 additions & 4 deletions protocol/linkpreview/linkpreview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestUnfurlURLs(t *testing.T) {
urls = append(urls, e.url)
}

links, err := UnfurlURLs(urls)
links, err := UnfurlURLs(nil, urls)
require.NoError(t, err)
require.Len(t, links, len(examples), "all URLs should have been unfurled successfully")

Expand All @@ -177,17 +177,17 @@ func TestUnfurlURLs(t *testing.T) {
}

// Test URL that doesn't return any OpenGraph title.
previews, err := UnfurlURLs([]string{"https://wikipedia.org"})
previews, err := UnfurlURLs(nil, []string{"https://wikipedia.org"})
require.NoError(t, err)
require.Empty(t, previews)

// Test 404.
previews, err = UnfurlURLs([]string{"https://github.com/status-im/i_do_not_exist"})
previews, err = UnfurlURLs(nil, []string{"https://github.com/status-im/i_do_not_exist"})
require.NoError(t, err)
require.Empty(t, previews)

// Test no response when trying to get OpenGraph metadata.
previews, err = UnfurlURLs([]string{"https://wikipedia.o"})
previews, err = UnfurlURLs(nil, []string{"https://wikipedia.o"})
require.NoError(t, err)
require.Empty(t, previews)
}
5 changes: 5 additions & 0 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/status-im/status-go/protocol/identity"
"github.com/status-im/status-go/protocol/identity/alias"
"github.com/status-im/status-go/protocol/identity/identicon"
"github.com/status-im/status-go/protocol/linkpreview"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/pushnotificationclient"
"github.com/status-im/status-go/protocol/pushnotificationserver"
Expand Down Expand Up @@ -5991,6 +5992,10 @@ func generateAliasAndIdenticon(pk string) (string, string, error) {

}

func (m *Messenger) UnfurlURLs(urls []string) ([]common.LinkPreview, error) {
return linkpreview.UnfurlURLs(m.logger, urls)
}

func (m *Messenger) SendEmojiReaction(ctx context.Context, chatID, messageID string, emojiID protobuf.EmojiReaction_Type) (*MessengerResponse, error) {
var response MessengerResponse

Expand Down
2 changes: 1 addition & 1 deletion services/ext/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ func (api *PublicAPI) GetTextURLs(text string) []string {
//
// This endpoint expects the client to send URLs normalized by GetTextURLs.
func (api *PublicAPI) UnfurlURLs(urls []string) ([]common.LinkPreview, error) {
return linkpreview.UnfurlURLs(urls)
return api.service.messenger.UnfurlURLs(urls)
}

func (api *PublicAPI) EnsVerified(pk, ensName string) error {
Expand Down

0 comments on commit a4039c6

Please sign in to comment.