Эх сурвалжийг харах

Send captions in a different message and improve other things

Tulir Asokan 6 жил өмнө
parent
commit
dad2fc29ab
3 өөрчлөгдсөн 43 нэмэгдсэн , 12 устгасан
  1. 5 1
      formatting.go
  2. 30 10
      portal.go
  3. 8 1
      user.go

+ 5 - 1
formatting.go

@@ -80,7 +80,11 @@ func (user *User) newWhatsAppFormatMaps() (map[*regexp.Regexp]string, map[*regex
 		mentionRegex: func(str string) string {
 			jid := str[1:] + whatsappExt.NewUserSuffix
 			puppet := user.GetPuppetByJID(jid)
-			return fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, puppet.MXID, puppet.Displayname)
+			mxid := puppet.MXID
+			if jid == user.JID() {
+				mxid = user.ID
+			}
+			return fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, mxid, puppet.Displayname)
 		},
 	}
 }

+ 30 - 10
portal.go

@@ -497,23 +497,22 @@ func (portal *Portal) HandleMediaMessage(download func() ([]byte, error), thumbn
 		portal.log.Errorln("Failed to upload media:", err)
 		return
 	}
-	if len(caption) == 0 {
-		caption = info.Id
-		exts, _ := mime.ExtensionsByType(mimeType)
-		if exts != nil && len(exts) > 0 {
-			caption += exts[0]
-		}
+
+	fileName := info.Id
+	exts, _ := mime.ExtensionsByType(mimeType)
+	if exts != nil && len(exts) > 0 {
+		fileName += exts[0]
 	}
 
-	content := gomatrix.Content{
-		Body: caption,
+	content := &gomatrix.Content{
+		Body: fileName,
 		URL:  uploaded.ContentURI,
 		Info: &gomatrix.FileInfo{
 			Size:     len(data),
 			MimeType: mimeType,
 		},
 	}
-	portal.SetReply(&content, info)
+	portal.SetReply(content, info)
 
 	if thumbnail != nil {
 		thumbnailMime := http.DetectContentType(thumbnail)
@@ -545,11 +544,32 @@ func (portal *Portal) HandleMediaMessage(download func() ([]byte, error), thumbn
 	}
 
 	intent.UserTyping(portal.MXID, false, 0)
-	resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, int64(info.Timestamp*1000))
+	ts := int64(info.Timestamp * 1000)
+	resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, ts)
 	if err != nil {
 		portal.log.Errorfln("Failed to handle message %s: %v", info.Id, err)
 		return
 	}
+
+	if len(caption) > 0 {
+		captionContent := &gomatrix.Content{
+			Body:    caption,
+			MsgType: gomatrix.MsgNotice,
+		}
+
+		htmlBody := portal.ParseWhatsAppFormat(captionContent.Body)
+		if htmlBody != captionContent.Body {
+			captionContent.FormattedBody = htmlBody
+			captionContent.Format = gomatrix.FormatHTML
+		}
+
+		_, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, captionContent, ts)
+		if err != nil {
+			portal.log.Warnfln("Failed to handle caption of message %s: %v", info.Id, err)
+		}
+		// TODO store caption mxid?
+	}
+
 	portal.MarkHandled(info.Id, resp.EventID)
 	portal.log.Debugln("Handled message", info.Id, "->", resp.EventID)
 }

+ 8 - 1
user.go

@@ -40,6 +40,7 @@ type User struct {
 
 	Admin       bool
 	Whitelisted bool
+	jid         string
 
 	portalsByMXID map[types.MatrixRoomID]*Portal
 	portalsByJID  map[types.WhatsAppID]*Portal
@@ -199,7 +200,13 @@ func (user *User) Login(roomID types.MatrixRoomID) {
 }
 
 func (user *User) JID() string {
-	return strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1)
+	if user.Conn == nil {
+		return ""
+	}
+	if len(user.jid) == 0 {
+		user.jid = strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1)
+	}
+	return user.jid
 }
 
 func (user *User) Sync() {