feat: limit unfurls number (#4288)

This commit is contained in:
Igor Sirotin 2023-11-10 10:32:58 +00:00 committed by GitHub
parent 2c954d42cf
commit c3687acc84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -17,6 +17,8 @@ import (
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
) )
const UnfurledLinksPerMessageLimit = 5
type UnfurlURLsResponse struct { type UnfurlURLsResponse struct {
LinkPreviews []*common.LinkPreview `json:"linkPreviews,omitempty"` LinkPreviews []*common.LinkPreview `json:"linkPreviews,omitempty"`
StatusLinkPreviews []*common.StatusLinkPreview `json:"statusLinkPreviews,omitempty"` StatusLinkPreviews []*common.StatusLinkPreview `json:"statusLinkPreviews,omitempty"`
@ -123,6 +125,12 @@ func GetURLs(text string) []string {
indexed[idx] = nil indexed[idx] = nil
urls = append(urls, idx) urls = append(urls, idx)
} }
// This is a temporary limitation solution,
// should be changed with https://github.com/status-im/status-go/issues/4235
if len(urls) == UnfurledLinksPerMessageLimit {
break
}
} }
return urls return urls

View File

@ -659,5 +659,17 @@ func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_Settings() {
s.Require().Len(linkPreviews.LinkPreviews, 0) s.Require().Len(linkPreviews.LinkPreviews, 0)
s.Require().Len(linkPreviews.StatusLinkPreviews, 1) // Status links are always unfurled s.Require().Len(linkPreviews.StatusLinkPreviews, 1) // Status links are always unfurled
s.Require().Equal(requestsCount, 0) s.Require().Equal(requestsCount, 0)
}
func (s *MessengerLinkPreviewsTestSuite) Test_UnfurlURLs_Limit() {
linksToUnfurl := "https://www.youtube.com/watch?v=6dkDepLX0rk " +
"https://www.youtube.com/watch?v=ferZnZ0_rSM " +
"https://www.youtube.com/watch?v=bdneye4pzMw " +
"https://www.youtube.com/watch?v=pRERgcQe-fQ " +
"https://www.youtube.com/watch?v=j82L3pLjb_0 " +
"https://www.youtube.com/watch?v=hxsJvKYyVyg " +
"https://www.youtube.com/watch?v=jIIuzB11dsA"
urls := GetURLs(linksToUnfurl)
s.Require().Equal(UnfurledLinksPerMessageLimit, len(urls))
} }