fix: always unfurl status links (#4159)

This commit is contained in:
Igor Sirotin 2023-10-17 08:25:45 +01:00 committed by GitHub
parent f2464cccfd
commit 036173cfed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 22 deletions

View File

@ -1 +1 @@
0.171.0
0.171.1

View File

@ -141,20 +141,6 @@ func (m *Messenger) UnfurlURLs(httpClient *http.Client, urls []string) (UnfurlUR
return response, fmt.Errorf("failed to get settigs: %w", err)
}
// We use switch case, though there's most cases are empty for code clarity.
switch s.URLUnfurlingMode {
case settings.URLUnfurlingDisableAll:
return response, fmt.Errorf("url unfurling is disabled")
case settings.URLUnfurlingEnableAll:
break
case settings.URLUnfurlingAlwaysAsk:
// This mode should be handled on the app side
// and is considered as equal to URLUnfurlingEnableAll in status-go.
break
default:
return response, fmt.Errorf("invalid url unfurling mode setting: %d", s.URLUnfurlingMode)
}
// Unfurl in a loop
response.LinkPreviews = make([]*common.LinkPreview, 0, len(urls))
@ -178,6 +164,12 @@ func (m *Messenger) UnfurlURLs(httpClient *http.Client, urls []string) (UnfurlUR
continue
}
// `AlwaysAsk` mode should be handled on the app side
// and is considered as equal to `EnableAll` in status-go.
if s.URLUnfurlingMode == settings.URLUnfurlingDisableAll {
continue
}
p, err := m.unfurlURL(httpClient, url)
if err != nil {
m.logger.Warn("failed to unfurl", zap.String("url", url), zap.Error(err))

View File

@ -516,12 +516,14 @@ func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_StatusCommunityJoined()
}
func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_Settings() {
u := "https://github.com"
// Create website stub
ogLink := "https://github.com"
requestsCount := 0
transport := StubTransport{}
transport.AddURLMatcherRoundTrip(
u,
ogLink,
func(req *http.Request) *http.Response {
requestsCount++
responseBody := []byte(`<html><head><meta property="og:title" content="TestTitle"></head></html>`)
@ -533,16 +535,33 @@ func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_Settings() {
)
stubbedClient := http.Client{Transport: &transport}
// Add contact
identity, err := crypto.GenerateKey()
s.Require().NoError(err)
c, err := BuildContactFromPublicKey(&identity.PublicKey)
s.Require().NoError(err)
s.Require().NotNil(c)
c.Bio = "TestBio_1"
c.DisplayName = "TestDisplayName_2"
s.m.allContacts.Store(c.ID, c)
statusUserLink, err := s.m.ShareUserURLWithData(c.ID)
s.Require().NoError(err)
linksToUnfurl := []string{ogLink, statusUserLink}
// Test `AlwaysAsk`
// NOTE: on status-go side `AlwaysAsk` == `EnableAll`, "asking" should be processed by the app
requestsCount = 0
err := s.m.settings.SaveSettingField(settings.URLUnfurlingMode, settings.URLUnfurlingAlwaysAsk)
err = s.m.settings.SaveSettingField(settings.URLUnfurlingMode, settings.URLUnfurlingAlwaysAsk)
s.Require().NoError(err)
linkPreviews, err := s.m.UnfurlURLs(&stubbedClient, []string{u})
linkPreviews, err := s.m.UnfurlURLs(&stubbedClient, linksToUnfurl)
s.Require().NoError(err)
s.Require().Len(linkPreviews.LinkPreviews, 1)
s.Require().Len(linkPreviews.StatusLinkPreviews, 1)
s.Require().Equal(requestsCount, 1)
// Test `EnableAll`
@ -550,9 +569,10 @@ func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_Settings() {
err = s.m.settings.SaveSettingField(settings.URLUnfurlingMode, settings.URLUnfurlingEnableAll)
s.Require().NoError(err)
linkPreviews, err = s.m.UnfurlURLs(&stubbedClient, []string{u})
linkPreviews, err = s.m.UnfurlURLs(&stubbedClient, linksToUnfurl)
s.Require().NoError(err)
s.Require().Len(linkPreviews.LinkPreviews, 1)
s.Require().Len(linkPreviews.StatusLinkPreviews, 1)
s.Require().Equal(requestsCount, 1)
// Test `DisableAll`
@ -560,9 +580,10 @@ func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_Settings() {
err = s.m.settings.SaveSettingField(settings.URLUnfurlingMode, settings.URLUnfurlingDisableAll)
s.Require().NoError(err)
linkPreviews, err = s.m.UnfurlURLs(&stubbedClient, []string{u})
s.Require().Error(err)
linkPreviews, err = s.m.UnfurlURLs(&stubbedClient, linksToUnfurl)
s.Require().NoError(err)
s.Require().Len(linkPreviews.LinkPreviews, 0)
s.Require().Len(linkPreviews.StatusLinkPreviews, 1) // Status links are always unfurled
s.Require().Equal(requestsCount, 0)
}