Prechádzať zdrojové kódy

Merge remote-tracking branch 'origin/sumner/bri-1873'

Tulir Asokan 3 rokov pred
rodič
commit
663abeeb04
2 zmenil súbory, kde vykonal 15 pridanie a 10 odobranie
  1. 2 2
      portal.go
  2. 13 8
      urlpreview.go

+ 2 - 2
portal.go

@@ -1510,7 +1510,7 @@ 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"] = portal.convertURLPreviewToBeeper(intent, source, msg.GetExtendedTextMessage())
 	}
 
 	return &ConvertedMessage{
@@ -2253,7 +2253,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,

+ 13 - 8
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,16 +111,21 @@ 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) {
-	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 previews []BeeperLinkPreview
+	if err := json.Unmarshal([]byte(rawPreview.Raw), &previews); err != nil || len(previews) == 0 {
 		return
 	}
-	var preview BeeperLinkPreview
-	if err := json.Unmarshal([]byte(rawPreview.Raw), &preview); err != nil || len(preview.MatchedURL) == 0 {
+	// WhatsApp only supports a single preview.
+	preview := previews[0]
+	if len(preview.MatchedURL) == 0 {
 		return
 	}