<commit_before>require 'timed_set'

class ProductsController < ProductController
  respond_to :html, :json

  before_action :authenticate_user!, only: [:new, :create, :edit, :update, :follow, :unfollow, :announcements, :welcome]
  before_action :set_product,
    only: [:show, :old, :edit, :update, :follow, :announcements, :unfollow, :metrics, :flag, :feature, :launch]

  def new
    @product = Product.new
    @product.user = current_user

    @four_word_story_example = [
        'Organize everything you love.',
        'Easily find useful information.'
      ].sample

    render layout: 'application'
  end

  def start
    @profit = ProfitReport.all.map(&:profit).sum.round(-6)
    render layout: 'application'
  end

  def create
    @product = create_product_with_params
    if @product.valid?
      respond_with(@product, location: product_welcome_path(@product))

      Karma::Kalkulate.new.award_for_product_to_stealth(@product)

      @product.retrieve_key_pair
      schedule_greet
      schedule_one_hour_checkin
      schedule_one_day_checkin
    else
      render action: :new, layout: 'application'
    end
  end

  def welcome
    find_product!
    authorize! :update, @product
  end

  def admin
    return redirect_to(product_url(@product)) unless current_user && current_user.is_staff?
    find_product!
  end

  def flag
    return redirect_to(product_url(@product)) unless current_user && current_user.is_staff?
    if request.post?
      @product.touch(:flagged_at)
      @product.update_attribute(:flagged_reason, params[:message])
      # TODO: disabling email to idea submitter for time being
      # ProductMailer.delay(queue: 'mailer').flagged(current_user.id, @product.id, params[:message])
      return redirect_to product_url(@product)
    end
  end

  def feature
    return head(:forbidden) unless current_user && current_user.is_staff?
    @product.touch(:featured_on)
    return redirect_to product_url(@product)
  end

  def show
    return redirect_to(about_url) if @product.meta?
    @user_metrics = UserMetricsSummary.new(@product, Date.today - 1.day)

    page_views = TimedSet.new($redis, "#{@product.id}:show")

    if page_views.add(request.remote_ip)
      Product.increment_counter(:view_count, @product.id)
      page_views.drop_older_than(5.minutes)
    end

    if signed_in? && current_user.staff?

      if params[:tag].present?
        @mark_name = params[:mark]
        @news_feed_to_show = Marks::MarkBasics.new.newsfeed_items_per_product_per_mark(@product, @mark_name).order(updated_at: :desc)
      else
        @news_feed_to_show = @product.news_feed_items.limit(20).order(updated_at: :desc)
      end

      @news_feed_items = ActiveModel::ArraySerializer.new(
        @news_feed_to_show
      )
      
      render 'products/new_show', layout: 'product'
      return
    end<commit_msg>pass top marks in controller to view<commit_after>require 'timed_set'

class ProductsController < ProductController
  respond_to :html, :json

  before_action :authenticate_user!, only: [:new, :create, :edit, :update, :follow, :unfollow, :announcements, :welcome]
  before_action :set_product,
    only: [:show, :old, :edit, :update, :follow, :announcements, :unfollow, :metrics, :flag, :feature, :launch]

  MARK_DISPLAY_LIMIT =  7 #maximum number of marks to display on product page

  def new
    @product = Product.new
    @product.user = current_user

    @four_word_story_example = [
        'Organize everything you love.',
        'Easily find useful information.'
      ].sample

    render layout: 'application'
  end

  def start
    @profit = ProfitReport.all.map(&:profit).sum.round(-6)
    render layout: 'application'
  end

  def create
    @product = create_product_with_params
    if @product.valid?
      respond_with(@product, location: product_welcome_path(@product))

      Karma::Kalkulate.new.award_for_product_to_stealth(@product)

      @product.retrieve_key_pair
      schedule_greet
      schedule_one_hour_checkin
      schedule_one_day_checkin
    else
      render action: :new, layout: 'application'
    end
  end

  def welcome
    find_product!
    authorize! :update, @product
  end

  def admin
    return redirect_to(product_url(@product)) unless current_user && current_user.is_staff?
    find_product!
  end

  def flag
    return redirect_to(product_url(@product)) unless current_user && current_user.is_staff?
    if request.post?
      @product.touch(:flagged_at)
      @product.update_attribute(:flagged_reason, params[:message])
      # TODO: disabling email to idea submitter for time being
      # ProductMailer.delay(queue: 'mailer').flagged(current_user.id, @product.id, params[:message])
      return redirect_to product_url(@product)
    end
  end

  def feature
    return head(:forbidden) unless current_user && current_user.is_staff?
    @product.touch(:featured_on)
    return redirect_to product_url(@product)
  end

  def show
    return redirect_to(about_url) if @product.meta?
    @user_metrics = UserMetricsSummary.new(@product, Date.today - 1.day)

    page_views = TimedSet.new($redis, "#{@product.id}:show")

    if page_views.add(request.remote_ip)
      Product.increment_counter(:view_count, @product.id)
      page_views.drop_older_than(5.minutes)
    end

    if signed_in? && current_user.staff?

      @top_wip_tags = Marks::MarkBasics.new.leading_marks_on_product(product, MARK_DISPLAY_LIMIT)

      if params[:tag].present?
        @mark_name = params[:mark]
        @news_feed_to_show = Marks::MarkBasics.new.newsfeed_items_per_product_per_mark(@product, @mark_name).order(updated_at: :desc)
      else
        @news_feed_to_show = @product.news_feed_items.limit(20).order(updated_at: :desc)
      end

      @news_feed_items = ActiveModel::ArraySerializer.new(
        @news_feed_to_show
      )

      render 'products/new_show', layout: 'product'
      return
    end
{'fraction_non_alphanumeric': 0.10409237561555443, 'fraction_numerical': 0.0018678892851078282, 'mean_word_length': 3.734726688102894, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4808247', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1860, 'n_words': 389}