From 6836af89ad3d35ab6c180d97e10ff27e6949d68f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonne=20Ha=C3=9F?= Date: Mon, 8 Dec 2014 01:32:05 +0100 Subject: [PATCH] Correctly implement OpenGraph tags Just defining og:site_name indicates that we support the OpenGraph protocol, but then we didn't set the required properties. --- app/helpers/open_graph_helper.rb | 66 ++++++++++++++++------- app/views/layouts/_open_graph.haml | 5 ++ app/views/layouts/application.html.haml | 8 ++- app/views/layouts/application.mobile.haml | 5 +- config/defaults.yml | 1 + lib/rack/chrome_frame.rb | 2 +- 6 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 app/views/layouts/_open_graph.haml diff --git a/app/helpers/open_graph_helper.rb b/app/helpers/open_graph_helper.rb index f587bc69d..79b924e88 100644 --- a/app/helpers/open_graph_helper.rb +++ b/app/helpers/open_graph_helper.rb @@ -1,43 +1,71 @@ module OpenGraphHelper - def og_title(post) - meta_tag_with_property('og:title', post_page_title(post, :length => 140)) + def og_title(title) + meta_tag_with_property('og:title', title) end def og_type(post) meta_tag_with_property('og:type', 'article') end - def og_url(post) - meta_tag_with_property('og:url', post_url(post)) + def og_url(url) + meta_tag_with_property('og:url', url) end - def og_image(post) - tags = post.photos.map{|x| meta_tag_with_property('og:image', x.url(:thumb_large))} + def og_image(post=nil) + tags = [] + tags = post.photos.map{|x| meta_tag_with_property('og:image', x.url(:thumb_large))} if post tags << meta_tag_with_property('og:image', default_image_url) if tags.empty? tags.join(' ') end - def og_description(post) - meta_tag_with_property('og:description', post_page_title(post, :length => 1000)) + def og_description(description) + meta_tag_with_property('og:description', description) end - def og_type - meta_tag_with_property('og:type', og_namespace('frame')) + def og_type(type='website') + meta_tag_with_property('og:type', type) end - def og_namespace(object) - namespace = AppConfig.services.facebook.open_graph_namespace.present? ? AppConfig.services.facebook.open_graph_namespace : 'joindiaspora' - "#{namespace}:frame" + def og_namespace + AppConfig.services.facebook.open_graph_namespace end - def og_page_specific_tags(post) - [og_title(post), og_type, - og_url(post), og_image(post), - og_description(post)].join(' ').html_safe + def og_site_name + meta_tag_with_property('og:site_name', AppConfig.settings.pod_name) + end + + def og_common_tags + [og_site_name] + end + + def og_general_tags + [ + *og_common_tags, + og_type, + og_title('diaspora* social network'), + og_image, + og_url(AppConfig.environment.url), + og_description('diaspora* is the online social world where you are in control.') + ].join("\n").html_safe + end + + def og_page_post_tags(post) + [ + *og_common_tags, + og_type("#{og_namespace}:frame"), + og_title(post_page_title(post, :length => 140)), + og_url(post_url(post)), + og_image(post), + og_description(post.message.plain_text_without_markdown truncate: 1000) + ].join("\n").html_safe + end + + def og_prefix + "og: http://ogp.me/ns# #{og_namespace}: https://diasporafoundation.org/ns/joindiaspora#" end def meta_tag_with_property(name, content) - content_tag(:meta, '', :property => name, :content => content) + tag(:meta, :property => name, :content => content) end def og_html(cache) @@ -53,7 +81,7 @@ module OpenGraphHelper def link_to_oembed_image(cache, prefix = 'thumbnail_') link_to(oembed_image_tag(cache, prefix), cache.url, :target => '_blank') end - + def oembed_image_tag(cache, prefix) image_tag(cache.data["#{prefix}url"], cache.options_hash(prefix)) end diff --git a/app/views/layouts/_open_graph.haml b/app/views/layouts/_open_graph.haml new file mode 100644 index 000000000..ff70913df --- /dev/null +++ b/app/views/layouts/_open_graph.haml @@ -0,0 +1,5 @@ +- if @post.present? + %link{:rel => 'alternate', :type => "application/json+oembed", :href => "#{oembed_url(:url => post_url(@post))}"} + = og_page_post_tags(@post) +- else + = og_general_tags diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 1924d2956..57e946f46 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -4,7 +4,7 @@ !!! %html{:lang => I18n.locale.to_s, :dir => (rtl?) ? 'rtl' : 'ltr'} - %head + %head{:prefix => og_prefix} %title = page_title yield(:page_title) @@ -12,11 +12,12 @@ %meta{"http-equiv"=>"Content-Type", :content=>"text/html; charset=utf-8"}/ %meta{:name => "description", :content => "diaspora*"}/ %meta{:name => "author", :content => "Diaspora, Inc."}/ - %meta{:property => "og:site_name", :content => "#{AppConfig.settings.pod_name}"}/ %link{:rel => 'shortcut icon', :href => "#{image_path('favicon.png')}" } %link{:rel => 'apple-touch-icon', :href => "#{image_path('apple-touch-icon.png')}"} + = render 'layouts/open_graph' + = chartbeat_head_block = include_mixpanel @@ -46,9 +47,6 @@ = yield(:head) = csrf_meta_tag - - if @post.present? - %link{:rel => 'alternate', :type => "application/json+oembed", :href => "#{oembed_url(:url => post_url(@post))}"} - = og_page_specific_tags(@post) = include_gon(:camel_case => true) diff --git a/app/views/layouts/application.mobile.haml b/app/views/layouts/application.mobile.haml index ac91997e7..967a8f63f 100644 --- a/app/views/layouts/application.mobile.haml +++ b/app/views/layouts/application.mobile.haml @@ -4,14 +4,13 @@ !!! %html{:lang => I18n.locale.to_s, :dir => (rtl?) ? 'rtl' : 'ltr'} - %head + %head{:prefix => og_prefix} %title = pod_name %meta{:name => "description", :content => "diaspora* mobile"}/ %meta{:name => "author", :content => "Diaspora, Inc."}/ %meta{:charset => 'utf-8'}/ - %meta{:property => "og:site_name", :content => "#{AppConfig.settings.pod_name}"}/ / Viewport scale %meta{:name =>'viewport', :content => "width=device-width, minimum-scale=1 maximum-scale=1"}/ @@ -30,6 +29,8 @@ /%meta{:name => "apple-mobile-web-app-capable", :content => "yes"} /%link{:rel => "apple-touch-startup-image", :href => "/images/apple-splash.png"} + = render 'layouts/open_graph' + = chartbeat_head_block / Stylesheets diff --git a/config/defaults.yml b/config/defaults.yml index 4049cc241..0b64dd50b 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -139,6 +139,7 @@ defaults: enable: false app_id: secret: + open_graph_namespace: 'joindiaspora' twitter: enable: false key: diff --git a/lib/rack/chrome_frame.rb b/lib/rack/chrome_frame.rb index de7234dd1..a81c7d12e 100644 --- a/lib/rack/chrome_frame.rb +++ b/lib/rack/chrome_frame.rb @@ -60,7 +60,7 @@ module Rack HEAD - body.gsub!('', "\n" + head ) + body.gsub!(//, "\n" + head ) body end