Skip to content

Commit

Permalink
Replicate oembed standards
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrkhalil committed Aug 1, 2023
1 parent f04c472 commit 70273a7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 23 deletions.
1 change: 1 addition & 0 deletions protocol/common/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type LinkPreview struct {
Hostname string `json:"hostname"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
HTML string `json:"height,omitempty"`
Thumbnail LinkPreviewThumbnail `json:"thumbnail,omitempty"`
}

Expand Down
91 changes: 68 additions & 23 deletions protocol/linkpreview/linkpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,22 @@ type OEmbedUnfurler struct {
url *neturl.URL
}

type OEmbedPhotoResponse struct {
Title string `json:"title"`
PhotoUrl string `json:"url"`
ThumbnailURL string `json:"thumbnail_url"`
Width int `json:"width"`
Height int `json:"height"`
type OEmbedBaseResponse struct {
Type string `json:"type"`
Version string `json:"version"`
Title string `json:"title,omitempty"`
AuthorName string `json:"author_name,omitempty"`
AuthorURL string `json:"author_url,omitempty"`
ProviderName string `json:"provider_name,omitempty"`
ProviderURL string `json:"provider_url,omitempty"`
ThumbnailURL string `json:"thumbnail_url,omitempty"`
URL string `json:"url,omitempty"`
HTML string `json:"html,omitempty"`
CacheAge int `json:"cache_age,omitempty"`
ThumbnailWidth int `json:"thumbnail_width,omitempty"`
ThumbnailHeight int `json:"thumbnail_height,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
}

func (u OEmbedUnfurler) newOEmbedURL() (*neturl.URL, error) {
Expand All @@ -152,6 +162,46 @@ func (u OEmbedUnfurler) newOEmbedURL() (*neturl.URL, error) {
return oembedURL, nil
}

func handlePhotoOembedType(preview common.LinkPreview, response OEmbedBaseResponse) (common.LinkPreview, error) {

if response.URL != "" {
preview.Thumbnail.URL = response.URL
}
if response.Width != 0 {
preview.Thumbnail.URL = response.URL
}
if response.Height != 0 {
preview.Thumbnail.URL = response.URL
}
return preview, nil
}

func handleVideoOembedType(preview common.LinkPreview, response OEmbedBaseResponse) (common.LinkPreview, error) {
if response.HTML != "" {
preview.HTML = response.HTML
}
if response.Width != 0 {
preview.Thumbnail.URL = response.URL
}
if response.Height != 0 {
preview.Thumbnail.URL = response.URL
}
return preview, nil
}

func handleRichOembedType(preview common.LinkPreview, response OEmbedBaseResponse) (common.LinkPreview, error) {
if response.HTML != "" {
preview.HTML = response.HTML
}
if response.Width != 0 {
preview.Thumbnail.URL = response.URL
}
if response.Height != 0 {
preview.Thumbnail.URL = response.URL
}
return preview, nil
}

func (u OEmbedUnfurler) unfurl() (common.LinkPreview, error) {
preview := newDefaultLinkPreview(u.url)
oembedURL, err := u.newOEmbedURL()
Expand All @@ -169,33 +219,28 @@ func (u OEmbedUnfurler) unfurl() (common.LinkPreview, error) {
return preview, err
}

var oembedResponse OEmbedPhotoResponse
var oembedResponse OEmbedBaseResponse
if err != nil {
return preview, err
}

err = json.Unmarshal(oembedBytes, &oembedResponse)

if err != nil {
return preview, err
if oembedResponse.Title != "" {
preview.Title = oembedResponse.Title
}
switch oembedResponse.Type {

if oembedResponse.Title == "" {
return preview, fmt.Errorf("missing required title in oEmbed response")
}
case "photo":
return handlePhotoOembedType(preview, oembedResponse)

preview.Title = oembedResponse.Title
var urlToUse string
case "video":
return handleVideoOembedType(preview, oembedResponse)

if oembedResponse.ThumbnailURL == "" {
urlToUse = oembedResponse.PhotoUrl
} else {
urlToUse = oembedResponse.ThumbnailURL
case "rich":
return handleRichOembedType(preview, oembedResponse)
default:
return preview, fmt.Errorf("unexpected oembed type: %v", oembedResponse.Type)
}

preview.Thumbnail.URL = urlToUse

return preview, nil
}

type OpenGraphMetadata struct {
Expand Down

0 comments on commit 70273a7

Please sign in to comment.