Repository: chatwoot/chatwoot
File: app/models/message.rb (Ruby)

Code before:
      { transcribed_text: a.meta&.dig('transcribed_text') }
    end.presence
  end

  def search_content_attributes_data
    {
      email: {
        subject: content_attributes.dig(:email, :subject) || content_attributes.dig('email', 'subject')
      }.compact
    }.compact
  end

  def search_conversation_data
    return nil unless conversation

    {
      id: conversation.id,
      display_id: conversation.display_id,
      status: conversation.status,
      assignee_id: conversation.assignee_id,
      team_id: conversation.team_id,
      contact_id: conversation.contact_id,
      custom_attributes: flatten_custom_attributes(conversation.custom_attributes),
      browser: conversation.additional_attributes&.dig('browser'),
      referer: conversation.additional_attributes&.dig('referer'),
      initated_at: conversation.additional_attributes&.dig('initated_at')
    }
  end

  def search_inbox_data
    return nil unless index

    {
      id: inbox.id,
      name: inbox.name,
      channel_type: inbox.channel_type
    }
  end

  def search_sender_data
    return nil unless sender

    {
      id: sender.id,
      type: sender.class.name,
      name: sender.try(:name),
      email: sender.try(:email),
      phone_number: sender.try(:phone_number),
      custom_attributes: sender.respond_to?(:custom_attributes) ? flatten_custom_attributes(sender.custom_attributes) : []
    }
  end

Review comment (suggestion):
```suggestion
      initated_at: conversation.additional_attributes&.dig('initiated_at')
```

Code after:

  def execute_after_create_commit_callbacks
    # rails issue with order of active record callbacks being executed https://github.com/rails/rails/issues/20911
    reopen_conversation
    set_conversation_activity
    dispatch_create_events
    send_reply
    execute_message_template_hooks
    update_contact_activity
  end

  def update_contact_activity
    sender.update(last_activity_at: DateTime.now) if sender.is_a?(Contact)
  end

  def update_waiting_since
    if human_response? && !private && conversation.waiting_since.present?
      Rails.configuration.dispatcher.dispatch(
        REPLY_CREATED, Time.zone.now, waiting_since: conversation.waiting_since, message: self
      )
      conversation.update(waiting_since: nil)
    end
    conversation.update(waiting_since: created_at) if incoming? && conversation.waiting_since.blank?
  end

  def human_response?
    # if the sender is not a user, it's not a human response
    # if automation rule id is present, it's not a human response
    # if campaign id is present, it's not a human response
    outgoing? &&
      content_attributes['automation_rule_id'].blank? &&
      additional_attributes['campaign_id'].blank? &&
      sender.is_a?(User)
  end

  def dispatch_create_events
    Rails.configuration.dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by)

    if valid_first_reply?
      Rails.configuration.dispatcher.dispatch(FIRST_REPLY_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by)
      conversation.update(first_reply_created_at: created_at, waiting_since: nil)
    else
      update_waiting_since
    end
  end

  def dispatch_update_event
    # ref: https://github.com/rails/rails/issues/44500
    # we want to skip the update event if the message is not updated
    return if previous_changes.blank?