feat: add flag to edited messages and content history tracking (#35)
* fix: handle protocol messages in IncomingMessageBaileysService and ignore it * fix: ignore protocol messages before create conversation and contact * feat: add flag to edited messages and content history tracking * refactor: update message model to use edit_history instead of content_history for tracking edits * refactor: update message model to track previous content instead of edit history * test: fix message content update test to correctly track original content --------- Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
This commit is contained in:
parent
62ef2113d5
commit
8d5a6bc4b5
@ -102,9 +102,12 @@ class Message < ApplicationRecord
|
|||||||
# [:external_created_at] : Can specify if the message was created at a different timestamp externally
|
# [:external_created_at] : Can specify if the message was created at a different timestamp externally
|
||||||
# [:external_error : Can specify if the message creation failed due to an error at external API
|
# [:external_error : Can specify if the message creation failed due to an error at external API
|
||||||
# [:is_reaction] : Used to denote if the message is a reaction and differentiate it from a simple reply message
|
# [:is_reaction] : Used to denote if the message is a reaction and differentiate it from a simple reply message
|
||||||
|
# [:is_edited, :previous_content] : Used to indicated edited message and previous content (before edit)
|
||||||
|
|
||||||
store :content_attributes, accessors: [:submitted_email, :items, :submitted_values, :email, :in_reply_to, :deleted,
|
store :content_attributes, accessors: [:submitted_email, :items, :submitted_values, :email, :in_reply_to, :deleted,
|
||||||
:external_created_at, :story_sender, :story_id, :external_error,
|
:external_created_at, :story_sender, :story_id, :external_error,
|
||||||
:translations, :in_reply_to_external_id, :is_unsupported, :is_reaction], coder: JSON
|
:translations, :in_reply_to_external_id, :is_unsupported,
|
||||||
|
:is_reaction, :is_edited, :previous_content], coder: JSON
|
||||||
|
|
||||||
store :external_source_ids, accessors: [:slack], coder: JSON, prefix: :external_source_id
|
store :external_source_ids, accessors: [:slack], coder: JSON, prefix: :external_source_id
|
||||||
|
|
||||||
|
|||||||
@ -277,7 +277,7 @@ class Whatsapp::IncomingMessageBaileysService < Whatsapp::IncomingMessageBaseSer
|
|||||||
raise MessageNotFoundError unless find_message_by_source_id(message_id)
|
raise MessageNotFoundError unless find_message_by_source_id(message_id)
|
||||||
|
|
||||||
update_status if @raw_message.dig(:update, :status).present?
|
update_status if @raw_message.dig(:update, :status).present?
|
||||||
update_message_content if @raw_message.dig(:update, :message).present?
|
handle_edited_content if @raw_message.dig(:update, :message).present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_status
|
def update_status
|
||||||
@ -318,15 +318,15 @@ class Whatsapp::IncomingMessageBaileysService < Whatsapp::IncomingMessageBaseSer
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_message_content
|
def handle_edited_content
|
||||||
message = @raw_message.dig(:update, :message, :editedMessage, :message)
|
@raw_message = @raw_message.dig(:update, :message, :editedMessage)
|
||||||
if message.blank?
|
content = message_content
|
||||||
Rails.logger.warn 'No valid message content found in the update event'
|
|
||||||
|
unless content
|
||||||
|
Rails.logger.warn 'No valid message content found in the edit event'
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
content = message[:conversation] || message.dig(:extendedTextMessage, :text)
|
@message.update!(content: content, is_edited: true, previous_content: @message.content)
|
||||||
|
|
||||||
@message.update!(content: content) if content.present?
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -726,7 +726,10 @@ describe Whatsapp::IncomingMessageBaileysService do
|
|||||||
|
|
||||||
described_class.new(inbox: inbox, params: params).perform
|
described_class.new(inbox: inbox, params: params).perform
|
||||||
|
|
||||||
|
original_content = message.content
|
||||||
expect(message.reload.content).to eq('New message content')
|
expect(message.reload.content).to eq('New message content')
|
||||||
|
expect(message.is_edited).to be(true)
|
||||||
|
expect(message.previous_content).to eq(original_content)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user