2013-06-14 08:08:30 +00:00
|
|
|
require "net/http"
|
|
|
|
require "uri"
|
|
|
|
require 'json'
|
|
|
|
|
|
|
|
class FacebookController< ApplicationController
|
|
|
|
|
2013-11-26 11:14:24 +00:00
|
|
|
@@facebook_server = "http://fb.tp.rulingcom.com"
|
|
|
|
|
2013-06-14 08:08:30 +00:00
|
|
|
def register_fb
|
|
|
|
user = User.find(params[:user])
|
|
|
|
if !user.facebook
|
|
|
|
user.build_facebook(fb_identifier: params[:identifier],connected: true)
|
|
|
|
user.save!
|
|
|
|
end
|
|
|
|
render :json=>{"success"=>true,"user"=>user.facebook.fb_identifier}.to_json
|
2013-08-01 13:50:49 +00:00
|
|
|
# respond_to do |format|
|
|
|
|
# format.js
|
|
|
|
# end
|
2013-06-14 08:08:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_friends
|
2013-11-26 11:14:24 +00:00
|
|
|
fb = current_user.facebook rescue nil
|
|
|
|
if fb
|
|
|
|
@url = "#{@@facebook_server}/get_friends?identifier=" + current_user.facebook.fb_identifier
|
2013-08-01 13:50:49 +00:00
|
|
|
uri = URI.parse(@url)
|
|
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
request = Net::HTTP::Get.new(uri.request_uri)
|
|
|
|
response = http.request(request)
|
|
|
|
friends = response.body
|
|
|
|
friends = JSON.parse(friends)
|
|
|
|
output = Array.new
|
|
|
|
friends.each do |friend|
|
|
|
|
output << {"name"=>friend['name'],"identifier"=>friend['identifier']}
|
|
|
|
end
|
|
|
|
render :json=>{"success"=>true,"count"=>friends.count,"friends"=>output}.to_json
|
|
|
|
else
|
|
|
|
render :json=>{"success"=>false,"message"=>"User is not connected to facebook."}.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def disconnect
|
|
|
|
user_fb = current_user.facebook
|
|
|
|
if user_fb
|
|
|
|
user_fb.destroy
|
|
|
|
end
|
|
|
|
render :json=>{"success"=>true}.to_json
|
2013-06-14 08:08:30 +00:00
|
|
|
end
|
|
|
|
|
2013-11-26 11:14:24 +00:00
|
|
|
def profile_import
|
|
|
|
temp_user = User.find(params[:user_id])
|
|
|
|
fb = temp_user.facebook rescue nil
|
|
|
|
if fb
|
|
|
|
@url = "#{@@facebook_server}/profile_import?identifier=" + fb.fb_identifier
|
|
|
|
uri = URI.parse(@url)
|
|
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
request = Net::HTTP::Get.new(uri.request_uri)
|
|
|
|
response = http.request(request)
|
|
|
|
friends = response.body
|
|
|
|
data = JSON.parse(friends)
|
|
|
|
if data["success"]
|
|
|
|
user = JSON.parse(data["user"])
|
|
|
|
u = User.find(current_user.id)
|
2013-11-27 11:29:23 +00:00
|
|
|
firstname = {}
|
|
|
|
lastname = {}
|
|
|
|
@site_valid_locales.each do |locale|
|
|
|
|
firstname[locale] = user["first_name"]
|
|
|
|
lastname[locale] = user["last_name"]
|
|
|
|
end
|
|
|
|
u.first_name_translations = firstname
|
|
|
|
u.last_name_translations = lastname
|
2013-11-26 11:14:24 +00:00
|
|
|
u.office_tel = user["phone"]
|
|
|
|
u.sex = user["gender"] || "unknown"
|
|
|
|
u.remote_avatar_url = user["picture"] if user["picture"]
|
2013-11-27 11:29:23 +00:00
|
|
|
if u.save
|
|
|
|
render :json=>{"success"=>true,"user"=>user}.to_json
|
|
|
|
else
|
2013-11-29 08:20:23 +00:00
|
|
|
render :json=>{"success"=>false,"message"=>"There was an error imporing data."}.to_json
|
2013-11-27 11:29:23 +00:00
|
|
|
end
|
|
|
|
|
2013-11-26 11:14:24 +00:00
|
|
|
else
|
|
|
|
render :json=>{"success"=>false,"message"=>data["message"]}.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
render :json=>{"success"=>false,"message"=>"User is not connected to facebook."}.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-25 17:00:51 +00:00
|
|
|
def server_redirect
|
2013-11-26 11:14:24 +00:00
|
|
|
@url = "#{@@facebook_server}/login?callback=#{request.protocol}#{request.host_with_port}/facebook/register_fb?user="+current_user.id.to_s rescue nil
|
|
|
|
if !@url.nil?
|
|
|
|
redirect_to @url
|
|
|
|
else
|
|
|
|
render :layout=> "devise"
|
|
|
|
end
|
2013-11-25 17:00:51 +00:00
|
|
|
end
|
2013-06-14 08:08:30 +00:00
|
|
|
end
|