From 7f6b319bed22c5b135a03e2d98931c9d55252031 Mon Sep 17 00:00:00 2001 From: egalvis Date: Thu, 2 Nov 2023 13:56:46 -0500 Subject: [PATCH 1/4] Added support for WhatsApp status replies --- portal.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/portal.go b/portal.go index c23a3afe..4d58f665 100644 --- a/portal.go +++ b/portal.go @@ -508,8 +508,10 @@ func getMessageType(waMsg *waProto.Message) string { switch { case waMsg == nil: return "ignore" - case waMsg.Conversation != nil, waMsg.ExtendedTextMessage != nil: + case waMsg.Conversation != nil: return "text" + case waMsg.ExtendedTextMessage != nil: + return "post_reply" case waMsg.ImageMessage != nil: return fmt.Sprintf("image %s", waMsg.GetImageMessage().GetMimetype()) case waMsg.StickerMessage != nil: @@ -985,6 +987,19 @@ func (portal *Portal) handleMessage(source *User, evt *events.Message, historica if len(eventID) != 0 { portal.finishHandling(existingMsg, &evt.Info, eventID, intent.UserID, dbMsgType, galleryPart, converted.Error) } + + // post_reply message is a WhatsApp status reply + if msgType == "post_reply" { + converted_quoted_message := portal.convertMessage(intent, source, &evt.Info, evt.Message.GetExtendedTextMessage().GetContextInfo().GetQuotedMessage(), false) + if converted_quoted_message == nil { + portal.log.Warnfln("Failed to convert post_reply message") + return + } + _, err := portal.sendMessage(converted_quoted_message.Intent, converted_quoted_message.Type, converted_quoted_message.Content, converted_quoted_message.Extra, evt.Info.Timestamp.UnixMilli()) + if err != nil { + portal.log.Errorfln("Failed to send %s to Matrix: %v", msgID, err) + } + } } else if msgType == "reaction" || msgType == "encrypted reaction" { if evt.Message.GetEncReactionMessage() != nil { decryptedReaction, err := source.Client.DecryptReaction(evt) From c44e0c44d5b6f6d7ef3627d9cb34d86a2a139a9e Mon Sep 17 00:00:00 2001 From: egalvis Date: Fri, 10 Nov 2023 09:59:47 -0500 Subject: [PATCH 2/4] Changed SetReply method to send quoted messages when replying to other room messages --- portal.go | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/portal.go b/portal.go index 4d58f665..07aef46d 100644 --- a/portal.go +++ b/portal.go @@ -508,10 +508,8 @@ func getMessageType(waMsg *waProto.Message) string { switch { case waMsg == nil: return "ignore" - case waMsg.Conversation != nil: + case waMsg.Conversation != nil, waMsg.ExtendedTextMessage != nil: return "text" - case waMsg.ExtendedTextMessage != nil: - return "post_reply" case waMsg.ImageMessage != nil: return fmt.Sprintf("image %s", waMsg.GetImageMessage().GetMimetype()) case waMsg.StickerMessage != nil: @@ -988,18 +986,6 @@ func (portal *Portal) handleMessage(source *User, evt *events.Message, historica portal.finishHandling(existingMsg, &evt.Info, eventID, intent.UserID, dbMsgType, galleryPart, converted.Error) } - // post_reply message is a WhatsApp status reply - if msgType == "post_reply" { - converted_quoted_message := portal.convertMessage(intent, source, &evt.Info, evt.Message.GetExtendedTextMessage().GetContextInfo().GetQuotedMessage(), false) - if converted_quoted_message == nil { - portal.log.Warnfln("Failed to convert post_reply message") - return - } - _, err := portal.sendMessage(converted_quoted_message.Intent, converted_quoted_message.Type, converted_quoted_message.Content, converted_quoted_message.Extra, evt.Info.Timestamp.UnixMilli()) - if err != nil { - portal.log.Errorfln("Failed to send %s to Matrix: %v", msgID, err) - } - } } else if msgType == "reaction" || msgType == "encrypted reaction" { if evt.Message.GetEncReactionMessage() != nil { decryptedReaction, err := source.Client.DecryptReaction(evt) @@ -2290,6 +2276,30 @@ func (portal *Portal) SetReply(msgID string, content *event.MessageEventContent, content.RelatesTo.InReplyTo.UnstableRoomID = targetPortal.MXID } }() + + if !portal.bridge.Config.Bridge.CrossRoomReplies && !replyTo.Chat.IsEmpty() && replyTo.Chat != key.JID { + if replyTo.Chat.Server == types.GroupServer { + key = database.NewPortalKey(replyTo.Chat, types.EmptyJID) + } else if replyTo.Chat == types.StatusBroadcastJID { + key = database.NewPortalKey(replyTo.Chat, key.Receiver) + } + if key != portal.Key { + targetPortal = portal.bridge.GetExistingPortalByJID(key) + if targetPortal == nil { + return false + } + } + message := portal.bridge.DB.Message.GetByJID(key, replyTo.MessageID) + if message != nil || !message.IsFakeMXID() { + evt, _ := targetPortal.MainIntent().GetEvent(targetPortal.MXID, message.MXID) + _ = evt.Content.ParseRaw(evt.Type) + content.EnsureHasHTML() + content.FormattedBody = evt.GenerateReplyFallbackHTML() + content.FormattedBody + content.Body = evt.GenerateReplyFallbackText() + content.Body + return true + } + } + if portal.bridge.Config.Bridge.CrossRoomReplies && !replyTo.Chat.IsEmpty() && replyTo.Chat != key.JID { if replyTo.Chat.Server == types.GroupServer { key = database.NewPortalKey(replyTo.Chat, types.EmptyJID) From 1fe5e65b733310b6653506809beb2d3fa4e5dd2f Mon Sep 17 00:00:00 2001 From: Esteban Galvis Date: Mon, 18 Dec 2023 11:00:45 -0500 Subject: [PATCH 3/4] fix: :bug: Improved status replies Improvement in status reply code and format style --- portal.go | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/portal.go b/portal.go index 07aef46d..a727fe00 100644 --- a/portal.go +++ b/portal.go @@ -2271,17 +2271,20 @@ func (portal *Portal) SetReply(msgID string, content *event.MessageEventContent, Logger() key := portal.Key targetPortal := portal + reply_format := "" defer func() { if content.RelatesTo != nil && content.RelatesTo.InReplyTo != nil && targetPortal != portal { content.RelatesTo.InReplyTo.UnstableRoomID = targetPortal.MXID } }() - if !portal.bridge.Config.Bridge.CrossRoomReplies && !replyTo.Chat.IsEmpty() && replyTo.Chat != key.JID { + if !replyTo.Chat.IsEmpty() && replyTo.Chat != portal.Key.JID { if replyTo.Chat.Server == types.GroupServer { key = database.NewPortalKey(replyTo.Chat, types.EmptyJID) + reply_format = "Replying to [a message] in a group : %s" } else if replyTo.Chat == types.StatusBroadcastJID { - key = database.NewPortalKey(replyTo.Chat, key.Receiver) + key = database.NewPortalKey(replyTo.Chat, portal.Key.Receiver) + reply_format = "Replying to [a status] : %s" } if key != portal.Key { targetPortal = portal.bridge.GetExistingPortalByJID(key) @@ -2289,32 +2292,17 @@ func (portal *Portal) SetReply(msgID string, content *event.MessageEventContent, return false } } - message := portal.bridge.DB.Message.GetByJID(key, replyTo.MessageID) - if message != nil || !message.IsFakeMXID() { - evt, _ := targetPortal.MainIntent().GetEvent(targetPortal.MXID, message.MXID) - _ = evt.Content.ParseRaw(evt.Type) + } + message := portal.bridge.DB.Message.GetByJID(portal.Key, replyTo.MessageID) + if message == nil || message.IsFakeMXID() { + if portal != targetPortal && !portal.bridge.Config.Bridge.CrossRoomReplies { + message := portal.bridge.DB.Message.GetByJID(key, replyTo.MessageID) + link := targetPortal.MXID.EventURI(message.MXID, portal.bridge.Config.Homeserver.Domain).MatrixToURL() content.EnsureHasHTML() - content.FormattedBody = evt.GenerateReplyFallbackHTML() + content.FormattedBody - content.Body = evt.GenerateReplyFallbackText() + content.Body + content.FormattedBody = fmt.Sprintf(reply_format, link, content.FormattedBody) + content.Body = fmt.Sprintf(reply_format, link, content.Body) return true } - } - - if portal.bridge.Config.Bridge.CrossRoomReplies && !replyTo.Chat.IsEmpty() && replyTo.Chat != key.JID { - if replyTo.Chat.Server == types.GroupServer { - key = database.NewPortalKey(replyTo.Chat, types.EmptyJID) - } else if replyTo.Chat == types.StatusBroadcastJID { - key = database.NewPortalKey(replyTo.Chat, key.Receiver) - } - if key != portal.Key { - targetPortal = portal.bridge.GetExistingPortalByJID(key) - if targetPortal == nil { - return false - } - } - } - message := portal.bridge.DB.Message.GetByJID(key, replyTo.MessageID) - if message == nil || message.IsFakeMXID() { if isHungryBackfill { content.RelatesTo = (&event.RelatesTo{}).SetReplyTo(targetPortal.deterministicEventID(replyTo.Sender, replyTo.MessageID, "")) portal.addReplyMention(content, replyTo.Sender, "") From bf78f58d2a828e5eb489673a8dbb4ee7edb724a3 Mon Sep 17 00:00:00 2001 From: Esteban Galvis Date: Mon, 18 Dec 2023 11:03:21 -0500 Subject: [PATCH 4/4] style: :rotating_light: Remove unnecessary extra newline --- portal.go | 1 - 1 file changed, 1 deletion(-) diff --git a/portal.go b/portal.go index a727fe00..ff3017b1 100644 --- a/portal.go +++ b/portal.go @@ -985,7 +985,6 @@ func (portal *Portal) handleMessage(source *User, evt *events.Message, historica if len(eventID) != 0 { portal.finishHandling(existingMsg, &evt.Info, eventID, intent.UserID, dbMsgType, galleryPart, converted.Error) } - } else if msgType == "reaction" || msgType == "encrypted reaction" { if evt.Message.GetEncReactionMessage() != nil { decryptedReaction, err := source.Client.DecryptReaction(evt)