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

View File

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

View File

@ -613,6 +613,48 @@ describe Whatsapp::IncomingMessageBaileysService do
expect(attachment.file.content_type).to eq('image/png') expect(attachment.file.content_type).to eq('image/png')
end end
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 end
context 'when processing messages.update event' do context 'when processing messages.update event' do