fix(baileys): update LID contact phone number if not present (#129)
* fix(baileys): update LID contact phone number if not present * fix(baileys): move try_update_contact_avatar call to update_contact_information method * refactor(baileys): consolidate contact update logic in update_contact_information method
This commit is contained in:
parent
c234023a4a
commit
a8777f5b04
@ -42,29 +42,28 @@ module Whatsapp::BaileysHandlers::MessagesUpsert
|
||||
end
|
||||
|
||||
def set_contact
|
||||
push_name = contact_name
|
||||
source_id = phone_number_from_jid
|
||||
contact_inbox = ::ContactInboxWithContactBuilder.new(
|
||||
# FIXME: update the source_id to use sender LID
|
||||
source_id: source_id,
|
||||
source_id: phone_number_from_jid,
|
||||
inbox: inbox,
|
||||
contact_attributes: { name: push_name, phone_number: "+#{source_id}", identifier: sender_lid }
|
||||
contact_attributes: { name: contact_name, phone_number: "+#{phone_number_from_jid}", identifier: sender_lid }
|
||||
).perform
|
||||
|
||||
@contact_inbox = contact_inbox
|
||||
@contact = contact_inbox.contact
|
||||
|
||||
@contact.update!(name: push_name) if @contact.name == source_id
|
||||
# NOTE: Backwards compatibility for previous contacts created without identifier.
|
||||
# Should be removed in the distant future, since all contacts should be created with identifier.
|
||||
update_contact_identifier
|
||||
try_update_contact_avatar
|
||||
update_contact_information
|
||||
end
|
||||
|
||||
def update_contact_identifier
|
||||
return if @contact.identifier.present?
|
||||
def update_contact_information
|
||||
updates = {}
|
||||
updates[:identifier] = sender_lid if @contact.identifier.blank?
|
||||
updates[:phone_number] = "+#{phone_number_from_jid}" if @contact.phone_number.blank?
|
||||
updates[:name] = contact_name if @contact.name == phone_number_from_jid || @contact.name == sender_lid
|
||||
|
||||
@contact.update!(identifier: sender_lid)
|
||||
@contact.update!(updates) if updates.present?
|
||||
|
||||
try_update_contact_avatar
|
||||
end
|
||||
|
||||
def handle_create_message
|
||||
|
||||
@ -813,6 +813,53 @@ describe Whatsapp::IncomingMessageBaileysService do
|
||||
expect(contact.reload.identifier).to eq('existing@lid')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when updating contact information' do
|
||||
it 'updates contact phone number if blank' do
|
||||
contact = create(:contact, account: inbox.account, phone_number: nil, identifier: '12345678@lid')
|
||||
create(:contact_inbox, inbox: inbox, contact: contact, source_id: '5511912345678')
|
||||
|
||||
described_class.new(inbox: inbox, params: params).perform
|
||||
|
||||
expect(contact.reload.phone_number).to eq('+5511912345678')
|
||||
end
|
||||
|
||||
it 'does not update contact phone number if already present' do
|
||||
contact = create(:contact, account: inbox.account, phone_number: '+9999999999', identifier: '12345678@lid')
|
||||
create(:contact_inbox, inbox: inbox, contact: contact, source_id: '5511912345678')
|
||||
|
||||
described_class.new(inbox: inbox, params: params).perform
|
||||
|
||||
expect(contact.reload.phone_number).to eq('+9999999999')
|
||||
end
|
||||
|
||||
it 'updates contact name if it matches phone number' do
|
||||
contact = create(:contact, account: inbox.account, name: '5511912345678', phone_number: '+5511912345678', identifier: '12345678@lid')
|
||||
create(:contact_inbox, inbox: inbox, contact: contact, source_id: '5511912345678')
|
||||
|
||||
described_class.new(inbox: inbox, params: params).perform
|
||||
|
||||
expect(contact.reload.name).to eq('John Doe')
|
||||
end
|
||||
|
||||
it 'updates contact name if it matches sender LID' do
|
||||
contact = create(:contact, account: inbox.account, name: '12345678@lid', phone_number: '+5511912345678', identifier: '12345678@lid')
|
||||
create(:contact_inbox, inbox: inbox, contact: contact, source_id: '5511912345678')
|
||||
|
||||
described_class.new(inbox: inbox, params: params).perform
|
||||
|
||||
expect(contact.reload.name).to eq('John Doe')
|
||||
end
|
||||
|
||||
it 'does not update contact name if it is different from phone number and sender LID' do
|
||||
contact = create(:contact, account: inbox.account, name: 'Existing Name', phone_number: '+5511912345678', identifier: '12345678@lid')
|
||||
create(:contact_inbox, inbox: inbox, contact: contact, source_id: '5511912345678')
|
||||
|
||||
described_class.new(inbox: inbox, params: params).perform
|
||||
|
||||
expect(contact.reload.name).to eq('Existing Name')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when processing messages.update event' do
|
||||
|
||||
Loading…
Reference in New Issue
Block a user