Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix the "Someone" nickname problem (whatsapp) #1931

Merged
merged 6 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bridge/whatsappmulti/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto.
senderJID := messageInfo.Sender
channel := messageInfo.Chat

senderName := b.getSenderName(messageInfo.Sender)
senderName := b.getSenderName(messageInfo)
if senderName == "" {
senderName = "Someone" // don't expose telephone number
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (b *Bwhatsapp) handleImageMessage(msg *events.Message) {
imsg := msg.Message.GetImageMessage()

senderJID := msg.Info.Sender
senderName := b.getSenderName(senderJID)
senderName := b.getSenderName(msg.Info)
ci := imsg.GetContextInfo()

if senderJID == (types.JID{}) && ci.Participant != nil {
Expand Down Expand Up @@ -181,7 +181,7 @@ func (b *Bwhatsapp) handleVideoMessage(msg *events.Message) {
imsg := msg.Message.GetVideoMessage()

senderJID := msg.Info.Sender
senderName := b.getSenderName(senderJID)
senderName := b.getSenderName(msg.Info)
ci := imsg.GetContextInfo()

if senderJID == (types.JID{}) && ci.Participant != nil {
Expand Down Expand Up @@ -238,7 +238,7 @@ func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) {
imsg := msg.Message.GetAudioMessage()

senderJID := msg.Info.Sender
senderName := b.getSenderName(senderJID)
senderName := b.getSenderName(msg.Info)
ci := imsg.GetContextInfo()

if senderJID == (types.JID{}) && ci.Participant != nil {
Expand Down Expand Up @@ -295,7 +295,7 @@ func (b *Bwhatsapp) handleDocumentMessage(msg *events.Message) {
imsg := msg.Message.GetDocumentMessage()

senderJID := msg.Info.Sender
senderName := b.getSenderName(senderJID)
senderName := b.getSenderName(msg.Info)
ci := imsg.GetContextInfo()

if senderJID == (types.JID{}) && ci.Participant != nil {
Expand Down
60 changes: 28 additions & 32 deletions bridge/whatsappmulti/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,46 @@ type ProfilePicInfo struct {
Status int16 `json:"status"`
}

func (b *Bwhatsapp) getSenderName(senderJid types.JID) string {
if sender, exists := b.contacts[senderJid]; exists {
if sender.FullName != "" {
return sender.FullName
func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method Bwhatsapp.getSenderName has a Cognitive Complexity of 21 (exceeds 20 allowed). Consider refactoring.

// Parse AD JID
var senderJid types.JID
senderJid.User, senderJid.Server = info.Sender.User, info.Sender.Server

for i := 0; i < 2; i++ {
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 info.PushName != "" {
return info.PushName
}

if sender.FirstName != "" {
return sender.FirstName
if sender, exists := b.contacts[senderJid]; exists {
if sender.FirstName != "" {
return sender.FirstName
}
}
}

// try to reload this contact
if _, err := b.wc.Store.Contacts.GetAllContacts(); err != nil {
b.Log.Errorf("error on update of contacts: %v", err)
}

allcontacts, err := b.wc.Store.Contacts.GetAllContacts()
if err != nil {
b.Log.Errorf("error on update of contacts: %v", err)
}

if len(allcontacts) > 0 {
b.contacts = allcontacts
}
if i > 0 {
break
}

if sender, exists := b.contacts[senderJid]; exists {
if sender.FullName != "" {
return sender.FullName
// try to reload this contact
if _, err := b.wc.Store.Contacts.GetAllContacts(); err != nil {
b.Log.Errorf("error on update of contacts: %v", err)
}
// 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

allcontacts, err := b.wc.Store.Contacts.GetAllContacts()
if err != nil {
b.Log.Errorf("error on update of contacts: %v", err)
}

if sender.FirstName != "" {
return sender.FirstName
if len(allcontacts) > 0 {
b.contacts = allcontacts
}
}

Expand Down