diff --git a/Changelog.md b/Changelog.md index 2d7e6f59a03f924197646fb2f6e710ed87b46f20..757acff09863ac790429fc5d3418e937056e738e 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 c37e2d61916e9c122b272b1ce2da834ca77e028e..1533a9fe049809a2f7056e9a04f217901f2cfcd5 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 8b5617508b471707109bc013bae5b8a2e85358f5..07addabc71c61134cdcc58fbf75382da14b298ba 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 39d2b10863f8e52e8011a4357fc8ab779584ba24..4de963e06bb0ea0e693332e24d98f0a0b15e9c9a 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 ab964b6ea8a04d1138e70b9c709c257a6400825e..203d1a349b7d9d0bb3283f7369b226eb70b312a9 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 += "<img src='#{photo.url(:scaled_full)}'/><br>" + html += "})\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 a4730b6e8c1cd468d41d47eeba4f5502271c97cc..37e931d31a778c0fa7ff873f7aa10fe4d216c88e 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 32e390b5bf20cfe391ad80db41250f6bc5fd5e38..e96b8c9ed09b891f886fde22016f878486326ff7 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 792c4df96fa908a2f2a58e2095d9bd15751e02bb..ea3b38f79dc727c58191e75c6d7ccdd5a5548994 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 86f57e56d456b9c6e25a15ffd76a3fd59ad9eacf..83d2ad78eb177a9883acd0bc9896cc483d8e8a47 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 988656db931c527a72a695ad86465233ae25f8c2..ccc2e154abf677ad5f291390471c93bff38e41cb 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