Created
August 23, 2010 20:45
-
-
Save oren/546302 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def import_contacts(data, params) | |
if data['token'] and !data['token'].blank? | |
token = data['token'] | |
imported = Cache.get("imported_#{current_user.uid}_#{data['token']}") | |
return nil unless imported | |
else | |
service_name = data['service'] | |
hash = validate(data) do | |
required(:service, :login, :password) | |
test("Service can't be blank", :service) do |val| | |
!val.empty? | |
end | |
test("Service #{service_name} is not supported", :service) do |val| | |
val.empty? || Dis::ImportContacts::SERVICES.include?(val.to_sym) | |
end | |
if !(data['service'] == 'outlook_csv') | |
test("Login can't be blank", :login) do |val| | |
!val.empty? | |
end | |
test("Password can't be blank", :password) do |val| | |
!val.empty? | |
end | |
end | |
end | |
imported = [] | |
if !(hash[:service] == 'outlook_csv') | |
imported = | |
Dis::ImportContacts.import(hash[:service], hash[:login], hash[:password]) | |
else | |
imported = | |
Dis::ImportContacts.import_csv(data['service'], "/home/oren/outlook.csv") | |
end | |
token = Digest::SHA1.hexdigest("#{current_user.uid}-#{Time.now}-#{rand(10000)}") | |
Cache.set("imported_#{current_user.uid}_#{token}", imported, 60 * 60 * 24) | |
end | |
current_page = params['page'] ? params['page'].to_i : 1 | |
ret = {:token => token, :list => [], :total_pages => 1, :current_page => current_page} | |
return ret unless imported and imported.size > 0 | |
imported_contact_emails = imported.map {|i| i[:email]} | |
#When requesting info on non-existing users | |
if params['filter'] == 'non_users' or params['filter'] == 'invited' | |
if params['filter'] == 'non_users' | |
glass_users = | |
Dis::Models::User.filter(:email => imported_contact_emails).select(:email).all | |
#Get a list of emails of the users who are already in the list | |
joined_emails = glass_users.map {|u| u.email} | |
imported.delete_if {|x| joined_emails.include?(x[:email])} | |
elsif params['filter'] == 'invited' | |
#Get all the emails for my pending invites | |
pending_invited_emails = current_user.invitations_dataset. | |
filter({:state => 'pending'} & {:email => imported_contact_emails}). | |
select(:email).all.map {|x| x.email} | |
imported.select! {|x| pending_invited_emails.include?(x[:email]) } | |
end | |
return ret unless imported.size > 0 | |
ret[:total_pages] = (imported.size - 1) / IMPORT_PAGE_SIZE + 1 | |
ret[:list] = imported[((current_page - 1) * IMPORT_PAGE_SIZE), IMPORT_PAGE_SIZE] | |
else #If we are asking for users who have already joined | |
glass_users = | |
Dis::Models::User.filter(:email => imported_contact_emails).all | |
#We either are going to select all users who are our contacts, or those who are not | |
#or we are going to return all existing users | |
if params['filter'] == 'contacts' #we want our contacts | |
glass_users.select! {|u| current_user.contact?(u)} | |
elsif params['filter'] == 'non_contacts' #otherwise we want users who are not our contacts | |
glass_users.delete_if {|u| current_user.contact?(u)} | |
end | |
return ret unless glass_users.size > 0 | |
ret[:total_pages] = (glass_users.size - 1) / IMPORT_PAGE_SIZE + 1 | |
ret[:list] = | |
glass_users[((current_page - 1) * IMPORT_PAGE_SIZE), IMPORT_PAGE_SIZE].map {|u| | |
resourcify(u) | |
} | |
end | |
ret | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment