<commit_before>require_dependency 'sql_builder'

class Admin::FlagsController < Admin::AdminController
  def index 


    sql = SqlBuilder.new "select p.id, t.title, p.cooked, p.user_id, p.topic_id, p.post_number, p.hidden, t.visible topic_visible
from posts p 
join topics t on t.id = topic_id
join (
  select 
    post_id, 
    count(*) as cnt, 
    max(created_at) max, 
    min(created_at) min 
    from post_actions
    /*where2*/
    group by post_id
) as a on a.post_id = p.id
/*where*/
/*order_by*/    
limit 100
"

    sql.where2 "post_action_type_id in (:flag_types)", flag_types: PostActionType.FlagTypes


    # it may make sense to add a view that shows flags on deleted posts, 
    # we don't clear the flags on post deletion, just supress counts
    #   they may have deleted_at on the action not set
    if params[:filter] == 'old'
      sql.where2 "deleted_at is not null"
    else
      sql.where "p.deleted_at is null and t.deleted_at is null"
      sql.where2 "deleted_at is null"
    end

    if params[:filter] == 'old'
      sql.order_by "max desc"
    else
      sql.order_by "cnt desc, max asc"
    end

    posts = sql.exec.to_a
    
    if posts.length == 0
      render :json => {users: [], posts: []}
      return 
    end

    map = {} 
    users = Set.new

    posts.each{ |p| 
      users << p["user_id"]
      p["excerpt"] = Post.excerpt(p["cooked"])
      p.delete "cooked"
      p[:topic_slug] = Slug.for(p["title"])
      map[p["id"]] = p
    }

    sql = SqlBuilder.new "select a.id, a.user_id, post_action_type_id, a.created_at, post_id, a.message 
from post_actions a
/*where*/ 
" 
    sql.where("post_action_type_id in (:flag_types)", flag_types: PostActionType.FlagTypes)
    sql.where("post_id in (:posts)", posts: posts.map{|p| p["id"].to_i})

    if params[:filter] == 'old'
      sql.where('deleted_at is not null')
    else
      sql.where('deleted_at is null')
    end

    actions = sql.exec.each do |action|
      p = map[action["post_id"]] 
      p[:post_actions] ||= []
      p[:post_actions] << action

      users << action["user_id"]
    end

    sql = 
"select id, username, name, email from users
where id in (?)"
        
    users = User.exec_sql(sql, users.to_a).to_a

    users.each { |u| 
      u["avatar_template"] = User.avatar_template(u["email"])
      u.delete("email")
    }

    render json: MultiJson.dump({users: users, posts: posts}) 
    
  end

  def clear
    p = Post.find(params[:id])
    PostAction.clear_flags!(p, current_user.id)
    p.hidden = false 
    p.hidden_reason_id = nil
    p.save
    render nothing: true 
  end
end<commit_msg>remove unused var<commit_after>require_dependency 'sql_builder'

class Admin::FlagsController < Admin::AdminController
  def index 

    sql = SqlBuilder.new "select p.id, t.title, p.cooked, p.user_id, p.topic_id, p.post_number, p.hidden, t.visible topic_visible
from posts p 
join topics t on t.id = topic_id
join (
  select 
    post_id, 
    count(*) as cnt, 
    max(created_at) max, 
    min(created_at) min 
    from post_actions
    /*where2*/
    group by post_id
) as a on a.post_id = p.id
/*where*/
/*order_by*/    
limit 100
"

    sql.where2 "post_action_type_id in (:flag_types)", flag_types: PostActionType.FlagTypes


    # it may make sense to add a view that shows flags on deleted posts, 
    # we don't clear the flags on post deletion, just supress counts
    #   they may have deleted_at on the action not set
    if params[:filter] == 'old'
      sql.where2 "deleted_at is not null"
    else
      sql.where "p.deleted_at is null and t.deleted_at is null"
      sql.where2 "deleted_at is null"
    end

    if params[:filter] == 'old'
      sql.order_by "max desc"
    else
      sql.order_by "cnt desc, max asc"
    end

    posts = sql.exec.to_a
    
    if posts.length == 0
      render :json => {users: [], posts: []}
      return 
    end

    map = {} 
    users = Set.new

    posts.each{ |p| 
      users << p["user_id"]
      p["excerpt"] = Post.excerpt(p["cooked"])
      p.delete "cooked"
      p[:topic_slug] = Slug.for(p["title"])
      map[p["id"]] = p
    }

    sql = SqlBuilder.new "select a.id, a.user_id, post_action_type_id, a.created_at, post_id, a.message 
from post_actions a
/*where*/ 
" 
    sql.where("post_action_type_id in (:flag_types)", flag_types: PostActionType.FlagTypes)
    sql.where("post_id in (:posts)", posts: posts.map{|p| p["id"].to_i})

    if params[:filter] == 'old'
      sql.where('deleted_at is not null')
    else
      sql.where('deleted_at is null')
    end

    sql.exec.each do |action|
      p = map[action["post_id"]] 
      p[:post_actions] ||= []
      p[:post_actions] << action

      users << action["user_id"]
    end

    sql = 
"select id, username, name, email from users
where id in (?)"
        
    users = User.exec_sql(sql, users.to_a).to_a

    users.each { |u| 
      u["avatar_template"] = User.avatar_template(u["email"])
      u.delete("email")
    }

    render json: MultiJson.dump({users: users, posts: posts}) 
  end

  def clear
    p = Post.find(params[:id])
    PostAction.clear_flags!(p, current_user.id)
    p.hidden = false 
    p.hidden_reason_id = nil
    p.save
    render nothing: true 
  end
end
{'fraction_non_alphanumeric': 0.1276799387442573, 'fraction_numerical': 0.0030627871362940277, 'mean_word_length': 2.6461967899511514, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, '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': '1355992', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1817, 'n_words': 488}