fix: remove cache from some baileys helpers (#69)

* fix: remove cache from some baileys helpers

* test: add spec
This commit is contained in:
Gabriel Jablonski 2025-06-16 18:02:29 -03:00 committed by GitHub
parent 0b09542d33
commit a355b53464
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 73 additions and 29 deletions

View File

@ -37,29 +37,29 @@ module Whatsapp::BaileysHandlers::Helpers # rubocop:disable Metrics/ModuleLength
def message_type # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
msg = @raw_message[:message]
@message_type ||= if msg.key?(:conversation) || msg.dig(:extendedTextMessage, :text).present?
'text'
elsif msg.key?(:imageMessage)
'image'
elsif msg.key?(:audioMessage)
'audio'
elsif msg.key?(:videoMessage)
'video'
elsif msg.key?(:documentMessage) || msg.key?(:documentWithCaptionMessage)
'file'
elsif msg.key?(:stickerMessage)
'sticker'
elsif msg.key?(:reactionMessage)
'reaction'
elsif msg.key?(:editedMessage)
'edited'
elsif msg.key?(:protocolMessage)
'protocol'
elsif msg.key?(:messageContextInfo)
'context'
else
'unsupported'
end
if msg.key?(:conversation) || msg.dig(:extendedTextMessage, :text).present?
'text'
elsif msg.key?(:imageMessage)
'image'
elsif msg.key?(:audioMessage)
'audio'
elsif msg.key?(:videoMessage)
'video'
elsif msg.key?(:documentMessage) || msg.key?(:documentWithCaptionMessage)
'file'
elsif msg.key?(:stickerMessage)
'sticker'
elsif msg.key?(:reactionMessage)
'reaction'
elsif msg.key?(:editedMessage)
'edited'
elsif msg.key?(:protocolMessage)
'protocol'
elsif msg.key?(:messageContextInfo)
'context'
else
'unsupported'
end
end
def message_content # rubocop:disable Metrics/CyclomaticComplexity
@ -105,7 +105,7 @@ module Whatsapp::BaileysHandlers::Helpers # rubocop:disable Metrics/ModuleLength
def phone_number_from_jid
# NOTE: jid shape is `<user>_<agent>:<device>@<server>`
# https://github.com/WhiskeySockets/Baileys/blob/v6.7.16/src/WABinary/jid-utils.ts#L19
@phone_number_from_jid ||= @raw_message[:key][:remoteJid].split('@').first.split(':').first.split('_').first
@raw_message[:key][:remoteJid].split('@').first.split(':').first.split('_').first
end
def contact_name

View File

@ -42,17 +42,18 @@ module Whatsapp::BaileysHandlers::MessagesUpsert
def set_contact
push_name = contact_name
source_id = phone_number_from_jid
contact_inbox = ::ContactInboxWithContactBuilder.new(
# FIXME: update the source_id to complete jid in future
source_id: phone_number_from_jid,
source_id: source_id,
inbox: inbox,
contact_attributes: { name: push_name, phone_number: "+#{phone_number_from_jid}" }
contact_attributes: { name: push_name, phone_number: "+#{source_id}" }
).perform
@contact_inbox = contact_inbox
@contact = contact_inbox.contact
@contact.update!(name: push_name) if @contact.name == phone_number_from_jid
@contact.update!(name: push_name) if @contact.name == source_id
end
def handle_create_message
@ -77,11 +78,12 @@ module Whatsapp::BaileysHandlers::MessagesUpsert
end
def message_content_attributes
type = message_type
content_attributes = { external_created_at: baileys_extract_message_timestamp(@raw_message[:messageTimestamp]) }
if message_type == 'reaction'
if type == 'reaction'
content_attributes[:in_reply_to_external_id] = @raw_message.dig(:message, :reactionMessage, :key, :id)
content_attributes[:is_reaction] = true
elsif message_type == 'unsupported'
elsif type == 'unsupported'
content_attributes[:is_unsupported] = true
end

View File

@ -613,6 +613,48 @@ describe Whatsapp::IncomingMessageBaileysService do
expect(attachment.file.content_type).to eq('image/png')
end
end
context 'when processing multiple messages' do
it 'creates separate contacts and conversations for each message' do # rubocop:disable RSpec/ExampleLength
raw_message1 = {
key: { id: 'msg_123', remoteJid: '5511912345678@s.whatsapp.net', fromMe: false },
pushName: 'John Doe',
messageTimestamp: timestamp,
message: { conversation: 'Hello from Baileys' }
}
raw_message2 = {
key: { id: 'msg_124', remoteJid: '5511987654321@s.whatsapp.net', fromMe: false },
pushName: 'Jane Doe',
messageTimestamp: timestamp,
message: { conversation: 'Hello from another user' }
}
params = {
webhookVerifyToken: webhook_verify_token,
event: 'messages.upsert',
data: {
type: 'notify',
messages: [raw_message1, raw_message2]
}
}
described_class.new(inbox: inbox, params: params).perform
conversations = inbox.conversations.order(:created_at)
expect(conversations.count).to eq(2)
first_conversation = conversations.first
first_message = first_conversation.messages.last
expect(first_conversation.contact.name).to eq('John Doe')
expect(first_message.source_id).to eq('msg_123')
expect(first_message.content).to eq('Hello from Baileys')
second_conversation = conversations.second
second_message = second_conversation.messages.last
expect(second_conversation.contact.name).to eq('Jane Doe')
expect(second_message.source_id).to eq('msg_124')
expect(second_message.content).to eq('Hello from another user')
end
end
end
context 'when processing messages.update event' do