<commit_before>class SessionsController < ApplicationController
  skip_before_filter :require_authentication
  
  def authenticate
    redirect_to client.web_server.authorize_url(
      redirect_uri: authentication_callback_url,
      scope: 'read'
    )
  end
  
  def create
    user_attributes = access_token.get('/api/user')
    user = sign_up user_attributes
    session[:user_id] = user.id
    redirect_to account_path
  end
  
  def destroy
    session[:user_id] = nil
    redirect_to root_path
  end
  
  private
  
  def sign_up(user_attributes)
    user = find_or_create_user user_attributes
    create_memberships user, user_attributes["memberships"]
    create_spaces user_attributes['admin_of'].map{|admin_of| admin_of['space_link']}
    user
  end
  
  def create_spaces(links)
    links.each do |link|
      find_or_create_space link
    end
  end
  
  def find_or_create_user(user_attributes)
    unless user = db.first(User.by_login(user_attributes["login"]))
      user = User.new(login: user_attributes["login"], admin_of: user_attributes['admin_of'].map{|space_attributes| access_token.get(space_attributes['space_link'])['id']})
      db.save user
    end
    user
  end
  
  def create_memberships(user, memberships_attributes)
    memberships_attributes.each do |membership_attributes|
      membership_details = access_token.get(membership_attributes['link'])
      unless db.load membership_details['id']
        db.save Membership.new user_id: user.id, id: membership_details['id'],
          space_id: find_or_create_space(membership_attributes['space_link']).id,
          name: membership_details['address']['name']
      end
    end
  end
  
  def find_or_create_space(space_url)
    space_attributes = access_token.get(space_url)
    unless space = db.load(space_attributes['id'])
      space = Space.new name: space_attributes['name'], id: space_attributes['id']
      db.save space
    end
    space
  end
  
  def client
    OAuth2::Client.new(Coworkers::Conf.app_id,
      Coworkers::Conf.app_secret,
      site: {
         url: Coworkers::Conf.app_site,
         ssl: {<commit_msg>don't import canceled/unconfirmed memberships<commit_after>class SessionsController < ApplicationController
  skip_before_filter :require_authentication
  
  def authenticate
    redirect_to client.web_server.authorize_url(
      redirect_uri: authentication_callback_url,
      scope: 'read'
    )
  end
  
  def create
    user_attributes = access_token.get('/api/user')
    user = sign_up user_attributes
    session[:user_id] = user.id
    redirect_to account_path
  end
  
  def destroy
    session[:user_id] = nil
    redirect_to root_path
  end
  
  private
  
  def sign_up(user_attributes)
    user = find_or_create_user user_attributes
    create_memberships user, user_attributes["memberships"]
    create_spaces user_attributes['admin_of'].map{|admin_of| admin_of['space_link']}
    user
  end
  
  def create_spaces(links)
    links.each do |link|
      find_or_create_space link
    end
  end
  
  def find_or_create_user(user_attributes)
    unless user = db.first(User.by_login(user_attributes["login"]))
      user = User.new(login: user_attributes["login"], admin_of: user_attributes['admin_of'].map{|space_attributes| access_token.get(space_attributes['space_link'])['id']})
      db.save user
    end
    user
  end
  
  def create_memberships(user, memberships_attributes)
    memberships_attributes.each do |membership_attributes|
      membership_details = access_token.get(membership_attributes['link'])
      unless membership_details['confirmed_at'].nil? || membership_details['canceled_to'] || db.load(membership_details['id'])
        db.save Membership.new user_id: user.id, id: membership_details['id'],
          space_id: find_or_create_space(membership_attributes['space_link']).id,
          name: membership_details['address']['name']
      end
    end
  end
  
  def find_or_create_space(space_url)
    space_attributes = access_token.get(space_url)
    unless space = db.load(space_attributes['id'])
      space = Space.new name: space_attributes['name'], id: space_attributes['id']
      db.save space
    end
    space
  end
  
  def client
    OAuth2::Client.new(Coworkers::Conf.app_id,
      Coworkers::Conf.app_secret,
      site: {
         url: Coworkers::Conf.app_site,
         ssl: {
{'fraction_non_alphanumeric': 0.09615827007131356, 'fraction_numerical': 0.00046008741660915573, 'mean_word_length': 3.8150609080841638, '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': '806945', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1362, 'n_words': 248}