mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-24 05:11:37 +00:00
Fix the "Someone" nickname problem (whatsapp) (#1931)
* Apply patch to work around the "Someone" nickname problem * Code cleanup (whatsapp) * Cleanup * Code cleanup (whatsapp) * Re-examine if sender exists * Cleanup (whatsapp)
This commit is contained in:
parent
91353d0a4d
commit
4cf313c4c6
@ -51,10 +51,7 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto.
|
|||||||
senderJID := messageInfo.Sender
|
senderJID := messageInfo.Sender
|
||||||
channel := messageInfo.Chat
|
channel := messageInfo.Chat
|
||||||
|
|
||||||
senderName := b.getSenderName(messageInfo.Sender)
|
senderName := b.getSenderName(messageInfo)
|
||||||
if senderName == "" {
|
|
||||||
senderName = "Someone" // don't expose telephone number
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.GetExtendedTextMessage() == nil && msg.GetConversation() == "" {
|
if msg.GetExtendedTextMessage() == nil && msg.GetConversation() == "" {
|
||||||
b.Log.Debugf("message without text content? %#v", msg)
|
b.Log.Debugf("message without text content? %#v", msg)
|
||||||
@ -82,9 +79,6 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto.
|
|||||||
// mentions comes as telephone numbers and we don't want to expose it to other bridges
|
// mentions comes as telephone numbers and we don't want to expose it to other bridges
|
||||||
// replace it with something more meaninful to others
|
// replace it with something more meaninful to others
|
||||||
mention := b.getSenderNotify(types.NewJID(numberAndSuffix[0], types.DefaultUserServer))
|
mention := b.getSenderNotify(types.NewJID(numberAndSuffix[0], types.DefaultUserServer))
|
||||||
if mention == "" {
|
|
||||||
mention = "someone"
|
|
||||||
}
|
|
||||||
|
|
||||||
text = strings.Replace(text, "@"+numberAndSuffix[0], "@"+mention, 1)
|
text = strings.Replace(text, "@"+numberAndSuffix[0], "@"+mention, 1)
|
||||||
}
|
}
|
||||||
@ -118,7 +112,7 @@ func (b *Bwhatsapp) handleImageMessage(msg *events.Message) {
|
|||||||
imsg := msg.Message.GetImageMessage()
|
imsg := msg.Message.GetImageMessage()
|
||||||
|
|
||||||
senderJID := msg.Info.Sender
|
senderJID := msg.Info.Sender
|
||||||
senderName := b.getSenderName(senderJID)
|
senderName := b.getSenderName(msg.Info)
|
||||||
ci := imsg.GetContextInfo()
|
ci := imsg.GetContextInfo()
|
||||||
|
|
||||||
if senderJID == (types.JID{}) && ci.Participant != nil {
|
if senderJID == (types.JID{}) && ci.Participant != nil {
|
||||||
@ -181,7 +175,7 @@ func (b *Bwhatsapp) handleVideoMessage(msg *events.Message) {
|
|||||||
imsg := msg.Message.GetVideoMessage()
|
imsg := msg.Message.GetVideoMessage()
|
||||||
|
|
||||||
senderJID := msg.Info.Sender
|
senderJID := msg.Info.Sender
|
||||||
senderName := b.getSenderName(senderJID)
|
senderName := b.getSenderName(msg.Info)
|
||||||
ci := imsg.GetContextInfo()
|
ci := imsg.GetContextInfo()
|
||||||
|
|
||||||
if senderJID == (types.JID{}) && ci.Participant != nil {
|
if senderJID == (types.JID{}) && ci.Participant != nil {
|
||||||
@ -238,7 +232,7 @@ func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) {
|
|||||||
imsg := msg.Message.GetAudioMessage()
|
imsg := msg.Message.GetAudioMessage()
|
||||||
|
|
||||||
senderJID := msg.Info.Sender
|
senderJID := msg.Info.Sender
|
||||||
senderName := b.getSenderName(senderJID)
|
senderName := b.getSenderName(msg.Info)
|
||||||
ci := imsg.GetContextInfo()
|
ci := imsg.GetContextInfo()
|
||||||
|
|
||||||
if senderJID == (types.JID{}) && ci.Participant != nil {
|
if senderJID == (types.JID{}) && ci.Participant != nil {
|
||||||
@ -295,7 +289,7 @@ func (b *Bwhatsapp) handleDocumentMessage(msg *events.Message) {
|
|||||||
imsg := msg.Message.GetDocumentMessage()
|
imsg := msg.Message.GetDocumentMessage()
|
||||||
|
|
||||||
senderJID := msg.Info.Sender
|
senderJID := msg.Info.Sender
|
||||||
senderName := b.getSenderName(senderJID)
|
senderName := b.getSenderName(msg.Info)
|
||||||
ci := imsg.GetContextInfo()
|
ci := imsg.GetContextInfo()
|
||||||
|
|
||||||
if senderJID == (types.JID{}) && ci.Participant != nil {
|
if senderJID == (types.JID{}) && ci.Participant != nil {
|
||||||
|
@ -18,24 +18,7 @@ type ProfilePicInfo struct {
|
|||||||
Status int16 `json:"status"`
|
Status int16 `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bwhatsapp) getSenderName(senderJid types.JID) string {
|
func (b *Bwhatsapp) reloadContacts(){
|
||||||
if sender, exists := b.contacts[senderJid]; exists {
|
|
||||||
if sender.FullName != "" {
|
|
||||||
return sender.FullName
|
|
||||||
}
|
|
||||||
// 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
|
|
||||||
if sender.PushName != "" {
|
|
||||||
return sender.PushName
|
|
||||||
}
|
|
||||||
|
|
||||||
if sender.FirstName != "" {
|
|
||||||
return sender.FirstName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// try to reload this contact
|
|
||||||
if _, err := b.wc.Store.Contacts.GetAllContacts(); err != nil {
|
if _, err := b.wc.Store.Contacts.GetAllContacts(); err != nil {
|
||||||
b.Log.Errorf("error on update of contacts: %v", err)
|
b.Log.Errorf("error on update of contacts: %v", err)
|
||||||
}
|
}
|
||||||
@ -48,32 +31,60 @@ func (b *Bwhatsapp) getSenderName(senderJid types.JID) string {
|
|||||||
if len(allcontacts) > 0 {
|
if len(allcontacts) > 0 {
|
||||||
b.contacts = allcontacts
|
b.contacts = allcontacts
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if sender, exists := b.contacts[senderJid]; exists {
|
func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string {
|
||||||
if sender.FullName != "" {
|
// Parse AD JID
|
||||||
return sender.FullName
|
var senderJid types.JID
|
||||||
}
|
senderJid.User, senderJid.Server = info.Sender.User, info.Sender.Server
|
||||||
// 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
|
|
||||||
if sender.PushName != "" {
|
|
||||||
return sender.PushName
|
|
||||||
}
|
|
||||||
|
|
||||||
if sender.FirstName != "" {
|
sender, exists := b.contacts[senderJid]
|
||||||
return sender.FirstName
|
|
||||||
}
|
if !exists || (sender.FullName == "" && sender.FirstName == "") {
|
||||||
|
b.reloadContacts() // Contacts may need to be reloaded
|
||||||
|
sender, exists = b.contacts[senderJid]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if exists && sender.FullName != "" {
|
||||||
|
return sender.FullName
|
||||||
|
}
|
||||||
|
|
||||||
|
if info.PushName != "" {
|
||||||
|
return info.PushName
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists && sender.FirstName != "" {
|
||||||
|
return sender.FirstName
|
||||||
|
}
|
||||||
|
|
||||||
return "Someone"
|
return "Someone"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bwhatsapp) getSenderNotify(senderJid types.JID) string {
|
func (b *Bwhatsapp) getSenderNotify(senderJid types.JID) string {
|
||||||
if sender, exists := b.contacts[senderJid]; exists {
|
sender, exists := b.contacts[senderJid]
|
||||||
|
|
||||||
|
if !exists || (sender.FullName == "" && sender.PushName == "" && sender.FirstName == "") {
|
||||||
|
b.reloadContacts() // Contacts may need to be reloaded
|
||||||
|
sender, exists = b.contacts[senderJid]
|
||||||
|
}
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
return "someone"
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists && sender.FullName != "" {
|
||||||
|
return sender.FullName
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists && sender.PushName != "" {
|
||||||
return sender.PushName
|
return sender.PushName
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
if exists && sender.FirstName != "" {
|
||||||
|
return sender.FirstName
|
||||||
|
}
|
||||||
|
|
||||||
|
return "someone"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bwhatsapp) GetProfilePicThumb(jid string) (*types.ProfilePictureInfo, error) {
|
func (b *Bwhatsapp) GetProfilePicThumb(jid string) (*types.ProfilePictureInfo, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user