From 5a70311fe707d4f621e619da3f283c40dccba72b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonne=20Ha=C3=9F?= Date: Sat, 20 Oct 2012 02:59:23 +0200 Subject: [PATCH 1/2] update and fix services --- Changelog.md | 1 + Gemfile | 6 ++-- Gemfile.lock | 41 ++++++++++++++------------- app/models/services/facebook.rb | 2 +- app/models/services/tumblr.rb | 20 ++++++------- app/models/services/twitter.rb | 27 +++++------------- config/initializers/omniauth.rb | 6 ++++ spec/models/services/facebook_spec.rb | 6 ++-- spec/models/services/tumblr_spec.rb | 8 ++---- spec/models/services/twitter_spec.rb | 14 ++++----- 10 files changed, 63 insertions(+), 68 deletions(-) diff --git a/Changelog.md b/Changelog.md index 2d7e6f59a..757acff09 100644 --- a/Changelog.md +++ b/Changelog.md @@ -23,6 +23,7 @@ ## Bug Fixes +* Fix posting to Facebook and Tumblr. Have a look at the updated [services guide](https://github.com/diaspora/diaspora/wiki/Howto-setup-services) for new Facebook instructions. * Fix issue with interacted_at in post fetcher. [#3607](https://github.com/diaspora/diaspora/pull/3607) * Fix error with show post Community Spotlight. [#3658](https://github.com/diaspora/diaspora/pull/3658) * Fix javascripts problem with read/unread notifications. [#3656](https://github.com/diaspora/diaspora/pull/3656) diff --git a/Gemfile b/Gemfile index c37e2d619..1533a9fe0 100644 --- a/Gemfile +++ b/Gemfile @@ -21,11 +21,11 @@ gem 'devise', '2.1.2' gem 'remotipart', '1.0.2' gem 'omniauth', '1.1.1' -gem 'omniauth-facebook', '1.3.0' +gem 'omniauth-facebook', '1.4.1' gem 'omniauth-tumblr', '1.1' -gem 'omniauth-twitter', '0.0.11' +gem 'omniauth-twitter', '0.0.13' -gem 'twitter', '2.0.2' +gem 'twitter', '4.1.1' # mail diff --git a/Gemfile.lock b/Gemfile.lock index 8b5617508..07addabc7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -184,7 +184,7 @@ GEM highline (1.6.12) hike (1.2.1) http_accept_language (1.0.2) - httpauth (0.1) + httpauth (0.2.0) i18n (0.6.1) i18n-inflector (2.6.6) i18n (>= 0.4.1) @@ -207,6 +207,8 @@ GEM railties (>= 3.1.0) jruby-pageant (1.1.1) json (1.7.5) + jwt (0.1.5) + multi_json (>= 1.0) kaminari (0.14.1) actionpack (>= 3.0.0) activesupport (>= 3.0.0) @@ -240,25 +242,27 @@ GEM net-ssh-gateway (1.1.0) net-ssh (>= 1.99.1) nokogiri (1.5.5) - oauth (0.4.6) - oauth2 (0.6.1) - faraday (~> 0.7) + oauth (0.4.7) + oauth2 (0.8.0) + faraday (~> 0.8) httpauth (~> 0.1) - multi_json (~> 1.3) + jwt (~> 0.1.4) + multi_json (~> 1.0) + rack (~> 1.2) omniauth (1.1.1) hashie (~> 1.2) rack - omniauth-facebook (1.3.0) - omniauth-oauth2 (~> 1.0.2) + omniauth-facebook (1.4.1) + omniauth-oauth2 (~> 1.1.0) omniauth-oauth (1.0.1) oauth omniauth (~> 1.0) - omniauth-oauth2 (1.0.2) - oauth2 (~> 0.6.0) + omniauth-oauth2 (1.1.1) + oauth2 (~> 0.8.0) omniauth (~> 1.0) omniauth-tumblr (1.1) omniauth-oauth (~> 1.0) - omniauth-twitter (0.0.11) + omniauth-twitter (0.0.13) multi_json (~> 1.3) omniauth-oauth (~> 1.0) orm_adapter (0.4.0) @@ -361,7 +365,7 @@ GEM libwebsocket (~> 0.1.3) multi_json (~> 1.0) rubyzip - simple_oauth (0.1.8) + simple_oauth (0.1.9) sinatra (1.3.3) rack (~> 1.3, >= 1.3.6) rack-protection (~> 1.2) @@ -378,11 +382,10 @@ GEM treetop (1.4.10) polyglot polyglot (>= 0.3.1) - twitter (2.0.2) - activesupport (>= 2.3.9, < 4) - faraday (~> 0.7) - multi_json (~> 1.0) - simple_oauth (~> 0.1) + twitter (4.1.1) + faraday (~> 0.8) + multi_json (~> 1.3) + simple_oauth (~> 0.1.6) typhoeus (0.3.3) mime-types tzinfo (0.3.33) @@ -452,9 +455,9 @@ DEPENDENCIES mysql2 (= 0.3.11) nokogiri (= 1.5.5) omniauth (= 1.1.1) - omniauth-facebook (= 1.3.0) + omniauth-facebook (= 1.4.1) omniauth-tumblr (= 1.1) - omniauth-twitter (= 0.0.11) + omniauth-twitter (= 0.0.13) rack-cors (= 0.2.7) rack-google-analytics (= 0.11.0) rack-piwik (= 0.1.3) @@ -476,7 +479,7 @@ DEPENDENCIES selenium-webdriver (= 2.25.0) spork (= 1.0.0rc3) timecop (= 0.5.1) - twitter (= 2.0.2) + twitter (= 4.1.1) typhoeus (= 0.3.3) uglifier (= 1.3.0) unicorn (= 4.3.1) diff --git a/app/models/services/facebook.rb b/app/models/services/facebook.rb index 39d2b1086..4de963e06 100644 --- a/app/models/services/facebook.rb +++ b/app/models/services/facebook.rb @@ -12,7 +12,7 @@ class Services::Facebook < Service def post(post, url='') Rails.logger.debug("event=post_to_service type=facebook sender_id=#{self.user_id}") if post.public? - post_to_facebook("https://graph.facebook.com/me/joindiaspora:make", create_open_graph_params(post).to_param) + post_to_facebook("https://graph.facebook.com/me/#{AppConfig.services.facebook.open_graph_namespace}:make", create_open_graph_params(post).to_param) else post_to_facebook("https://graph.facebook.com/me/feed", create_post_params(post).to_param) end diff --git a/app/models/services/tumblr.rb b/app/models/services/tumblr.rb index ab964b6ea..203d1a349 100644 --- a/app/models/services/tumblr.rb +++ b/app/models/services/tumblr.rb @@ -17,28 +17,28 @@ class Services::Tumblr < Service end def post(post, url='') - - consumer = OAuth::Consumer.new(consumer_key, consumer_secret, :site => 'http://tumblr.com') + consumer = OAuth::Consumer.new(consumer_key, consumer_secret, :site => 'http://api.tumblr.com') access = OAuth::AccessToken.new(consumer, self.access_token, self.access_secret) + body = build_tumblr_post(post, url) - begin - resp = access.post('http://tumblr.com/api/write', body) - resp - rescue => e - nil + user_info = JSON.parse(access.get("/v2/user/info").body) + blogs = user_info["response"]["user"]["blogs"].map { |blog| URI.parse(blog['url']) } + blogs.each do |blog| + access.post("/v2/blog/#{blog.host}/post", body) end end def build_tumblr_post(post, url) - {:generator => 'diaspora', :type => 'regular', :body => tumblr_template(post, url)} + { :type => 'text', :format => "markdown", :body => tumblr_template(post, url) } end def tumblr_template(post, url) html = '' post.photos.each do |photo| - html += "
" + html += "![photo](#{photo.url(:scaled_full)})\n\n" end - html += auto_link(post.text, :link => :urls) + html += post.text + html += "\n\n[original post](#{url})" end end diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb index a4730b6e8..37e931d31 100644 --- a/app/models/services/twitter.rb +++ b/app/models/services/twitter.rb @@ -12,9 +12,7 @@ class Services::Twitter < Service Rails.logger.debug("event=post_to_service type=twitter sender_id=#{self.user_id}") message = public_message(post, url) - configure_twitter - - Twitter.update(message) + client.update(message) end @@ -28,25 +26,14 @@ class Services::Twitter < Service end def profile_photo_url - configure_twitter - - Twitter.profile_image(nickname, :size => "original") + client.user(nickname).profile_image_url_https("original") end private - def configure_twitter - twitter_key = AppConfig.services.twitter.key - twitter_consumer_secret = AppConfig.services.twitter.secret - - if twitter_key.blank? || twitter_consumer_secret.blank? - Rails.logger.info "you have a blank twitter key or secret.... you should look into that" - end - - Twitter.configure do |config| - config.consumer_key = twitter_key - config.consumer_secret = twitter_consumer_secret - config.oauth_token = self.access_token - config.oauth_token_secret = self.access_secret - end + def client + @client ||= Twitter::Client.new( + oauth_token: self.access_token, + oauth_token_secret: self.access_secret + ) end end diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index 32e390b5b..e96b8c9ed 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -5,10 +5,16 @@ Rails.application.config.middleware.use OmniAuth::Builder do if AppConfig.services.twitter.enable? provider :twitter, AppConfig.services.twitter.key, AppConfig.services.twitter.secret + Twitter.configure do |config| + config.consumer_key = AppConfig.services.twitter.key + config.consumer_secret = AppConfig.services.twitter.secret + end end + if AppConfig.services.tumblr.enable? provider :tumblr, AppConfig.services.tumblr.key, AppConfig.services.tumblr.secret end + if AppConfig.services.facebook.enable? provider :facebook, AppConfig.services.facebook.app_id, AppConfig.services.facebook.secret, { :display => "popup", :scope => "publish_actions,publish_stream,offline_access", diff --git a/spec/models/services/facebook_spec.rb b/spec/models/services/facebook_spec.rb index 792c4df96..ea3b38f79 100644 --- a/spec/models/services/facebook_spec.rb +++ b/spec/models/services/facebook_spec.rb @@ -11,7 +11,7 @@ describe Services::Facebook do describe '#post' do it 'posts a status message to facebook' do - stub_request(:post, "https://graph.facebook.com/me/joindiaspora:make"). + stub_request(:post, "https://graph.facebook.com/me/#{AppConfig.services.facebook.open_graph_namespace}:make"). to_return(:status => 200, :body => "", :headers => {}) @service.post(@post) end @@ -19,13 +19,13 @@ describe Services::Facebook do it 'swallows exception raised by facebook always being down' do pending "temporarily disabled to figure out while some requests are failing" - stub_request(:post,"https://graph.facebook.com/me/joindiaspora:make"). + stub_request(:post,"https://graph.facebook.com/me/#{AppConfig.services.facebook.open_graph_namespace}:make"). to_raise(StandardError) @service.post(@post) end it 'should call public message' do - stub_request(:post, "https://graph.facebook.com/me/joindiaspora:make"). + stub_request(:post, "https://graph.facebook.com/me/#{AppConfig.services.facebook.open_graph_namespace}:make"). to_return(:status => 200) url = "foo" @service.should_not_receive(:public_message) diff --git a/spec/models/services/tumblr_spec.rb b/spec/models/services/tumblr_spec.rb index 86f57e56d..83d2ad78e 100644 --- a/spec/models/services/tumblr_spec.rb +++ b/spec/models/services/tumblr_spec.rb @@ -11,14 +11,12 @@ describe Services::Tumblr do describe '#post' do it 'posts a status message to tumblr' do + response = mock + response.stub(:body).and_return('{"response": {"user": {"blogs": [{"url": "http://foo.tumblr.com"}]}}}') + OAuth::AccessToken.any_instance.should_receive(:get).with("/v2/user/info").and_return(response) OAuth::AccessToken.any_instance.should_receive(:post) @service.post(@post) end - - it 'swallows exception raised by tumblr not being webscale' do - OAuth::AccessToken.any_instance.should_receive(:post).and_raise(StandardError) - @service.post(@post) - end end end diff --git a/spec/models/services/twitter_spec.rb b/spec/models/services/twitter_spec.rb index 988656db9..ccc2e154a 100644 --- a/spec/models/services/twitter_spec.rb +++ b/spec/models/services/twitter_spec.rb @@ -11,18 +11,18 @@ describe Services::Twitter do describe '#post' do it 'posts a status message to twitter' do - Twitter.should_receive(:update).with(instance_of(String)) + Twitter::Client.any_instance.should_receive(:update).with(instance_of(String)) @service.post(@post) end it 'swallows exception raised by twitter always being down' do pending - Twitter.should_receive(:update).and_raise(StandardError) + Twitter::Client.any_instance.should_receive(:update).and_raise(StandardError) @service.post(@post) end it 'should call public message' do - Twitter.stub!(:update) + Twitter::Client.any_instance.stub(:update) url = "foo" @service.should_receive(:public_message).with(@post, url) @service.post(@post, url) @@ -74,12 +74,12 @@ describe Services::Twitter do end describe "#profile_photo_url" do it 'returns the original profile photo url' do - stub_request(:get, "https://api.twitter.com/1/users/profile_image/joindiaspora?size=original"). - to_return(:status => 302, :body => "", :headers => {:location => "http://a2.twimg.com/profile_images/uid/avatar.png"}) + user_stub = stub + user_stub.should_receive(:profile_image_url_https).with("original").and_return("http://a2.twimg.com/profile_images/uid/avatar.png") + Twitter::Client.any_instance.should_receive(:user).with("joindiaspora").and_return(user_stub) @service.nickname = "joindiaspora" - @service.profile_photo_url.should == - "http://a2.twimg.com/profile_images/uid/avatar.png" + @service.profile_photo_url.should == "http://a2.twimg.com/profile_images/uid/avatar.png" end end end From e974d2934fb8000f521fec06eac6a0f95d350706 Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Thu, 1 Nov 2012 23:21:22 +0200 Subject: [PATCH 2/2] Always use basic Facebook Graph API to post messages, fixes public posts. Remove unnecessary Facebook opengraph config items --- Changelog.md | 1 + app/models/services/facebook.rb | 10 +--------- config/defaults.yml | 1 - config/diaspora.yml.example | 3 --- spec/models/services/facebook_spec.rb | 6 +++--- 5 files changed, 5 insertions(+), 16 deletions(-) diff --git a/Changelog.md b/Changelog.md index baf62dbf2..86cf393af 100644 --- a/Changelog.md +++ b/Changelog.md @@ -31,6 +31,7 @@ * Fix javascripts error in invitations facebox. [#3638](https://github.com/diaspora/diaspora/pull/3638) * Fix css overflow problem in aspect dropdown on welcome page. [#3637](https://github.com/diaspora/diaspora/pull/3637) * Fix empty page after authenticating with other services. [#3693](https://github.com/diaspora/diaspora/pull/3693) +* Fix posting public posts to Facebook. [#2882](https://github.com/diaspora/diaspora/issues/2882), [#3650](https://github.com/diaspora/diaspora/issues/3650) # 0.0.1.2 diff --git a/app/models/services/facebook.rb b/app/models/services/facebook.rb index 39d2b1086..45ee65817 100644 --- a/app/models/services/facebook.rb +++ b/app/models/services/facebook.rb @@ -11,21 +11,13 @@ class Services::Facebook < Service def post(post, url='') Rails.logger.debug("event=post_to_service type=facebook sender_id=#{self.user_id}") - if post.public? - post_to_facebook("https://graph.facebook.com/me/joindiaspora:make", create_open_graph_params(post).to_param) - else - post_to_facebook("https://graph.facebook.com/me/feed", create_post_params(post).to_param) - end + post_to_facebook("https://graph.facebook.com/me/feed", create_post_params(post).to_param) end def post_to_facebook(url, body) Faraday.post(url, body) end - def create_open_graph_params(post) - {:post => "#{AppConfig.pod_uri.to_s}#{short_post_path(post)}", :access_token => self.access_token} - end - def create_post_params(post) message = post.text(:plain_text => true) {:message => message, :access_token => self.access_token, :link => URI.extract(message, ['https', 'http']).first} diff --git a/config/defaults.yml b/config/defaults.yml index fffeeb1a9..169edaa7e 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -56,7 +56,6 @@ defaults: enable: false app_id: secret: - open_graph_namespace: 'joindiaspora' twitter: enable: false key: diff --git a/config/diaspora.yml.example b/config/diaspora.yml.example index b9b241c5e..1932a7a6e 100644 --- a/config/diaspora.yml.example +++ b/config/diaspora.yml.example @@ -252,9 +252,6 @@ configuration: ## Section #enable: true #app_id: 'abcdef' #secret: 'changeme' - ## this will be the namespace for your object, - ## it should be configured in your FB app - #open_graph_namespace: ## OAuth credentials for Twitter: twitter: ## Section diff --git a/spec/models/services/facebook_spec.rb b/spec/models/services/facebook_spec.rb index 792c4df96..f401b83a5 100644 --- a/spec/models/services/facebook_spec.rb +++ b/spec/models/services/facebook_spec.rb @@ -11,7 +11,7 @@ describe Services::Facebook do describe '#post' do it 'posts a status message to facebook' do - stub_request(:post, "https://graph.facebook.com/me/joindiaspora:make"). + stub_request(:post, "https://graph.facebook.com/me/feed"). to_return(:status => 200, :body => "", :headers => {}) @service.post(@post) end @@ -19,13 +19,13 @@ describe Services::Facebook do it 'swallows exception raised by facebook always being down' do pending "temporarily disabled to figure out while some requests are failing" - stub_request(:post,"https://graph.facebook.com/me/joindiaspora:make"). + stub_request(:post,"https://graph.facebook.com/me/feed"). to_raise(StandardError) @service.post(@post) end it 'should call public message' do - stub_request(:post, "https://graph.facebook.com/me/joindiaspora:make"). + stub_request(:post, "https://graph.facebook.com/me/feed"). to_return(:status => 200) url = "foo" @service.should_not_receive(:public_message)