<commit_before>require_relative '../interactors/interactors/channels'

class UsersController < ApplicationController
  layout "frontend"

  before_filter :load_user, except: [:search, :tour_users, :seen_messages]

  def show
    authorize! :show, @user
    backbone_responder do
      render 'users/_user', user: @user
    end
  end

  # TODO: convert this page to backbone
  def edit
    authorize! :access, Ability::FactlinkWebapp
    authorize! :update, @user
  end

  def update
    authorize! :update, @user

    # sometimes it is passed in user, sometimes it isn't :(
    user_hash =  params[:user] || params

    if @user.update_attributes user_hash
      respond_to do |format|
        format.html { redirect_to edit_user_url(@user.username), notice: 'Your account was successfully updated.' }
        format.json { render json: {}}
      end
    else
      respond_to do |format|
        format.html do
          authorize! :access, Ability::FactlinkWebapp
          render :edit
        end
        format.json { render json: { status: :unprocessable_entity } }
      end
    end
  end

  def delete
    authorize! :delete, @user #TODO: move to interactor?

    password_ok = @user.valid_password? #TODO: move to interactor?

    used_is_deleted = password_ok && interactor(:'users/deleted', password: params[:user_current_password])

    if used_is_deleted
      respond_to do |format|
        #TODO: UI, clear session?
        format.html { redirect_to edit_user_url(@user.username), notice: 'Your account was successfully deleted.' }
        format.json { render json: {} }
      end
    else
      respond_to do |format|
        format.html do
          authorize! :access, Ability::FactlinkWebapp
          render :edit #TODO: this is probably wrong; but UI is out of scope here.
        end
        format.json { render json: { status: :unprocessable_entity } }
      end
    end
  end

  def activities
    authorize! :see_activities, @user
    @activities = @user.graph_user.notifications.below('inf', count: 10, reversed: true, withscores: true )

    @activities.select! { |a| a[:item] && a[:item].still_valid? }
    @showing_notifications = true
    respond_to do |format|
      format.json { render 'channels/activities' }
    end
  end
<commit_msg>Use interactor in controller<commit_after>require_relative '../interactors/interactors/channels'

class UsersController < ApplicationController
  layout "frontend"

  before_filter :load_user, except: [:search, :tour_users, :seen_messages]

  def show
    authorize! :show, @user
    backbone_responder do
      render 'users/_user', user: @user
    end
  end

  # TODO: convert this page to backbone
  def edit
    authorize! :access, Ability::FactlinkWebapp
    authorize! :update, @user
  end

  def update
    authorize! :update, @user

    # sometimes it is passed in user, sometimes it isn't :(
    user_hash =  params[:user] || params

    if @user.update_attributes user_hash
      respond_to do |format|
        format.html { redirect_to edit_user_url(@user.username), notice: 'Your account was successfully updated.' }
        format.json { render json: {}}
      end
    else
      respond_to do |format|
        format.html do
          authorize! :access, Ability::FactlinkWebapp
          render :edit
        end
        format.json { render json: { status: :unprocessable_entity } }
      end
    end
  end

  def delete
    password_ok = @user.valid_password? #TODO: move to interactor?

    if password_ok
      interactor(:'users/delete', user_id:@user.id)

      respond_to do |format|
        #TODO: UI, clear session?
        format.html { redirect_to edit_user_url(@user.username), notice: 'Your account was successfully deleted.' }
        format.json { render json: {} }
      end
    else
      respond_to do |format|
        format.html do
          authorize! :access, Ability::FactlinkWebapp
          render :edit #TODO: this is probably wrong; but UI is out of scope here.
        end
        format.json { render json: { status: :unprocessable_entity } }
      end
    end
  end

  def activities
    authorize! :see_activities, @user
    @activities = @user.graph_user.notifications.below('inf', count: 10, reversed: true, withscores: true )

    @activities.select! { |a| a[:item] && a[:item].still_valid? }
    @showing_notifications = true
    respond_to do |format|
      format.json { render 'channels/activities' }
    end
  end

{'fraction_non_alphanumeric': 0.09574228429826538, 'fraction_numerical': 0.0009011038522189682, 'mean_word_length': 2.8541666666666665, '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': '4319447', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1311, 'n_words': 389}