forked from lug/matterbridge
Handle Whatsapp threading/replies (whatsapp) (#1974)
* Handle Whatsapp threading/replies.
In this change we are updating whatsapp message IDs to include sender JID as this is necessary to reply to a message
https://github.com/tulir/whatsmeow/issues/88#issuecomment-1093195237
https://github.com/tulir/whatsmeow/discussions/148#discussioncomment-3094325
Based on commit 6afa93e537 from #1934
Author: Iiro Laiho <iiro.laiho@iki.fi>
* Fix replies.
Sender JID can have a `:` inside of it, using `/` as a delimiter now.
Added messageID parser + struct.
messages sent with an attachment do not show replies
But at least common `sendMessage` will make repies from whatsapp to an attachement bridge across.
The new message ID format broke message deleting, so we change the messageID into the real id at the beginning of send.
We really do need the extra info for when we reply to a message though.
* Refactored message replies.
file/Image/audio/replies all work now.
This commit is contained in:
@@ -7,7 +7,10 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
goproto "google.golang.org/protobuf/proto"
|
||||
|
||||
"go.mau.fi/whatsmeow"
|
||||
"go.mau.fi/whatsmeow/binary/proto"
|
||||
"go.mau.fi/whatsmeow/store"
|
||||
"go.mau.fi/whatsmeow/store/sqlstore"
|
||||
"go.mau.fi/whatsmeow/types"
|
||||
@@ -122,3 +125,63 @@ func (b *Bwhatsapp) getDevice() (*store.Device, error) {
|
||||
|
||||
return device, nil
|
||||
}
|
||||
|
||||
func (b *Bwhatsapp) getNewReplyContext(parentID string) (*proto.ContextInfo, error) {
|
||||
replyInfo, err := b.parseMessageID(parentID)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sender := fmt.Sprintf("%s@%s", replyInfo.Sender.User, replyInfo.Sender.Server)
|
||||
ctx := &proto.ContextInfo{
|
||||
StanzaId: &replyInfo.MessageID,
|
||||
Participant: &sender,
|
||||
QuotedMessage: &proto.Message{Conversation: goproto.String("")},
|
||||
}
|
||||
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (b *Bwhatsapp) parseMessageID(id string) (*Replyable, error) {
|
||||
// No message ID in case action is executed on a message sent before the bridge was started
|
||||
// and then the bridge cache doesn't have this message ID mapped
|
||||
if id == "" {
|
||||
return &Replyable{MessageID: id}, nil
|
||||
}
|
||||
|
||||
replyInfo := strings.Split(id, "/")
|
||||
|
||||
if len(replyInfo) == 2 {
|
||||
sender, err := types.ParseJID(replyInfo[0])
|
||||
|
||||
if err == nil {
|
||||
return &Replyable{
|
||||
MessageID: types.MessageID(replyInfo[1]),
|
||||
Sender: sender,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
err := fmt.Errorf("MessageID does not match format of {senderJID}:{messageID} : \"%s\"", id)
|
||||
|
||||
return &Replyable{MessageID: id}, err
|
||||
}
|
||||
|
||||
func getParentIdFromCtx(ci *proto.ContextInfo) string {
|
||||
if ci != nil && ci.StanzaId != nil {
|
||||
senderJid, err := types.ParseJID(*ci.Participant)
|
||||
|
||||
if err == nil {
|
||||
return getMessageIdFormat(senderJid, *ci.StanzaId)
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func getMessageIdFormat(jid types.JID, messageID string) string {
|
||||
// we're crafting our own JID str as AD JID format messes with how stuff looks on a webclient
|
||||
jidStr := fmt.Sprintf("%s@%s", jid.User, jid.Server)
|
||||
return fmt.Sprintf("%s/%s", jidStr, messageID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user