       0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0
      ˆÂ‘äÇò 8àIÌ³(•ÿçÁƒh¹À¿¾      0
      ˆÂ‘äÇò 8àIÌ³(•ÿçÁƒh¹À¿¾             0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0
      ˆÂ‘äÇò 8àIÌ³+Â•ÿçÁƒh¹À¿¾      30
      	�236_•I|¥‰ÓMjqØ‚¦ ë<óo¬À¿  ì    	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>
  4   ˆl–Ü4ý(TÊ:”Ê�vàŸ¸'TÅ£Žþ_ifåå(|q�ÏÃ148ÂÁÀ  ”    ‹      ‹àb```f`abb`f2XCCÜt-˜„Ášˆy�˜‰x0 u€ÅY8AšòsS‹�$A×°¼ü¢ÜÄ ûÔ ˆFÆÿPµ9.¨%ŒhòÌP³8Ë‹2“r€–@¸ŠRÓR‹Ró’"Éù¹E©ÅÅ©) C^œ¯Ù         ˆÄ‘äÇò 8àIÌ´Èßx?ÏÃƒh¹ÂÁÀ      0
      ˆÄ‘äÇò 8àIÌ´Ø@VŸÃƒh¹ÂÁÀ      0
      ˆÄ‘äÇò 8àIÌ´ã¯4çóÃƒh¹ÂÁÀ      0
  N   ˆl–Ãa¾”J_)â€~àŸ¸'ÔÅ£‘äÇò 8àIÌ´ì€ßÏÄƒh¹Ãa–Ãa¾”J_)â€~àŸ¸'ÔÅ£Â      scsi:t-0x05
  [   ˆl–ßi~”Šj"Tâ‚Æ[¸ØÅ£�þBÐ†³�ÈÈed¤—ùÆƒl.Å_�uÐb&=LyZËcõa–Ãa¾”J_)â€~àŸ¸ËÊbÑ¿Å *    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

class Service::Ticket::Article::Create < Service::Base
  requires_current_user!

  attr_reader :article_data, :ticket

  def initialize(article_data:, ticket:)
    @article_data = article_data
    @ticket = ticket
  end

  def execute
    article_data.delete(:ticket_id)

    attachments_raw     = article_data.delete(:attachments) || {}
    time_unit           = article_data.delete(:time_unit)
    accounted_time_type = article_data.delete(:accounted_time_type)
    subtype             = article_data.delete(:subtype)

    preprocess_article_data(article_data, ticket)

    ticket.articles.new(article_data).tap do |article|
      article.check_mentions_raises_error = true
      article.check_email_recipient_raises_error = true

      transform_article(article, attachments_raw, subtype)

      # Article and accounted time are stored in one transaction, so that the
      #   'after_create_commit' subscription trigger of the article fires only once
      #   the accounted time is available for the clients as well.
      ActiveRecord::Base.transaction do
        article.save!

        time_accounting(article, time_unit, accounted_time_type)
      end

      form_id_cleanup(attachments_raw)
    end
  end

  private

  def preprocess_article_data(article_data, ticket)
    preprocess_type(article_data)
    preprocess_to_cc(article_data)
    preprocess_sender(article_data, ticket)
    preprocess_for_customer(article_data, ticket)
  end

  def preprocess_type(article_data)
    type_name = article_data[:type] || 'note'

    article_data[:type] = Ticket::Article::Type.lookup(name: type_name)
  end

  def preprocess_to_cc(article_data)
    %i[to cc].each do |field|
      article_data[field] = article_data[field].join(', ') if article_data[field].is_a? Array
      article_data[field] ||= ''
    end
  end

  def preprocess_sender(article_data, ticket)
    sender_name = if agent_on_ticket?(ticket)
                    article_data[:sender].presence || 'Agent'
                  else
                    'Customer'
                  end

    article_data[:sender] = Ticket::Article::Sender.lookup(name: sender_name)
  end

  def preprocess_for_customer(article_data, ticket)
    return if agent_on_ticket?(ticket)

    if %w[note web].exclude? article_data[:type]&.name
      article_data[:type] = Ticket::Article::Type.lookup(name: 'note')
    end

    article_data.delete :origin_by_id

    article_data[:internal] = false

    # Article preferences are internal metadata; only trusted (agent) callers may set them.
    article_data.delete :preferences
  end

  def transform_article(article, attachments_raw, subtype)
    transform_attachments(article, attachments_r