From 711548c0ba6e9a2b29bcb7d8f631b449e95604ec Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Fri, 4 Feb 2022 18:17:01 -0700 Subject: [PATCH 1/3] link previews: update to support a list Uses com.beeper.linkpreviews instead of com.beeper.linkpreview --- portal.go | 6 ++++-- urlpreview.go | 13 +++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/portal.go b/portal.go index 72958d0..a484516 100644 --- a/portal.go +++ b/portal.go @@ -1510,7 +1510,9 @@ func (portal *Portal) convertTextMessage(intent *appservice.IntentAPI, source *U } expiresIn = contextInfo.GetExpiration() - extraAttrs["com.beeper.linkpreview"] = portal.convertURLPreviewToBeeper(intent, source, msg.GetExtendedTextMessage()) + extraAttrs["com.beeper.linkpreviews"] = []*BeeperLinkPreview{ + portal.convertURLPreviewToBeeper(intent, source, msg.GetExtendedTextMessage()), + } } return &ConvertedMessage{ @@ -2253,7 +2255,7 @@ func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) (*waP if content.MsgType == event.MsgEmote && !relaybotFormatted { text = "/me " + text } - if ctxInfo.StanzaId != nil || ctxInfo.MentionedJid != nil || ctxInfo.Expiration != nil || evt.Content.Raw["com.beeper.linkpreview"] != nil { + if ctxInfo.StanzaId != nil || ctxInfo.MentionedJid != nil || ctxInfo.Expiration != nil || evt.Content.Raw["com.beeper.linkpreviews"] != nil { msg.ExtendedTextMessage = &waProto.ExtendedTextMessage{ Text: &text, ContextInfo: &ctxInfo, diff --git a/urlpreview.go b/urlpreview.go index ff5b8d9..f00d2df 100644 --- a/urlpreview.go +++ b/urlpreview.go @@ -115,12 +115,17 @@ func (portal *Portal) convertURLPreviewToBeeper(intent *appservice.IntentAPI, so } func (portal *Portal) convertURLPreviewToWhatsApp(sender *User, evt *event.Event, dest *waProto.ExtendedTextMessage) { - rawPreview := gjson.GetBytes(evt.Content.VeryRaw, `com\.beeper\.linkpreview`) - if !rawPreview.Exists() || !rawPreview.IsObject() { + rawPreview := gjson.GetBytes(evt.Content.VeryRaw, `com\.beeper\.linkpreviews`) + if !rawPreview.Exists() || !rawPreview.IsArray() { return } - var preview BeeperLinkPreview - if err := json.Unmarshal([]byte(rawPreview.Raw), &preview); err != nil || len(preview.MatchedURL) == 0 { + var previews []BeeperLinkPreview + if err := json.Unmarshal([]byte(rawPreview.Raw), &previews); err != nil { + return + } + // WhatsApp only supports a single preview. + preview := previews[0] + if len(preview.MatchedURL) == 0 { return } From bc6b9266a950ba2c35d1c54f4d4af6aff999cb51 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Sun, 6 Feb 2022 17:53:49 -0700 Subject: [PATCH 2/3] link previews: fix logic if there are no previews Co-authored-by: Tulir Asokan --- urlpreview.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/urlpreview.go b/urlpreview.go index f00d2df..12d80ab 100644 --- a/urlpreview.go +++ b/urlpreview.go @@ -120,7 +120,7 @@ func (portal *Portal) convertURLPreviewToWhatsApp(sender *User, evt *event.Event return } var previews []BeeperLinkPreview - if err := json.Unmarshal([]byte(rawPreview.Raw), &previews); err != nil { + if err := json.Unmarshal([]byte(rawPreview.Raw), &previews); err != nil || len(previews) == 0 { return } // WhatsApp only supports a single preview. From b6352c4991324148603f1c051fc0907be5339c36 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Sun, 6 Feb 2022 22:30:00 -0700 Subject: [PATCH 3/3] link previews: only include in Matrix event when there's an actual link --- portal.go | 4 +--- urlpreview.go | 8 ++++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/portal.go b/portal.go index a484516..e86b859 100644 --- a/portal.go +++ b/portal.go @@ -1510,9 +1510,7 @@ func (portal *Portal) convertTextMessage(intent *appservice.IntentAPI, source *U } expiresIn = contextInfo.GetExpiration() - extraAttrs["com.beeper.linkpreviews"] = []*BeeperLinkPreview{ - portal.convertURLPreviewToBeeper(intent, source, msg.GetExtendedTextMessage()), - } + extraAttrs["com.beeper.linkpreviews"] = portal.convertURLPreviewToBeeper(intent, source, msg.GetExtendedTextMessage()) } return &ConvertedMessage{ diff --git a/urlpreview.go b/urlpreview.go index 12d80ab..829e298 100644 --- a/urlpreview.go +++ b/urlpreview.go @@ -52,12 +52,12 @@ type BeeperLinkPreview struct { ImageType string `json:"og:image:type,omitempty"` } -func (portal *Portal) convertURLPreviewToBeeper(intent *appservice.IntentAPI, source *User, msg *waProto.ExtendedTextMessage) (output *BeeperLinkPreview) { +func (portal *Portal) convertURLPreviewToBeeper(intent *appservice.IntentAPI, source *User, msg *waProto.ExtendedTextMessage) []*BeeperLinkPreview { if msg.GetMatchedText() == "" { - return + return []*BeeperLinkPreview{} } - output = &BeeperLinkPreview{ + output := &BeeperLinkPreview{ MatchedURL: msg.GetMatchedText(), CanonicalURL: msg.GetCanonicalUrl(), Title: msg.GetTitle(), @@ -111,7 +111,7 @@ func (portal *Portal) convertURLPreviewToBeeper(intent *appservice.IntentAPI, so output.Type = "video.other" } - return + return []*BeeperLinkPreview{output} } func (portal *Portal) convertURLPreviewToWhatsApp(sender *User, evt *event.Event, dest *waProto.ExtendedTextMessage) {