diff --git a/app/models/service.rb b/app/models/service.rb index 8f0ed9d1c..cbcce373c 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -15,10 +15,10 @@ class Service < ActiveRecord::Base def public_message(post, length, url = "") Rails.logger.info("Posting out to #{self.class}") - url = "" if post.respond_to?(:photos) && post.photos.count == 0 - space_for_url = url.blank? ? 0 : (url.length + 1) + url = Rails.application.routes.url_helpers.short_post_url(post, :protocol => AppConfig[:pod_uri].scheme, :host => AppConfig[:pod_uri].authority) + space_for_url = 21 + 1 truncated = truncate(post.text(:plain_text => true), :length => (length - space_for_url)) - truncated = "#{truncated} #{url}" unless url.blank? + truncated = "#{truncated} #{url}" return truncated end diff --git a/app/models/services/facebook.rb b/app/models/services/facebook.rb index fd56840b1..636ea5e7b 100644 --- a/app/models/services/facebook.rb +++ b/app/models/services/facebook.rb @@ -1,3 +1,4 @@ +require 'uri' class Services::Facebook < Service include Rails.application.routes.url_helpers @@ -9,11 +10,24 @@ class Services::Facebook < Service def post(post, url='') Rails.logger.debug("event=post_to_service type=facebook sender_id=#{self.user_id}") - Faraday.post("https://graph.facebook.com/me/joindiaspora:make", create_post_params(post).to_param) + 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 + end + + def post_to_facebook(url, body) + Faraday.post(url, body) + end + + def create_open_graph_params(post) + {:post => "#{AppConfig[:pod_url]}#{short_post_path(post)}", :access_token => self.access_token} end def create_post_params(post) - {:post => "#{AppConfig[:pod_url]}#{short_post_path(post)}", :access_token => self.access_token} + message = post.text(:plain_text => true) + {:message => message, :access_token => self.access_token, :link => URI.extract(message, ['https', 'http']).first} end def public_message(post, url) diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 4640eff14..b74842453 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -7,6 +7,7 @@ require 'spec_helper' describe HomeController do describe '#show' do it 'does not redirect' do + sign_out :user get :show response.should_not be_redirect end diff --git a/spec/models/services/facebook_spec.rb b/spec/models/services/facebook_spec.rb index cfdd2e759..96a6d88cb 100644 --- a/spec/models/services/facebook_spec.rb +++ b/spec/models/services/facebook_spec.rb @@ -4,7 +4,7 @@ describe Services::Facebook do before do @user = alice - @post = @user.post(:status_message, :text => "hello", :to =>@user.aspects.first.id) + @post = @user.post(:status_message, :text => "hello", :to =>@user.aspects.first.id, :public =>true) @service = Services::Facebook.new(:access_token => "yeah") @user.services << @service end diff --git a/spec/models/services/twitter_spec.rb b/spec/models/services/twitter_spec.rb index cccd19094..988656db9 100644 --- a/spec/models/services/twitter_spec.rb +++ b/spec/models/services/twitter_spec.rb @@ -11,7 +11,7 @@ describe Services::Twitter do describe '#post' do it 'posts a status message to twitter' do - Twitter.should_receive(:update).with(@post.text) + Twitter.should_receive(:update).with(instance_of(String)) @service.post(@post) end @@ -37,36 +37,38 @@ describe Services::Twitter do it "should not truncate a short message" do short_message = SecureRandom.hex(20) short_post = stub(:text => short_message ) - @service.public_message(short_post, '').should == short_message + @service.public_message(short_post, '').should include(short_message) end + it "should truncate a long message" do long_message = SecureRandom.hex(220) - long_post = stub(:text => long_message ) - @service.public_message(long_post, '').should == long_message.first(137) + "..." + long_post = stub(:text => long_message, :id => 1 ) + @service.public_message(long_post, '').should match long_message.first(100) + end + it "should not truncate a long message with an http url" do - long_message = @long_message_start + " http://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + @long_message_end - long_post = stub(:text => long_message ) - answer = @service.public_message(long_post, '') + long_message = " http://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + @long_message_end + long_post = stub(:text => long_message, :id => 1 ) + @post.text = long_message + answer = @service.public_message(@post, '') - answer.starts_with?( @long_message_start ).should be_true - answer.ends_with?( @long_message_end ).should be_true + answer.should_not match /\.\.\./ end + it "should not truncate a long message with an https url" do - long_message = @long_message_start + " https://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + @long_message_end - long_post = stub(:text => long_message ) - - answer = @service.public_message(long_post, '') - answer.starts_with?( @long_message_start ).should be_true - answer.ends_with?( @long_message_end ).should be_true + long_message = " https://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + @long_message_end + @post.text = long_message + answer = @service.public_message(@post, '') + answer.should_not match /\.\.\./ end + it "should truncate a long message with an ftp url" do long_message = @long_message_start + " ftp://joindiaspora.com/a-very-long-url-name-that-will-be-shortened.html " + @long_message_end - long_post = stub(:text => long_message ) + long_post = stub(:text => long_message, :id => 1 ) answer = @service.public_message(long_post, '') - answer.starts_with?( @long_message_start ).should be_true - answer.ends_with?( @long_message_end ).should_not be_true + answer.should match /\.\.\./ end end