<commit_before>class TopicsController < ApplicationController
  
  before_filter :require_login, :except => [:index, :show, :show_posters, :show_new, :unknown_request]
  before_filter :can_edit, :only => [:edit, :update, :destroy]
  
  def index
    if logged_in?      
      @topics = Topic.paginate(:page => params[:page], :include => [:user, :last_poster], :order => 'last_post_at desc')
    else
      @topics = Topic.paginate(:page => params[:page], :include => [:user, :last_poster], :order => 'last_post_at desc', :conditions => ["private = ?", false])
    end
  end

  def show
    @topic = Topic.find(params[:id], :include => :forum)
    redirect_to login_path if (!logged_in? && @topic.private)
    @posts = @topic.posts.paginate(:page => params[:page], :include => :user)
    redirect_to @topic if @posts.blank? # if params[:page] is too big, no posts will be found
    @page = params[:page] ? params[:page] : 1
    @padding = ((@page.to_i - 1) * Topic::PER_PAGE) # to get post #s w/ pagination
    @topic.hit!
  end

  def new
  end

  def create
    @topic = current_user.topics.build(params[:topic])
    @post = @topic.posts.build(params[:topic]) ; @post.user = current_user
    redirect_to @topic and return if @topic.save && @post.save
    render :action => "new"
  end

  def edit
    @topic = Topic.find(params[:id])
  end

  def update
    @topic = Topic.find(params[:id])
    if @topic.update_attributes(params[:topic])
      redirect_to @topic
    else
      render :action => "edit"
    end
  end

  def destroy
    @topic = Topic.find(params[:id])
    @topic.destroy
    redirect_to topics_url
  end
    
  def show_new
    @topic = Topic.find(params[:id])
    redirect_to topic_path(:id => params[:id]) and return if @topic.posts_count == 1 # if the first post, see it from the top of the page
    @post = @topic.posts.find(:first, :order => 'created_at asc', :conditions => ["created_at >= ?", session[:online_at]]) unless !logged_in?
    @post = Post.find(@topic.last_post_id) if @post.nil?
    redirect_to :controller => 'topics', :action => 'show', :id => @topic.id, :page => @post.page, :anchor => 'p' + @post.id.to_s
  end
  
  def show_posters
    @topic = Topic.find(params[:id])
    @posters = @topic.posts.map(&:user) ; @posters.uniq!
    render :update do |page| 
      page.toggle :posters
      page.replace_html 'posters', "#{@posters.map { |u| "#{h u.login}" } * ', ' }" 
    end 
  end
  
  def unknown_request
    if request.request_uri.include?('viewtopic.php') # catch punbb-style urls
      if params[:id].blank? # punbb can show a topic based on the post_id being passed as "pid"
        @post = Post.find(params[:pid]) # if this is the case, get the post info
        params[:id] = @post.topic_id # set the regular topic_id value as id with this post's topic_id
      end
      redirect_to topic_path(:id => params[:id], :anchor => params[:anchor])<commit_msg>removing unecessary check in topics/show_new<commit_after>class TopicsController < ApplicationController
  
  before_filter :require_login, :except => [:index, :show, :show_posters, :show_new, :unknown_request]
  before_filter :can_edit, :only => [:edit, :update, :destroy]
  
  def index
    if logged_in?      
      @topics = Topic.paginate(:page => params[:page], :include => [:user, :last_poster], :order => 'last_post_at desc')
    else
      @topics = Topic.paginate(:page => params[:page], :include => [:user, :last_poster], :order => 'last_post_at desc', :conditions => ["private = ?", false])
    end
  end

  def show
    @topic = Topic.find(params[:id], :include => :forum)
    redirect_to login_path if (!logged_in? && @topic.private)
    @posts = @topic.posts.paginate(:page => params[:page], :include => :user)
    redirect_to @topic if @posts.blank? # if params[:page] is too big, no posts will be found
    @page = params[:page] ? params[:page] : 1
    @padding = ((@page.to_i - 1) * Topic::PER_PAGE) # to get post #s w/ pagination
    @topic.hit!
  end

  def new
  end

  def create
    @topic = current_user.topics.build(params[:topic])
    @post = @topic.posts.build(params[:topic]) ; @post.user = current_user
    redirect_to @topic and return if @topic.save && @post.save
    render :action => "new"
  end

  def edit
    @topic = Topic.find(params[:id])
  end

  def update
    @topic = Topic.find(params[:id])
    if @topic.update_attributes(params[:topic])
      redirect_to @topic
    else
      render :action => "edit"
    end
  end

  def destroy
    @topic = Topic.find(params[:id])
    @topic.destroy
    redirect_to topics_url
  end
    
  def show_new
    @topic = Topic.find(params[:id])
    @post = @topic.posts.find(:first, :order => 'created_at asc', :conditions => ["created_at >= ?", session[:online_at]]) unless !logged_in?
    @post = Post.find(@topic.last_post_id) if @post.nil?
    redirect_to :controller => 'topics', :action => 'show', :id => @topic.id, :page => @post.page, :anchor => 'p' + @post.id.to_s
  end
  
  def show_posters
    @topic = Topic.find(params[:id])
    @posters = @topic.posts.map(&:user) ; @posters.uniq!
    render :update do |page| 
      page.toggle :posters
      page.replace_html 'posters', "#{@posters.map { |u| "#{h u.login}" } * ', ' }" 
    end 
  end
  
  def unknown_request
    if request.request_uri.include?('viewtopic.php') # catch punbb-style urls
      if params[:id].blank? # punbb can show a topic based on the post_id being passed as "pid"
        @post = Post.find(params[:pid]) # if this is the case, get the post info
        params[:id] = @post.topic_id # set the regular topic_id value as id with this post's topic_id
      end
      redirect_to topic_path(:id => params[:id], :anchor => params[:anchor])
{'fraction_non_alphanumeric': 0.15337423312883436, 'fraction_numerical': 0.0008764241893076249, 'mean_word_length': 3.4301242236024843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, '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': '7469403', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1925, 'n_words': 511}