From ad6229bc4f3270518adbdc8d4d52c00017bbd5c4 Mon Sep 17 00:00:00 2001 From: Shivek Khurana Date: Wed, 3 Feb 2021 18:41:53 +0530 Subject: [PATCH] Follow short urls to get long url and then use that long url with oembed --- protocol/urls/urls.go | 35 +++++++++++++++++++++++++++++++++++ protocol/urls/urls_test.go | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/protocol/urls/urls.go b/protocol/urls/urls.go index 979082ad9..172f9565e 100644 --- a/protocol/urls/urls.go +++ b/protocol/urls/urls.go @@ -68,6 +68,11 @@ func LinkPreviewWhitelist() []Site { Address: "tenor.com", ImageSite: true, }, + Site{ + Title: "GIPHY GIFs Short URLS", + Address: "gph.se", + ImageSite: true, + }, Site{ Title: "GIPHY GIFs", Address: "giphy.com", @@ -171,6 +176,34 @@ func GetGiphyPreviewData(link string) (previewData LinkPreviewData, err error) { return previewData, nil } +// Giphy has a shortener service called gph.se, the oembed service doesn't work with shortened urls, +// so we need to fetch the long url first +func GetGiphyLongURL(shortURL string) (longURL string, err error) { + res, err := http.Get(shortURL) + + if err != nil { + return longURL, fmt.Errorf("can't get bytes from Giphy's short url at %s", shortURL) + } + + canonicalURL := res.Request.URL.String() + if (canonicalURL == shortURL) { + // no redirect, ie. not a valid url + return longURL, fmt.Errorf("unable to process Giphy's short url at %s", shortURL) + } else { + return canonicalURL, err + } +} + +func GetGiphyShortURLPreviewData (shortURL string) (data LinkPreviewData, err error) { + longURL, err := GetGiphyLongURL(shortURL) + + if err != nil { + return data, err + } else { + return GetGiphyPreviewData(longURL) + } +} + func GetTenorOembed(url string) (data TenorOembedData, err error) { oembedLink := fmt.Sprintf(TenorOembedLink, url) @@ -216,6 +249,8 @@ func GetLinkPreviewData(link string) (previewData LinkPreviewData, err error) { return GetGithubPreviewData(link) case "giphy.com": return GetGiphyPreviewData(link) + case "gph.se": + return GetGiphyShortURLPreviewData(link) case "tenor.com": return GetTenorPreviewData(link) default: diff --git a/protocol/urls/urls_test.go b/protocol/urls/urls_test.go index d9e93a42a..94f1ed653 100644 --- a/protocol/urls/urls_test.go +++ b/protocol/urls/urls_test.go @@ -58,16 +58,43 @@ func TestGetGiphyPreviewData(t *testing.T) { require.Error(t, err) - // Other link shapes - // shortLink := "https://gph.is/g/aXLyK7P" mediaLink := "https://media.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif" - // shortLinkData, _ := GetGiphyPreviewData(shortLink) mediaLinkData, _ := GetGiphyPreviewData(mediaLink) require.Equal(t, thumbnailUrlWithoutSubdomain(mediaLinkData.ThumbnailURL), thumbnailUrlWithoutSubdomain(previewData.ThumbnailURL)) } +func TestGetGiphyLongURL(t *testing.T) { + shortURL := "https://gph.is/g/aXLyK7P" + computedLongURL, _ := GetGiphyLongURL(shortURL) + actualLongURL := "https://giphy.com/gifs/FullMag-robot-boston-dynamics-dance-lcG3qwtTKSNI2i5vst" + + require.Equal(t, computedLongURL, actualLongURL) + + _, err := GetGiphyLongURL("http://this-giphy-site-doesn-not-exist.se/bogus-url") + require.Error(t, err) + + _, err = GetGiphyLongURL("http://gph.se/bogus-url-but-correct-domain") + require.Error(t, err) +} + + +func TestGetGiphyShortURLPreviewData(t *testing.T) { + shortURL := "https://gph.is/g/aXLyK7P" + previewData, err := GetGiphyShortURLPreviewData(shortURL) + + bostonDynamicsEthGifData := LinkPreviewData{ + Site: "GIPHY", + Title: "Boston Dynamics Yes GIF by FullMag - Find & Share on GIPHY", + ThumbnailURL: "https://media1.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif", + } + + require.NoError(t, err) + require.Equal(t, bostonDynamicsEthGifData.Site, previewData.Site) + require.Equal(t, bostonDynamicsEthGifData.Title, previewData.Title) +} + func TestGetTenorPreviewData(t *testing.T) { validTenorLink := "https://tenor.com/view/robot-dance-do-you-love-me-boston-boston-dynamics-dance-gif-19998728" previewData, err := GetTenorPreviewData(validTenorLink)