Skip to content

Commit

Permalink
Refactored message replies.
Browse files Browse the repository at this point in the history
file/Image/audio/replies all work now.
  • Loading branch information
yousefmansy1 committed Feb 24, 2023
1 parent cffa6d3 commit 264ab84
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 59 deletions.
34 changes: 16 additions & 18 deletions bridge/whatsappmulti/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto.
}
}

parentID := ""
if msg.GetExtendedTextMessage() != nil {
ci := msg.GetExtendedTextMessage().GetContextInfo()
parentID = getParentIdFromCtx(ci)
}

rmsg := config.Message{
UserID: senderJID.String(),
Username: senderName,
Expand All @@ -94,12 +100,8 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto.
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
ID: getMessageIdFormat(senderJID.String(), messageInfo.ID),
}

if msg.GetExtendedTextMessage() != nil {
ci := msg.GetExtendedTextMessage().GetContextInfo()
appendParentID(ci, &rmsg)
ID: getMessageIdFormat(senderJID, messageInfo.ID),
ParentID: parentID,
}

if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
Expand Down Expand Up @@ -131,11 +133,10 @@ func (b *Bwhatsapp) handleImageMessage(msg *events.Message) {
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
ID: getMessageIdFormat(senderJID.String(), msg.Info.ID),
ID: getMessageIdFormat(senderJID, msg.Info.ID),
ParentID: getParentIdFromCtx(ci),
}

appendParentID(ci, &rmsg)

if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
rmsg.Avatar = avatarURL
}
Expand Down Expand Up @@ -196,11 +197,10 @@ func (b *Bwhatsapp) handleVideoMessage(msg *events.Message) {
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
ID: getMessageIdFormat(senderJID.String(), msg.Info.ID),
ID: getMessageIdFormat(senderJID, msg.Info.ID),
ParentID: getParentIdFromCtx(ci),
}

appendParentID(ci, &rmsg)

if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
rmsg.Avatar = avatarURL
}
Expand Down Expand Up @@ -254,11 +254,10 @@ func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) {
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
ID: getMessageIdFormat(senderJID.String(), msg.Info.ID),
ID: getMessageIdFormat(senderJID, msg.Info.ID),
ParentID: getParentIdFromCtx(ci),
}

appendParentID(ci, &rmsg)

if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
rmsg.Avatar = avatarURL
}
Expand Down Expand Up @@ -313,11 +312,10 @@ func (b *Bwhatsapp) handleDocumentMessage(msg *events.Message) {
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
ID: getMessageIdFormat(senderJID.String(), msg.Info.ID),
ID: getMessageIdFormat(senderJID, msg.Info.ID),
ParentID: getParentIdFromCtx(ci),
}

appendParentID(ci, &rmsg)

if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
rmsg.Avatar = avatarURL
}
Expand Down
37 changes: 30 additions & 7 deletions bridge/whatsappmulti/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"strings"

"github.com/42wim/matterbridge/bridge/config"
goproto "google.golang.org/protobuf/proto"

"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/binary/proto"
Expand Down Expand Up @@ -126,12 +126,21 @@ func (b *Bwhatsapp) getDevice() (*store.Device, error) {
return device, nil
}

