diaspora/app/models/services/facebook.rb
Jonne Haß 3fc3b249e7 End the require mess
* Rename and reorganize post fetcher to fix autoloading, also let it use
  Faradays default connection so we get nice redirects
* Add initializer to load libs at a central place
* added lib dir to autoload_once paths to increase thread safety
* Moved lib/exceptions.rb to lib/diaspora/ to conform namespacing
2013-03-21 23:37:53 +01:00

48 lines
1.6 KiB
Ruby

class Services::Facebook < Service
include Rails.application.routes.url_helpers
include MarkdownifyHelper
OVERRIDE_FIELDS_ON_FB_UPDATE = [:contact_id, :person_id, :request_id, :invitation_id, :photo_url, :name, :username]
MAX_CHARACTERS = 420
def provider
"facebook"
end
def post(post, url='')
Rails.logger.debug("event=post_to_service type=facebook sender_id=#{self.user_id}")
response = post_to_facebook("https://graph.facebook.com/me/feed", create_post_params(post).to_param)
response = JSON.parse response.body
post.facebook_id = response["id"]
post.save
end
def post_to_facebook(url, body)
Faraday.post(url, body)
end
def create_post_params(post)
message = strip_markdown(post.text(:plain_text => true))
if post.photos.any?
message += " " + Rails.application.routes.url_helpers.short_post_url(post, :protocol => AppConfig.pod_uri.scheme, :host => AppConfig.pod_uri.authority)
end
{:message => message, :access_token => self.access_token, :link => URI.extract(message, ['https', 'http']).first}
end
def public_message(post, url)
super(post, MAX_CHARACTERS, url)
end
def profile_photo_url
"https://graph.facebook.com/#{self.uid}/picture?type=large&access_token=#{URI.escape(self.access_token)}"
end
def delete_post(service_post_id)
Rails.logger.debug("event=delete_from_service type=facebook sender_id=#{self.user_id}")
delete_from_facebook("https://graph.facebook.com/#{service_post_id}/", {:access_token => self.access_token})
end
def delete_from_facebook(url, body)
Faraday.delete(url, body)
end
end