<commit_before>require_relative 'application_controller'

class SocialAccountsController < ApplicationController
  # Got some inspiration from: http://www.communityguides.eu/articles/16

  layout 'social_account_popup'

  def callback
    omniauth_obj = request.env['omniauth.auth']

    if user_signed_in?
      connect_provider params[:provider_name], omniauth_obj
    else
      sign_in_through_provider params[:provider_name], omniauth_obj
    end

    respond_to do |format|
      format.html { render :callback }
    end
  rescue Exception => error
    @event = "social_error"
    @event_details = error.message

    flash[:alert] = @event_details

    respond_to do |format|
      format.html { render :callback }
    end
  end

  def deauthorize
    case params[:provider_name]
    when 'facebook'
      provider_deauthorize 'facebook' do |uid, token|
        response = HTTParty.delete("https://graph.facebook.com/#{uid}/permissions?access_token=#{token}")
        if response.code != 200 and response.code != 400
          fail "Facebook deauthorize failed: '#{response.body}'."
        end
      end
    when 'twitter'
      provider_deauthorize 'twitter' do
        flash[:notice] = 'To complete, please deauthorize Factlink at the Twitter website.'
      end
    else
      fail "Wrong OAuth provider: #{omniauth['provider']}"
    end

    redirect_to edit_user_path(current_user)
  end

  def oauth_failure
    if params[:error_description].blank?
      params[:error_description] ||= "unspecified error"
    end
    @event = "social_error"
    @event_details = "Authorization failed: #{params[:error_description]}."

    respond_to do |format|
      format.html { render :callback }
    end
  end

  private

  def connect_provider provider_name, omniauth_obj
    authorize! :update, current_user

    if is_connected_to_different_user(provider_name, omniauth_obj)
      fail "Already connected to a different account, please sign in to the connected account or reconnect your account."
    elsif omniauth_obj
      current_user.social_account(provider_name).update_attributes!(omniauth_obj: omniauth_obj)
      flash[:notice] = "Succesfully connected."
      @event = 'authorized'
      @event_details = provider_name
    else
      fail "Error connecting."
    end
  end

  def is_connected_to_different_user provider_name, omniauth_obj
    social_account = current_user.social_account(provider_name)

    social_account.persisted? && social_account.uid != omniauth_obj['uid']
  end

  def sign_in_through_provider provider_name, omniauth_obj
    social_account = SocialAccount.find_by_provider_and_uid(provider_name, omniauth_obj['uid'])

    if social_account and social_account.user
      @user = social_account.user
      sign_in @user
      @event = 'signed_in'
      @event_details = provider_name
    else
      @event = 'social_error'
      @event_details = "No connected #{provider_name.capitalize} account found. "+
                       "Please sign in with your credentials and connect your #{provider_name.capitalize} account."
    end
  end

  def provider_deauthorize provider_name, &block
    authorize! :update, current_user

    social_account = current_user.social_account(provider_name)

    if social_account.persisted?
      uid = social_account.omniauth_obj['uid']
      token = social_account.omniauth_obj['credentials']['token']

      begin
        block.call uid, token
        social_account.delete
        flash[:notice] ||= "Succesfully disconnected."
      rescue => e
        flash[:alert] = "Error disconnecting. #{e.message}"
      end
    else
      flash[:alert] = "Already disconnected."
    end
  end
end
<commit_msg>Removed unnecessary formats and renders<commit_after>require_relative 'application_controller'

class SocialAccountsController < ApplicationController
  # Got some inspiration from: http://www.communityguides.eu/articles/16

  layout 'social_account_popup'

  def callback
    omniauth_obj = request.env['omniauth.auth']

    if user_signed_in?
      connect_provider params[:provider_name], omniauth_obj
    else
      sign_in_through_provider params[:provider_name], omniauth_obj
    end
  rescue Exception => error
    @event = "social_error"
    @event_details = error.message

    flash[:alert] = @event_details
  end

  def deauthorize
    case params[:provider_name]
    when 'facebook'
      provider_deauthorize 'facebook' do |uid, token|
        response = HTTParty.delete("https://graph.facebook.com/#{uid}/permissions?access_token=#{token}")
        if response.code != 200 and response.code != 400
          fail "Facebook deauthorize failed: '#{response.body}'."
        end
      end
    when 'twitter'
      provider_deauthorize 'twitter' do
        flash[:notice] = 'To complete, please deauthorize Factlink at the Twitter website.'
      end
    else
      fail "Wrong OAuth provider: #{omniauth['provider']}"
    end

    redirect_to edit_user_path(current_user)
  end

  def oauth_failure
    if params[:error_description].blank?
      params[:error_description] ||= "unspecified error"
    end
    @event = "social_error"
    @event_details = "Authorization failed: #{params[:error_description]}."

    render :callback
  end

  private

  def connect_provider provider_name, omniauth_obj
    authorize! :update, current_user

    if is_connected_to_different_user(provider_name, omniauth_obj)
      fail "Already connected to a different account, please sign in to the connected account or reconnect your account."
    elsif omniauth_obj
      current_user.social_account(provider_name).update_attributes!(omniauth_obj: omniauth_obj)
      flash[:notice] = "Succesfully connected."
      @event = 'authorized'
      @event_details = provider_name
    else
      fail "Error connecting."
    end
  end

  def is_connected_to_different_user provider_name, omniauth_obj
    social_account = current_user.social_account(provider_name)

    social_account.persisted? && social_account.uid != omniauth_obj['uid']
  end

  def sign_in_through_provider provider_name, omniauth_obj
    social_account = SocialAccount.find_by_provider_and_uid(provider_name, omniauth_obj['uid'])

    if social_account and social_account.user
      @user = social_account.user
      sign_in @user
      @event = 'signed_in'
      @event_details = provider_name
    else
      @event = 'social_error'
      @event_details = "No connected #{provider_name.capitalize} account found. "+
                       "Please sign in with your credentials and connect your #{provider_name.capitalize} account."
    end
  end

  def provider_deauthorize provider_name, &block
    authorize! :update, current_user

    social_account = current_user.social_account(provider_name)

    if social_account.persisted?
      uid = social_account.omniauth_obj['uid']
      token = social_account.omniauth_obj['credentials']['token']

      begin
        block.call uid, token
        social_account.delete
        flash[:notice] ||= "Succesfully disconnected."
      rescue => e
        flash[:alert] = "Error disconnecting. #{e.message}"
      end
    else
      flash[:alert] = "Already disconnected."
    end
  end
end

{'fraction_non_alphanumeric': 0.08606500209234202, 'fraction_numerical': 0.0022318314967219974, 'mean_word_length': 3.467289719626168, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2884755', 'n_tokens_mistral': 2136, 'n_tokens_neox': 2046, 'n_words': 501}