func appendParentID(ci *proto.ContextInfo, rmsg *config.Message) {
func (b *Bwhatsapp) getNewReplyContext(parentID string) (*proto.ContextInfo, error) {
replyInfo, err := b.parseMessageID(parentID)

if ci != nil && ci.StanzaId != nil {
// rmsg.ParentID = *ci.StanzaId
rmsg.ParentID = getMessageIdFormat(*ci.Participant, *ci.StanzaId)
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) {
Expand Down Expand Up @@ -159,6 +168,20 @@ func (b *Bwhatsapp) parseMessageID(id string) (*Replyable, error) {
return &Replyable{MessageID: id}, err
}

func getMessageIdFormat(authorJID string, messageID string) string {
return fmt.Sprintf("%s/%s", authorJID, messageID)
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)
}
79 changes: 45 additions & 34 deletions bridge/whatsappmulti/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ func (b *Bwhatsapp) PostDocumentMessage(msg config.Message, filetype string) (st

// Post document message
var message proto.Message
var ctx *proto.ContextInfo
if msg.ParentID != "" {
ctx, _ = b.getNewReplyContext(msg.ParentID)
}

message.DocumentMessage = &proto.DocumentMessage{
Title: &fi.Name,
Expand All @@ -238,6 +242,7 @@ func (b *Bwhatsapp) PostDocumentMessage(msg config.Message, filetype string) (st
FileSha256: resp.FileSHA256,
FileLength: goproto.Uint64(resp.FileLength),
Url: &resp.URL,
ContextInfo: ctx,
}

b.Log.Debugf("=> Sending %#v as a document", msg)
Expand All @@ -261,6 +266,10 @@ func (b *Bwhatsapp) PostImageMessage(msg config.Message, filetype string) (strin
}

var message proto.Message
var ctx *proto.ContextInfo
if msg.ParentID != "" {
ctx, _ = b.getNewReplyContext(msg.ParentID)
}

message.ImageMessage = &proto.ImageMessage{
Mimetype: &filetype,
Expand All @@ -270,6 +279,7 @@ func (b *Bwhatsapp) PostImageMessage(msg config.Message, filetype string) (strin
FileSha256: resp.FileSHA256,
FileLength: goproto.Uint64(resp.FileLength),
Url: &resp.URL,
ContextInfo: ctx,
}

b.Log.Debugf("=> Sending %#v as an image", msg)
Expand All @@ -289,6 +299,10 @@ func (b *Bwhatsapp) PostVideoMessage(msg config.Message, filetype string) (strin
}

var message proto.Message
var ctx *proto.ContextInfo
if msg.ParentID != "" {
ctx, _ = b.getNewReplyContext(msg.ParentID)
}

message.VideoMessage = &proto.VideoMessage{
Mimetype: &filetype,
Expand All @@ -298,6 +312,7 @@ func (b *Bwhatsapp) PostVideoMessage(msg config.Message, filetype string) (strin
FileSha256: resp.FileSHA256,
FileLength: goproto.Uint64(resp.FileLength),
Url: &resp.URL,
ContextInfo: ctx,
}

b.Log.Debugf("=> Sending %#v as a video", msg)
Expand All @@ -317,6 +332,10 @@ func (b *Bwhatsapp) PostAudioMessage(msg config.Message, filetype string) (strin
}

var message proto.Message
var ctx *proto.ContextInfo
if msg.ParentID != "" {
ctx, _ = b.getNewReplyContext(msg.ParentID)
}

message.AudioMessage = &proto.AudioMessage{
Mimetype: &filetype,
Expand All @@ -325,6 +344,7 @@ func (b *Bwhatsapp) PostAudioMessage(msg config.Message, filetype string) (strin
FileSha256: resp.FileSHA256,
FileLength: goproto.Uint64(resp.FileLength),
Url: &resp.URL,
ContextInfo: ctx,
}

b.Log.Debugf("=> Sending %#v as audio", msg)
Expand All @@ -341,39 +361,6 @@ func (b *Bwhatsapp) PostAudioMessage(msg config.Message, filetype string) (strin
return ID, err
}

func (b *Bwhatsapp) sendMessage(rmsg config.Message, message *proto.Message) (string, error) {
groupJID, _ := types.ParseJID(rmsg.Channel)
ID := whatsmeow.GenerateMessageID()
text := rmsg.Username + rmsg.Text

// If we have a parent ID send an extended message
if rmsg.ParentID != "" {
replyInfo, err := b.parseMessageID(rmsg.ParentID)

if err == nil {
sender := replyInfo.Sender.String()

// append reply info
message.ExtendedTextMessage = &proto.ExtendedTextMessage{
Text: &text,
ContextInfo: &proto.ContextInfo{
StanzaId: &replyInfo.MessageID,
Participant: &sender,
QuotedMessage: &proto.Message{Conversation: goproto.String("")},
},
}

_, err := b.wc.SendMessage(context.Background(), groupJID, message, whatsmeow.SendRequestExtra{ID: ID})

return getMessageIdFormat(b.Config.GetString("Number")[1:]+"@s.whatsapp.net", ID), err
}
}

_, err := b.wc.SendMessage(context.TODO(), groupJID, message, whatsmeow.SendRequestExtra{ID: ID})

return getMessageIdFormat(b.Config.GetString("Number")[1:]+"@s.whatsapp.net", ID), err
}

// Send a message from the bridge to WhatsApp
func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
groupJID, _ := types.ParseJID(msg.Channel)
Expand Down Expand Up @@ -430,11 +417,35 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
}
}

var message proto.Message
text := msg.Username + msg.Text

var message proto.Message
// If we have a parent ID send an extended message
if msg.ParentID != "" {
replyContext, err := b.getNewReplyContext(msg.ParentID)

if err == nil {
message = proto.Message{
ExtendedTextMessage: &proto.ExtendedTextMessage{
Text: &text,
ContextInfo: replyContext,
},
}

return b.sendMessage(msg, &message)
}
}

message.Conversation = &text

return b.sendMessage(msg, &message)
}

func (b *Bwhatsapp) sendMessage(rmsg config.Message, message *proto.Message) (string, error) {
groupJID, _ := types.ParseJID(rmsg.Channel)
ID := whatsmeow.GenerateMessageID()

_, err := b.wc.SendMessage(context.Background(), groupJID, message, whatsmeow.SendRequestExtra{ID: ID})

return getMessageIdFormat(*b.wc.Store.ID, ID), err
}

0 comments on commit 264ab84

Please sign in to comment.