#475 Handle notifications inside WhatsApp
Provide meaningful label outside and don't expose user numbers.
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
package bwhatsapp
|
package bwhatsapp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
|
|
||||||
"github.com/Rhymen/go-whatsapp"
|
"github.com/Rhymen/go-whatsapp"
|
||||||
|
|
||||||
|
whatsappExt "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -41,16 +44,24 @@ func (b *Bwhatsapp) HandleTextMessage(message whatsapp.TextMessage) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// translate sender's Jid to the nicest username we can get
|
// translate sender's Jid to the nicest username we can get
|
||||||
senderName := senderJid
|
senderName := b.getSenderName(senderJid)
|
||||||
if sender, exists := b.users[senderJid]; exists {
|
if senderName == "" {
|
||||||
if sender.Name != "" {
|
senderName = "Someone" // don't expose telephone number
|
||||||
senderName = sender.Name
|
}
|
||||||
|
|
||||||
} else {
|
extText := message.Info.Source.Message.ExtendedTextMessage
|
||||||
// if user is not in phone contacts
|
if extText != nil && extText.ContextInfo != nil && extText.ContextInfo.MentionedJid != nil {
|
||||||
// it is the most obvious scenario unless you sync your phone contacts with some remote updated source
|
// handle user mentions
|
||||||
// users can change it in their WhatsApp settings -> profile -> click on Avatar
|
for _, mentionedJid := range extText.ContextInfo.MentionedJid {
|
||||||
senderName = sender.Notify
|
numberAndSuffix := strings.SplitN(mentionedJid, "@", 2)
|
||||||
|
|
||||||
|
// mentions comes as telephone numbers and we don't want to expose it to other bridges
|
||||||
|
// replace it with something more meaninful to others
|
||||||
|
mention := b.getSenderNotify(numberAndSuffix[0] + whatsappExt.NewUserSuffix)
|
||||||
|
if mention == "" {
|
||||||
|
mention = "someone"
|
||||||
|
}
|
||||||
|
message.Text = strings.Replace(message.Text, "@"+numberAndSuffix[0], "@"+mention, 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,3 +62,25 @@ func (b *Bwhatsapp) writeSession(session whatsapp.Session) error {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bwhatsapp) getSenderName(senderJid string) string {
|
||||||
|
if sender, exists := b.users[senderJid]; exists {
|
||||||
|
if sender.Name != "" {
|
||||||
|
return sender.Name
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// if user is not in phone contacts
|
||||||
|
// it is the most obvious scenario unless you sync your phone contacts with some remote updated source
|
||||||
|
// users can change it in their WhatsApp settings -> profile -> click on Avatar
|
||||||
|
return sender.Notify
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bwhatsapp) getSenderNotify(senderJid string) string {
|
||||||
|
if sender, exists := b.users[senderJid]; exists {
|
||||||
|
return sender.Notify
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user