Skip to content

Commit

Permalink
[status-mobile-16457] Fix messages containing Giphy URLs not being sent
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrkhalil committed Jul 29, 2023
1 parent cbb845b commit 9ae179f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
18 changes: 17 additions & 1 deletion protocol/linkpreview/linkpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type OEmbedUnfurler struct {

type OEmbedResponse struct {
Title string `json:"title"`
ThumbnailURL string `json:"thumbnail_url"`
ThumbnailURL string `json:"url"`
}

func (u OEmbedUnfurler) newOEmbedURL() (*neturl.URL, error) {
Expand Down Expand Up @@ -181,6 +181,7 @@ func (u OEmbedUnfurler) unfurl() (common.LinkPreview, error) {
}

preview.Title = oembedResponse.Title
preview.Thumbnail.URL = oembedResponse.ThumbnailURL
return preview, nil
}

Expand Down Expand Up @@ -256,6 +257,21 @@ func newUnfurler(logger *zap.Logger, httpClient http.Client, url *neturl.URL) Un
logger: logger,
httpClient: httpClient,
}
case "giphy.com", "media.giphy.com":
return OEmbedUnfurler{
oembedEndpoint: "https://giphy.com/services/oembed",
url: url,
logger: logger,
httpClient: httpClient,
}
case "tenor.com", "media.tenor.com":
// TODO: Investigate a way to unfurl previews that contain iframes (Webviews?)
return OEmbedUnfurler{
oembedEndpoint: "https://tenor.com/oembed",
url: url,
logger: logger,
httpClient: httpClient,
}
default:
return OpenGraphUnfurler{
url: url,
Expand Down
48 changes: 48 additions & 0 deletions protocol/linkpreview/linkpreview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,54 @@ func Test_UnfurlURLs_YouTube(t *testing.T) {
assertContainsLongString(t, expected.Thumbnail.DataURI, preview.Thumbnail.DataURI, 100)
}

func Test_UnfurlURLs_giphy(t *testing.T) {
url := "https://www.giphy.com/stickers/happyplaceshow-transparent-sof2kXOSK5beJdb7xH"

expected := common.LinkPreview{
URL: url,
Hostname: "www.giphy.com",
Title: "Floating Tv Show Sticker by Happy Place for iOS & Android | GIPHY",
Thumbnail: common.LinkPreviewThumbnail{
URL: "https://media4.giphy.com/media/sof2kXOSK5beJdb7xH/giphy.gif",
},
}

transport := StubTransport{}
transport.AddURLMatcher(
"https://giphy.com/services/oembed",
[]byte(`
{
"title": "Floating Tv Show Sticker by Happy Place for iOS & Android | GIPHY",
"url": "https://media4.giphy.com/media/sof2kXOSK5beJdb7xH/giphy.gif",
"height": 480,
"width": 400,
"author_name": "Happy Place",
"author_url": "https://giphy.com/happyplaceshow",
"provider_name": "GIPHY",
"provider_url": "https://giphy.com/",
"type": "photo"
}
`),
)
transport.fallbackToDefaultTransport = true

stubbedClient := http.Client{Transport: &transport}

previews, err := UnfurlURLs(nil, stubbedClient, []string{url})
require.NoError(t, err)
require.Len(t, previews, 1)
preview := previews[0]

require.Equal(t, expected.URL, preview.URL)
require.Equal(t, expected.Hostname, preview.Hostname)
require.Equal(t, expected.Title, preview.Title)
require.Equal(t, expected.Description, preview.Description)
require.Equal(t, expected.Thumbnail.Width, preview.Thumbnail.Width)
require.Equal(t, expected.Thumbnail.Height, preview.Thumbnail.Height)
require.Equal(t, expected.Thumbnail.URL, preview.Thumbnail.URL)
assertContainsLongString(t, expected.Thumbnail.DataURI, preview.Thumbnail.DataURI, 100)
}

func Test_UnfurlURLs_Reddit(t *testing.T) {
url := "https://www.reddit.com/r/Bitcoin/comments/13j0tzr/the_best_bitcoin_explanation_of_all_times/?utm_source=share"
expected := common.LinkPreview{
Expand Down

0 comments on commit 9ae179f

Please sign in to comment.