MM-45000: Upgrade to new opengraph module path (#20701)

https://mattermost.atlassian.net/browse/MM-45000

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2022-07-25 14:25:32 +05:30
коммит произвёл GitHub
родитель 68055bcb10
Коммит fa9477d332
6 изменённых файлов: 33 добавлений и 23 удалений

Просмотреть файл

@@ -13,6 +13,7 @@ import (
"unicode/utf8"
"github.com/dyatlov/go-opengraph/opengraph"
"github.com/dyatlov/go-opengraph/opengraph/types/image"
)
const (
@@ -50,7 +51,7 @@ func truncateText(original string) string {
return original
}
func firstNImages(images []*opengraph.Image, maxImages int) []*opengraph.Image {
func firstNImages(images []*image.Image, maxImages int) []*image.Image {
if maxImages < 0 { // don't break stuff, if it's weird, go for sane defaults
maxImages = LinkMetadataMaxImages
}

Просмотреть файл

@@ -11,14 +11,20 @@ import (
"unicode/utf8"
"github.com/dyatlov/go-opengraph/opengraph"
"github.com/dyatlov/go-opengraph/opengraph/types/article"
"github.com/dyatlov/go-opengraph/opengraph/types/audio"
"github.com/dyatlov/go-opengraph/opengraph/types/book"
"github.com/dyatlov/go-opengraph/opengraph/types/image"
"github.com/dyatlov/go-opengraph/opengraph/types/profile"
"github.com/dyatlov/go-opengraph/opengraph/types/video"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const BigText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus maximus faucibus ex, vitae placerat neque feugiat ac. Nam tempus libero quis pellentesque feugiat. Cras tristique diam vel condimentum viverra. Proin molestie posuere leo. Nam pulvinar, ex quis tristique cursus, turpis ante commodo elit, a dapibus est ipsum id eros. Mauris tortor dolor, posuere ac velit vitae, faucibus viverra fusce."
func sampleImage(imageName string) *opengraph.Image {
return &opengraph.Image{
func sampleImage(imageName string) *image.Image {
return &image.Image{
URL: fmt.Sprintf("http://example.com/%s", imageName),
SecureURL: fmt.Sprintf("https://example.com/%s", imageName),
Type: "png",
@@ -180,7 +186,7 @@ func TestLinkMetadataDeserializeDataToConcreteType(t *testing.T) {
og := &opengraph.OpenGraph{
URL: "http://example.com",
Description: "Hello, world!",
Images: []*opengraph.Image{
Images: []*image.Image{
{
URL: "http://example.com/image.png",
},
@@ -260,24 +266,24 @@ func TestTruncateText(t *testing.T) {
func TestFirstNImages(t *testing.T) {
t.Run("when empty, return an empty one", func(t *testing.T) {
empty := make([]*opengraph.Image, 0)
empty := make([]*image.Image, 0)
assert.Exactly(t, firstNImages(empty, 1), empty, "Should be the same element")
})
t.Run("when it contains one element, return the same array", func(t *testing.T) {
one := []*opengraph.Image{sampleImage("image.png")}
one := []*image.Image{sampleImage("image.png")}
assert.Exactly(t, firstNImages(one, 1), one, "Should be the same element")
})
t.Run("when it contains more than one element and asking for only one, return the first one", func(t *testing.T) {
two := []*opengraph.Image{sampleImage("image.png"), sampleImage("notme.png")}
two := []*image.Image{sampleImage("image.png"), sampleImage("notme.png")}
assert.True(t, strings.HasSuffix(firstNImages(two, 1)[0].URL, "image.png"), "Should be the image element")
})
t.Run("when it contains less than asked, return the original", func(t *testing.T) {
two := []*opengraph.Image{sampleImage("image.png"), sampleImage("notme.png")}
two := []*image.Image{sampleImage("image.png"), sampleImage("notme.png")}
assert.Equal(t, two, firstNImages(two, 10), "should be the same pointer")
})
t.Run("asking for negative images", func(t *testing.T) {
six := []*opengraph.Image{
six := []*image.Image{
sampleImage("image.png"),
sampleImage("another.png"),
sampleImage("yetanother.jpg"),
@@ -300,18 +306,18 @@ func TestTruncateOpenGraph(t *testing.T) {
SiteName: BigText,
Locale: "[EN-en]",
LocalesAlternate: []string{"[EN-ca]", "[ES-es]"},
Images: []*opengraph.Image{
Images: []*image.Image{
sampleImage("image.png"),
sampleImage("another.png"),
sampleImage("yetanother.jpg"),
sampleImage("metoo.gif"),
sampleImage("fifth.ico"),
sampleImage("notme.tiff")},
Audios: []*opengraph.Audio{{}},
Videos: []*opengraph.Video{{}},
Article: &opengraph.Article{},
Book: &opengraph.Book{},
Profile: &opengraph.Profile{},
Audios: []*audio.Audio{{}},
Videos: []*video.Video{{}},
Article: &article.Article{},
Book: &book.Book{},
Profile: &profile.Profile{},
}
result := TruncateOpenGraph(&og)
assert.Nil(t, result.Article, "No article stored")