From 6c53d06cc225e9b38a0e0454bcc0e8d4d7bae88e Mon Sep 17 00:00:00 2001 From: Krzysztof Madejski Date: Thu, 14 Feb 2019 13:25:16 +0100 Subject: [PATCH] #475 Handle notifications inside WhatsApp Provide meaningful label outside and don't expose user numbers. --- bridge/whatsapp/handlers.go | 29 ++++++++++++++++++++--------- bridge/whatsapp/helpers.go | 22 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/bridge/whatsapp/handlers.go b/bridge/whatsapp/handlers.go index ebf6926f..456f2009 100644 --- a/bridge/whatsapp/handlers.go +++ b/bridge/whatsapp/handlers.go @@ -1,11 +1,14 @@ package bwhatsapp import ( + "strings" "time" "github.com/42wim/matterbridge/bridge/config" "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 - senderName := senderJid - if sender, exists := b.users[senderJid]; exists { - if sender.Name != "" { - senderName = sender.Name + senderName := b.getSenderName(senderJid) + if senderName == "" { + senderName = "Someone" // don't expose telephone number + } - } 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 - senderName = sender.Notify + extText := message.Info.Source.Message.ExtendedTextMessage + if extText != nil && extText.ContextInfo != nil && extText.ContextInfo.MentionedJid != nil { + // handle user mentions + for _, mentionedJid := range extText.ContextInfo.MentionedJid { + 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) } } diff --git a/bridge/whatsapp/helpers.go b/bridge/whatsapp/helpers.go index da83ef99..946fa28b 100644 --- a/bridge/whatsapp/helpers.go +++ b/bridge/whatsapp/helpers.go @@ -62,3 +62,25 @@ func (b *Bwhatsapp) writeSession(session whatsapp.Session) error { 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 "" +}