<commit_before>class UsersController < ApplicationController
  include HighlightHelper

  authorize_action(%i[edit update confirm_destroy destroy]) do
    current_user.present? && (current_user.admin? || (current_user.user_id.to_s == params[:id]))
  end

  authorize_action(%i[show_votes edit_password update_password]) do
    current_user.present? && (current_user.user_id.to_s == params[:id])
  end

  def index
    @limit = conf('pagination_users').to_i

    if params[:s].present?
      @users = User.where('LOWER(username) LIKE LOWER(?)', '%' + params[:s].strip + '%')
      @search_term = params[:s]
    elsif params[:nick].present?
      @users = User.where('LOWER(username) LIKE LOWER(?)', params[:nick].strip + '%')
      params[:sort] = 'activity'
      params[:dir] = 'desc'
    elsif params[:exact].present?
      @users = User.where('LOWER(username) = LOWER(?)', params[:exact].strip)
    else
      @users = User
    end

    @users = sort_query(%w[username created_at updated_at score active admin activity],
                        @users, admin: 'COALESCE(admin, false)')
               .order('username ASC')
               .page(params[:page]).per(@limit)

    respond_to do |format|
      format.html
      format.json { render json: @users }
    end
  end

  def show
    @user = User.preload(badge_users: :badge).find(params[:id])
    @settings = @user.settings || Setting.new
    @settings.options ||= {}

    @messages_by_months = Message
                            .select("DATE_TRUNC('month', created_at) created_at, COUNt(*) cnt")
                            .where(user_id: @user.user_id,
                                   deleted: false)
                            .order("DATE_TRUNC('month', created_at)")
                            .group("DATE_TRUNC('month', created_at)")
                            .all

    @messages_count = Message.where(user_id: @user.user_id, deleted: false).count

    fids = Forum.visible_forums(current_user).select(:forum_id)

    @last_messages = Message
                       .preload(:owner, :tags, votes: :voters, thread: :forum)
                       .where('user_id = ? AND deleted = false AND forum_id IN (?)', @user.user_id, fids)
                       .order('created_at DESC')
                       .limit(5)

    @tags_cnts = MessageTag
                   .preload(tag: :forum)
                   .joins('INNER JOIN messages USING(message_id)')
                   .select('tag_id, COUNT(*) AS cnt')
                   .where('deleted = false AND user_id = ? AND forum_id IN (?)', @user.user_id, fids)
                   .group('tag_id')
                   .order('cnt DESC')
                   .limit(10)

    @point_msgs = Message
                    .preload(:owner, :tags, votes: :voters, thread: :forum)
                    .where('deleted = false AND upvotes > 0 AND user_id = ? AND forum_id IN (?)',
                           @user.user_id, fids)
                    .order('upvotes - downvotes DESC')
                    .limit(10)

    scored_msgs = Score<commit_msg>bestscored messages order by date also<commit_after>class UsersController < ApplicationController
  include HighlightHelper

  authorize_action(%i[edit update confirm_destroy destroy]) do
    current_user.present? && (current_user.admin? || (current_user.user_id.to_s == params[:id]))
  end

  authorize_action(%i[show_votes edit_password update_password]) do
    current_user.present? && (current_user.user_id.to_s == params[:id])
  end

  def index
    @limit = conf('pagination_users').to_i

    if params[:s].present?
      @users = User.where('LOWER(username) LIKE LOWER(?)', '%' + params[:s].strip + '%')
      @search_term = params[:s]
    elsif params[:nick].present?
      @users = User.where('LOWER(username) LIKE LOWER(?)', params[:nick].strip + '%')
      params[:sort] = 'activity'
      params[:dir] = 'desc'
    elsif params[:exact].present?
      @users = User.where('LOWER(username) = LOWER(?)', params[:exact].strip)
    else
      @users = User
    end

    @users = sort_query(%w[username created_at updated_at score active admin activity],
                        @users, admin: 'COALESCE(admin, false)')
               .order('username ASC')
               .page(params[:page]).per(@limit)

    respond_to do |format|
      format.html
      format.json { render json: @users }
    end
  end

  def show
    @user = User.preload(badge_users: :badge).find(params[:id])
    @settings = @user.settings || Setting.new
    @settings.options ||= {}

    @messages_by_months = Message
                            .select("DATE_TRUNC('month', created_at) created_at, COUNt(*) cnt")
                            .where(user_id: @user.user_id,
                                   deleted: false)
                            .order("DATE_TRUNC('month', created_at)")
                            .group("DATE_TRUNC('month', created_at)")
                            .all

    @messages_count = Message.where(user_id: @user.user_id, deleted: false).count

    fids = Forum.visible_forums(current_user).select(:forum_id)

    @last_messages = Message
                       .preload(:owner, :tags, votes: :voters, thread: :forum)
                       .where('user_id = ? AND deleted = false AND forum_id IN (?)', @user.user_id, fids)
                       .order('created_at DESC')
                       .limit(5)

    @tags_cnts = MessageTag
                   .preload(tag: :forum)
                   .joins('INNER JOIN messages USING(message_id)')
                   .select('tag_id, COUNT(*) AS cnt')
                   .where('deleted = false AND user_id = ? AND forum_id IN (?)', @user.user_id, fids)
                   .group('tag_id')
                   .order('cnt DESC')
                   .limit(10)

    @point_msgs = Message
                    .preload(:owner, :tags, votes: :voters, thread: :forum)
                    .where('deleted = false AND upvotes > 0 AND user_id = ? AND forum_id IN (?)',
                           @user.user_id, fids)
                    .order('upvotes - downvotes DESC', 'created_at DESC')
                    .limit(10)

    scored_msgs = Score
{'fraction_non_alphanumeric': 0.13340916477088072, 'fraction_numerical': 0.0019499512512187196, 'mean_word_length': 2.00537109375, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1668543', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1808, 'n_words': 376}