<commit_before>class Api::ChannelsController < Api::BaseController
  before_action :require_owner,
                only: %i(verify notify show create)

  before_action :require_channel,
                only: %i(verify notify show)

  before_action :ensure_json_content_type,
                only: %i(create)
  before_action :get_current_channel, only: :verification_status

  include PublishersHelper

  def notify
    if params[:type].blank?
      return render(status: 400, json: { message: "parameter 'type' is required" })
    end

    PublisherNotifier.new(
      notification_params: params[:params],
      notification_type: params[:type],
      publisher: @owner,
      channel: @channel
    ).perform
    render(json: { message: "success" })
  rescue PublisherNotifier::InvalidNotificationTypeError => error
    render(json: { message: error.message }, status: 400)
  end

  def verify
    if params[:verificationId].blank?
      return render(status: 400, json: { message: "parameter 'verificationId' is required" })
    end

    if params[:verificationId] != @channel.id
      return render(status: 400, json: { message: "parameter 'verificationId' does not match the channel id" })
    end

    if params[:verified].nil?
      return render(status: 400, json: { message: "parameter 'verified' is required" })
    end

    # Only set verified to true, and only if the channel is not already verified. This keeps verified from flipping
    # back and forth resulting in sending the verified notification repeatedly.
    # Eyeshade calls this endpoint in response to a `GET /v1/owners/{owner}/verify/{publisher}` call.
    # Because of this we will send notification emails from the SiteChannelVerifier instead of here.
    if !@channel.verified && params[:verified]
      success = @channel.update!(verified: true)
    else
      success = true
    end

    if success
      head :no_content
    else
      render(json: { errors: @channel.errors }, status: 400)
    end
  end

  def show
    render(json: @channel.details, status: :ok)
  end

  def create
    channel = Channel.new(publisher: @owner, verified: true, created_via_api: true)

    channel.details = SiteChannelDetails.new(site_channel_details_params)

    SiteChannelDomainSetter.new(channel_details: channel.details).perform

    channel.save!

    # once the channel has been saved send it to eyeshade
    begin
      PublisherChannelSetter.new(publisher: @owner).perform
    rescue => e
      require "sentry-raven"
      Raven.capture_exception(e)
    end

    render(json: channel.details, status: :ok)
  end

  def verification_status
    render(
      json: {
        status: channel_verification_status(@current_channel),
        details: channel_verification_details(@current_channel)
      },
      status: :ok
    )
  end

  private

  def require_owner
    owner_id = publisher_id_from_owner_identifier(params[:owner_id])
    @owner = Publisher.where(id: owner_id).first

    return @owner if @owner
    response = {
        error: "Invalid owner",
        message: "Can't find an owner with ID #{params[:owner_id]}"<commit_msg>Modified response API to match Eyeshade<commit_after>class Api::ChannelsController < Api::BaseController
  before_action :require_owner,
                only: %i(verify notify show create)

  before_action :require_channel,
                only: %i(verify notify show)

  before_action :ensure_json_content_type,
                only: %i(create)
  before_action :get_current_channel, only: :verification_status

  include PublishersHelper

  def notify
    if params[:type].blank?
      return render(status: 400, json: { message: "parameter 'type' is required" })
    end

    PublisherNotifier.new(
      notification_params: params[:params],
      notification_type: params[:type],
      publisher: @owner,
      channel: @channel
    ).perform
    render(json: { message: "success" })
  rescue PublisherNotifier::InvalidNotificationTypeError => error
    render(json: { message: error.message }, status: 400)
  end

  def verify
    if params[:verificationId].blank?
      return render(status: 400, json: { message: "parameter 'verificationId' is required" })
    end

    if params[:verificationId] != @channel.id
      return render(status: 400, json: { message: "parameter 'verificationId' does not match the channel id" })
    end

    if params[:verified].nil?
      return render(status: 400, json: { message: "parameter 'verified' is required" })
    end

    # Only set verified to true, and only if the channel is not already verified. This keeps verified from flipping
    # back and forth resulting in sending the verified notification repeatedly.
    # Eyeshade calls this endpoint in response to a `GET /v1/owners/{owner}/verify/{publisher}` call.
    # Because of this we will send notification emails from the SiteChannelVerifier instead of here.
    if !@channel.verified && params[:verified]
      success = @channel.update!(verified: true)
    else
      success = true
    end

    if success
      head :no_content
    else
      render(json: { errors: @channel.errors }, status: 400)
    end
  end

  def show
    render(json: @channel.details, status: :ok)
  end

  def create
    channel = Channel.new(publisher: @owner, verified: true, created_via_api: true)

    channel.details = SiteChannelDetails.new(site_channel_details_params)

    SiteChannelDomainSetter.new(channel_details: channel.details).perform

    channel.save!

    # once the channel has been saved send it to eyeshade
    begin
      PublisherChannelSetter.new(publisher: @owner).perform
    rescue => e
      require "sentry-raven"
      Raven.capture_exception(e)
    end

    render(json: channel.details, status: :ok)
  end

  def verification_status
    if @current_channel.verified?
      render(
        json: {
          status: "success",
          verificationId: @current_channel.id
        },
        status: :ok
      ) and return
    else
      render(
        json: {
          status: "failure"
        },
        status: :ok
      ) and return
    end
  end

  private

  def require_owner
    owner_id = publisher_id_from_owner_identifier(params[:owner_id])
    @owner = Publisher.where(id: owner_id).first

    return @owner if @owner
    response = {
        error: "Invalid owner",
        message: "Can't find an owner with ID #{params[:owner_id]}"
{'fraction_non_alphanumeric': 0.09213412723284237, 'fraction_numerical': 0.005954246317768724, 'mean_word_length': 3.120723047127179, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1202952', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1775, 'n_words': 540}