# == Schema Information
#
# Table name: message_templates
#
#  id               :integer          not null, primary key
#  type             :string(255)
#  sys_notice       :text(65535)
#  email            :text(65535)
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#  notification_url :string(255)
#  email_title      :string(255)
#

# 我管理的仓库被复刻
class MessageTemplate::ProjectForked < MessageTemplate

  # MessageTemplate::ProjectForked.get_message_content(User.where(login: 'yystopf'))
  def self.get_message_content(receivers, user, project)
    receivers.each do |receiver|
      if receiver.user_template_message_setting.present? 
        send_setting = receiver.user_template_message_setting.notification_body["ManageProject::Forked"]
        send_setting = send_setting.nil? ? UserTemplateMessageSetting.init_notification_body["ManageProject::Forked"] : send_setting
        receivers = receivers.where.not(id: receiver.id) unless send_setting
      end
    end
    return '', '', '' if receivers.blank?
    content = sys_notice.gsub('{nickname1}', user&.real_name).gsub('{nickname2}', project&.owner&.real_name).gsub('{repository}', project&.name)
    url = notification_url.gsub('{owner}', user&.login).gsub('{identifier}', project&.identifier)
    return receivers_string(receivers), content, url
  rescue => e
    Rails.logger.info("MessageTemplate::ProjectForked.get_message_content [ERROR] #{e}")
    return '', '', ''
  end
end
