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.
This commit is contained in:
Jonne Haß 2014-12-08 01:32:05 +01:00
parent 60a5d2a5ba
commit 6836af89ad
6 changed files with 60 additions and 27 deletions

View file

@ -1,43 +1,71 @@
module OpenGraphHelper module OpenGraphHelper
def og_title(post) def og_title(title)
meta_tag_with_property('og:title', post_page_title(post, :length => 140)) meta_tag_with_property('og:title', title)
end end
def og_type(post) def og_type(post)
meta_tag_with_property('og:type', 'article') meta_tag_with_property('og:type', 'article')
end end
def og_url(post) def og_url(url)
meta_tag_with_property('og:url', post_url(post)) meta_tag_with_property('og:url', url)
end end
def og_image(post) def og_image(post=nil)
tags = post.photos.map{|x| meta_tag_with_property('og:image', x.url(:thumb_large))} 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 << meta_tag_with_property('og:image', default_image_url) if tags.empty?
tags.join(' ') tags.join(' ')
end end
def og_description(post) def og_description(description)
meta_tag_with_property('og:description', post_page_title(post, :length => 1000)) meta_tag_with_property('og:description', description)
end end
def og_type def og_type(type='website')
meta_tag_with_property('og:type', og_namespace('frame')) meta_tag_with_property('og:type', type)
end end
def og_namespace(object) def og_namespace
namespace = AppConfig.services.facebook.open_graph_namespace.present? ? AppConfig.services.facebook.open_graph_namespace : 'joindiaspora' AppConfig.services.facebook.open_graph_namespace
"#{namespace}:frame"
end end
def og_page_specific_tags(post) def og_site_name
[og_title(post), og_type, meta_tag_with_property('og:site_name', AppConfig.settings.pod_name)
og_url(post), og_image(post), end
og_description(post)].join(' ').html_safe
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 end
def meta_tag_with_property(name, content) def meta_tag_with_property(name, content)
content_tag(:meta, '', :property => name, :content => content) tag(:meta, :property => name, :content => content)
end end
def og_html(cache) def og_html(cache)

View file

@ -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

View file

@ -4,7 +4,7 @@
!!! !!!
%html{:lang => I18n.locale.to_s, :dir => (rtl?) ? 'rtl' : 'ltr'} %html{:lang => I18n.locale.to_s, :dir => (rtl?) ? 'rtl' : 'ltr'}
%head %head{:prefix => og_prefix}
%title %title
= page_title yield(:page_title) = page_title yield(:page_title)
@ -12,11 +12,12 @@
%meta{"http-equiv"=>"Content-Type", :content=>"text/html; charset=utf-8"}/ %meta{"http-equiv"=>"Content-Type", :content=>"text/html; charset=utf-8"}/
%meta{:name => "description", :content => "diaspora*"}/ %meta{:name => "description", :content => "diaspora*"}/
%meta{:name => "author", :content => "Diaspora, Inc."}/ %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 => 'shortcut icon', :href => "#{image_path('favicon.png')}" }
%link{:rel => 'apple-touch-icon', :href => "#{image_path('apple-touch-icon.png')}"} %link{:rel => 'apple-touch-icon', :href => "#{image_path('apple-touch-icon.png')}"}
= render 'layouts/open_graph'
= chartbeat_head_block = chartbeat_head_block
= include_mixpanel = include_mixpanel
@ -46,9 +47,6 @@
= yield(:head) = yield(:head)
= csrf_meta_tag = 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) = include_gon(:camel_case => true)

View file

@ -4,14 +4,13 @@
!!! !!!
%html{:lang => I18n.locale.to_s, :dir => (rtl?) ? 'rtl' : 'ltr'} %html{:lang => I18n.locale.to_s, :dir => (rtl?) ? 'rtl' : 'ltr'}
%head %head{:prefix => og_prefix}
%title %title
= pod_name = pod_name
%meta{:name => "description", :content => "diaspora* mobile"}/ %meta{:name => "description", :content => "diaspora* mobile"}/
%meta{:name => "author", :content => "Diaspora, Inc."}/ %meta{:name => "author", :content => "Diaspora, Inc."}/
%meta{:charset => 'utf-8'}/ %meta{:charset => 'utf-8'}/
%meta{:property => "og:site_name", :content => "#{AppConfig.settings.pod_name}"}/
/ Viewport scale / Viewport scale
%meta{:name =>'viewport', :content => "width=device-width, minimum-scale=1 maximum-scale=1"}/ %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"} /%meta{:name => "apple-mobile-web-app-capable", :content => "yes"}
/%link{:rel => "apple-touch-startup-image", :href => "/images/apple-splash.png"} /%link{:rel => "apple-touch-startup-image", :href => "/images/apple-splash.png"}
= render 'layouts/open_graph'
= chartbeat_head_block = chartbeat_head_block
/ Stylesheets / Stylesheets

View file

@ -139,6 +139,7 @@ defaults:
enable: false enable: false
app_id: app_id:
secret: secret:
open_graph_namespace: 'joindiaspora'
twitter: twitter:
enable: false enable: false
key: key:

View file

@ -60,7 +60,7 @@ module Rack
<meta http-equiv="X-UA-Compatible" content="chrome=1"> <meta http-equiv="X-UA-Compatible" content="chrome=1">
HEAD HEAD
body.gsub!('<head>', "<head>\n" + head ) body.gsub!(/<head(.*)>/, "<head\\1>\n" + head )
body body
end end