From dad2fc29ab1759b61d7d5aed242c446456730da2 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 27 Aug 2018 23:15:05 +0300 Subject: [PATCH] Send captions in a different message and improve other things --- formatting.go | 6 +++++- portal.go | 40 ++++++++++++++++++++++++++++++---------- user.go | 9 ++++++++- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/formatting.go b/formatting.go index 65d4b7a..edafb2b 100644 --- a/formatting.go +++ b/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(`%s`, puppet.MXID, puppet.Displayname) + mxid := puppet.MXID + if jid == user.JID() { + mxid = user.ID + } + return fmt.Sprintf(`%s`, mxid, puppet.Displayname) }, } } diff --git a/portal.go b/portal.go index 1e81ca4..0bafbb7 100644 --- a/portal.go +++ b/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) } diff --git a/user.go b/user.go index e29442d..6acb81b 100644 --- a/user.go +++ b/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() {