Commit 3b213a94d9682b4aefcc60500f1f00f76e2351b9

Authored by Caio Almeida
Committed by Daniela Feitosa
1 parent 3e5f93f4

Changing the way content is shared on Facebook

* For all types of article, its title, url and beginning of body are shared
* The first image in the body is used as thumbnail
* To use this, it's necessary to add 'www.facebook.com' in the list addthis_options in config/noosfero.yml

(ActionItem1972)
app/helpers/content_viewer_helper.rb
@@ -46,4 +46,13 @@ module ContentViewerHelper @@ -46,4 +46,13 @@ module ContentViewerHelper
46 end 46 end
47 end 47 end
48 48
  49 + def addthis_facebook_url(article)
  50 + "http://www.facebook.com/sharer.php?s=100&p[title]=%{title}&p[summary]=%{summary}&p[url]=%{url}&p[images][0]=%{image}" % {
  51 + :title => CGI.escape(article.title),
  52 + :url => CGI.escape(url_for(article.url)),
  53 + :summary => CGI.escape(truncate(strip_tags(article.body.to_s), 300)),
  54 + :image => CGI.escape(article.body_images_paths.first.to_s)
  55 + }
  56 + end
  57 +
49 end 58 end
app/models/article.rb
@@ -509,6 +509,13 @@ class Article < ActiveRecord::Base @@ -509,6 +509,13 @@ class Article < ActiveRecord::Base
509 self.parent && self.parent.accept_uploads? 509 self.parent && self.parent.accept_uploads?
510 end 510 end
511 511
  512 + def body_images_paths
  513 + require 'uri'
  514 + Hpricot(self.body.to_s).search('img[@src]').collect do |i|
  515 + (self.profile && self.profile.environment) ? URI.join(self.profile.environment.top_url, i.attributes['src']).to_s : i.attributes['src']
  516 + end
  517 + end
  518 +
512 private 519 private
513 520
514 def sanitize_tag_list 521 def sanitize_tag_list
app/views/content_viewer/view_page.rhtml
@@ -38,6 +38,13 @@ @@ -38,6 +38,13 @@
38 <script type="text/javascript"> 38 <script type="text/javascript">
39 addthis_pub = '<%= escape_javascript( NOOSFERO_CONF['addthis_pub'] ) %>'; 39 addthis_pub = '<%= escape_javascript( NOOSFERO_CONF['addthis_pub'] ) %>';
40 addthis_logo = '<%= escape_javascript( NOOSFERO_CONF['addthis_logo'] ) %>'; 40 addthis_logo = '<%= escape_javascript( NOOSFERO_CONF['addthis_logo'] ) %>';
  41 + addthis_config = {
  42 + services_custom: {
  43 + name: 'Facebook',
  44 + url: '<%= addthis_facebook_url(@page) %>',
  45 + icon: 'http://cache.addthiscdn.com/icons/v1/thumbs/facebook.gif'
  46 + }
  47 + };
41 addthis_options = '<%= escape_javascript( NOOSFERO_CONF['addthis_options'] ) %>'; 48 addthis_options = '<%= escape_javascript( NOOSFERO_CONF['addthis_options'] ) %>';
42 </script> 49 </script>
43 <a href="http://www.addthis.com/bookmark.php" id="bt_addThis" target="_blank" onmouseover="return addthis_open(this, '', '[URL]')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="/images/bt-bookmark.gif" width="53" height="16" border="0" alt="" /></a><script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script> 50 <a href="http://www.addthis.com/bookmark.php" id="bt_addThis" target="_blank" onmouseover="return addthis_open(this, '', '[URL]')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="/images/bt-bookmark.gif" width="53" height="16" border="0" alt="" /></a><script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script>
test/unit/article_test.rb
@@ -1526,4 +1526,33 @@ class ArticleTest &lt; Test::Unit::TestCase @@ -1526,4 +1526,33 @@ class ArticleTest &lt; Test::Unit::TestCase
1526 assert_includes Article.find_by_contents('thing'), art2 1526 assert_includes Article.find_by_contents('thing'), art2
1527 end 1527 end
1528 1528
  1529 + should 'get images paths in article body' do
  1530 + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
  1531 + a = TinyMceArticle.new :profile => @profile
  1532 + a.body = 'Noosfero <img src="http://noosfero.com/test.png" /> test <img src="http://test.com/noosfero.png" />'
  1533 + assert_includes a.body_images_paths, 'http://noosfero.com/test.png'
  1534 + assert_includes a.body_images_paths, 'http://test.com/noosfero.png'
  1535 + end
  1536 +
  1537 + should 'get absolute images paths in article body' do
  1538 + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
  1539 + a = TinyMceArticle.new :profile => @profile
  1540 + a.body = 'Noosfero <img src="test.png" alt="Absolute" /> test <img src="/relative/path.png" />'
  1541 + assert_includes a.body_images_paths, 'http://noosfero.org/test.png'
  1542 + assert_includes a.body_images_paths, 'http://noosfero.org/relative/path.png'
  1543 + end
  1544 +
  1545 + should 'return empty if there are no images in article body' do
  1546 + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
  1547 + a = Event.new :profile => @profile
  1548 + a.body = 'Noosfero test'
  1549 + assert_equal [], a.body_images_paths
  1550 + end
  1551 +
  1552 + should 'return empty if body is nil' do
  1553 + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
  1554 + a = Article.new :profile => @profile
  1555 + assert_equal [], a.body_images_paths
  1556 + end
  1557 +
