link previews: update to support a list

Uses com.beeper.linkpreviews instead of com.beeper.linkpreview
This commit is contained in:
Sumner Evans 2022-02-04 18:17:01 -07:00
parent 04f8518ad9
commit 711548c0ba
No known key found for this signature in database
GPG key ID: 8904527AB50022FD
2 changed files with 13 additions and 6 deletions

View file

@ -1510,7 +1510,9 @@ func (portal *Portal) convertTextMessage(intent *appservice.IntentAPI, source *U
} }
expiresIn = contextInfo.GetExpiration() 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{ return &ConvertedMessage{
@ -2253,7 +2255,7 @@ func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) (*waP
if content.MsgType == event.MsgEmote && !relaybotFormatted { if content.MsgType == event.MsgEmote && !relaybotFormatted {
text = "/me " + text 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{ msg.ExtendedTextMessage = &waProto.ExtendedTextMessage{
Text: &text, Text: &text,
ContextInfo: &ctxInfo, ContextInfo: &ctxInfo,

View file

@ -115,12 +115,17 @@ func (portal *Portal) convertURLPreviewToBeeper(intent *appservice.IntentAPI, so
} }
func (portal *Portal) convertURLPreviewToWhatsApp(sender *User, evt *event.Event, dest *waProto.ExtendedTextMessage) { func (portal *Portal) convertURLPreviewToWhatsApp(sender *User, evt *event.Event, dest *waProto.ExtendedTextMessage) {
rawPreview := gjson.GetBytes(evt.Content.VeryRaw, `com\.beeper\.linkpreview`) rawPreview := gjson.GetBytes(evt.Content.VeryRaw, `com\.beeper\.linkpreviews`)
if !rawPreview.Exists() || !rawPreview.IsObject() { if !rawPreview.Exists() || !rawPreview.IsArray() {
return return
} }
var preview BeeperLinkPreview var previews []BeeperLinkPreview
if err := json.Unmarshal([]byte(rawPreview.Raw), &preview); err != nil || len(preview.MatchedURL) == 0 { 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 return
} }