1529 end 1558 end
test/unit/content_viewer_helper_test.rb
@@ -83,6 +83,42 @@ class ContentViewerHelperTest &lt; Test::Unit::TestCase @@ -83,6 +83,42 @@ class ContentViewerHelperTest &lt; Test::Unit::TestCase
83 assert_no_match /feed/, result 83 assert_no_match /feed/, result
84 end 84 end
85 85
  86 + should 'generate facebook addthis url for article' do
  87 + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
  88 + [TextileArticle, Blog, Folder, Gallery, UploadedFile, Forum, Event, TextArticle, TinyMceArticle].each do |model|
  89 + a = model.new(:name => 'Some title', :body => 'Some text here.', :profile => profile)
  90 + assert_equal "http://www.facebook.com/sharer.php?s=100&p[title]=Some+title&p[summary]=Some+text+here.&p[url]=http%3A%2F%2Fnoosfero.org%2Fblog_helper_test%2Fsome-title&p[images][0]=", addthis_facebook_url(a)
  91 + end
  92 + end
  93 +
  94 + should 'generate facebook addthis url without body' do
  95 + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
  96 + a = TinyMceArticle.new(:name => 'Test', :body => nil, :profile => profile)
  97 + assert_equal "http://www.facebook.com/sharer.php?s=100&p[title]=Test&p[summary]=&p[url]=http%3A%2F%2Fnoosfero.org%2Fblog_helper_test%2Ftest&p[images][0]=", addthis_facebook_url(a)
  98 + end
  99 +
  100 + should 'generate facebook addthis url without tags in body' do
  101 + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
  102 + a = TinyMceArticle.new(:name => 'Some title', :body => '<p>This <b class="bold">is</b> a test</p>', :profile => profile)
  103 + assert_equal "http://www.facebook.com/sharer.php?s=100&p[title]=Some+title&p[summary]=This+is+a+test&p[url]=http%3A%2F%2Fnoosfero.org%2Fblog_helper_test%2Fsome-title&p[images][0]=", addthis_facebook_url(a)
  104 + end
  105 +
  106 + should 'generate facebook addthis url with truncated body' do
  107 + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
  108 + a = TinyMceArticle.new(:name => 'Some title', :body => 'test' * 76, :profile => profile)
  109 + assert_equal "http://www.facebook.com/sharer.php?s=100&p[title]=Some+title&p[summary]=#{'test' * 74}t...&p[url]=http%3A%2F%2Fnoosfero.org%2Fblog_helper_test%2Fsome-title&p[images][0]=", addthis_facebook_url(a)
  110 + end
  111 +
  112 + should 'generate facebook addthis url for tinymce article with images' do
  113 + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
  114 + a = TinyMceArticle.new(:name => 'Some title', :body => '<p>This <b class="bold">is</b> a <img src="/images/x.png" />test</p>', :profile => profile)
  115 + assert_equal "http://www.facebook.com/sharer.php?s=100&p[title]=Some+title&p[summary]=This+is+a+test&p[url]=http%3A%2F%2Fnoosfero.org%2Fblog_helper_test%2Fsome-title&p[images][0]=http%3A%2F%2Fnoosfero.org%2Fimages%2Fx.png", addthis_facebook_url(a)
  116 + end
  117 +
  118 + protected
  119 +
  120 + include ActionView::Helpers::TextHelper
  121 +
86 end 122 end
87 123
88 def show_date(date) 124 def show_date(date)
@@ -96,3 +132,9 @@ def _(text) @@ -96,3 +132,9 @@ def _(text)
96 end 132 end
97 def will_paginate(arg1, arg2) 133 def will_paginate(arg1, arg2)
98 end 134 end
  135 +def strip_tags(html)
  136 + html.gsub(/<[^>]+>/, '')
  137 +end
  138 +def url_for(args = {})
  139 + ['http:/', args[:host], args[:profile], args[:page]].join('/')
  140 +end