diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 5dee5bb46..81e4e8726 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -7,7 +7,7 @@ require Rails.root.join("app", "presenters", "post_presenter") class PostsController < ApplicationController include PostsHelper - before_filter :authenticate_user!, :except => [:show, :iframe] + before_filter :authenticate_user!, :except => [:show, :iframe, :oembed] before_filter :set_format_if_malformed_from_status_net, :only => :show layout 'post' @@ -24,13 +24,7 @@ class PostsController < ApplicationController end def show - key = params[:id].to_s.length <= 8 ? :id : :guid - - if user_signed_in? - @post = current_user.find_visible_shareable_by_id(Post, params[:id], :key => key) - else - @post = Post.where(key => params[:id], :public => true).includes(:author, :comments => :author).first - end + @post = find_by_guid_or_id_with_current_user(params[:id]) if @post # @commenting_disabled = can_not_comment_on_post? @@ -59,6 +53,13 @@ class PostsController < ApplicationController render :text => post_iframe_url(params[:id]), :layout => false end + def oembed + post_id = OEmbedPresenter.id_from_url(params.delete(:url)) + post = find_by_guid_or_id_with_current_user(post_id) + oembed = OEmbedPresenter.new(post, params.slice(:format, :maxheight, :minheight)) + render :json => oembed + end + def destroy @post = current_user.posts.where(:id => params[:id]).first if @post @@ -76,6 +77,16 @@ class PostsController < ApplicationController private + def find_by_guid_or_id_with_current_user(id) + key = id.to_s.length <= 8 ? :id : :guid + if user_signed_in? + current_user.find_visible_shareable_by_id(Post, id, :key => key) + else + Post.where(key => id, :public => true).includes(:author, :comments => :author).first + end + + end + def set_format_if_malformed_from_status_net request.format = :html if request.format == 'application/html+xml' end diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb index d8a2bd17e..9f5c4aeaf 100644 --- a/app/helpers/posts_helper.rb +++ b/app/helpers/posts_helper.rb @@ -20,6 +20,6 @@ module PostsHelper def post_iframe_url(post_id, opts={}) opts[:width] ||= 516 opts[:height] ||= 315 - "".html_safe + "".html_safe end end diff --git a/app/presenters/o_embed_presenter.rb b/app/presenters/o_embed_presenter.rb index a5a312a3c..0723b7df9 100644 --- a/app/presenters/o_embed_presenter.rb +++ b/app/presenters/o_embed_presenter.rb @@ -1,5 +1,7 @@ +require 'uri' class OEmbedPresenter include PostsHelper + include ActionView::Helpers::TextHelper def initialize(post, opts = {}) @post = post @@ -10,24 +12,27 @@ class OEmbedPresenter as_json(opts).to_json end - def as_json(opts) + def as_json(opts={}) { :provider_name => "Diaspora", :provider_url => AppConfig[:pod_url], + :type => 'rich', :version => '1.0', :title => post_title, :author_name => post_author, :author_url => post_author_url, - :width => @opts.fetch(:height, 516), - :height => @opts.fetch(:width, 320), + :width => @opts.fetch(:maxwidth, 516), + :height => @opts.fetch(:maxheight, 320), :html => iframe_html } end - private + def self.id_from_url(url) + URI.parse(url).path.gsub(%r{\/posts\/|\/p\/}, '') + end def post_title - @post.text + post_page_title(@post) end def post_author @@ -35,10 +40,10 @@ class OEmbedPresenter end def post_author_url - Rails.application.routes.url_helpers.person_url(@post.author) + Rails.application.routes.url_helpers.person_url(@post.author, :host => AppConfig[:pod_uri].host) end def iframe_html - post_iframe_url(@post.id) + post_iframe_url(@post.id, :height => @opts[:maxheight], :width => @opts[:maxwidth]) end end \ No newline at end of file diff --git a/app/views/layouts/post.html.haml b/app/views/layouts/post.html.haml index 75ae0cb19..98f941219 100644 --- a/app/views/layouts/post.html.haml +++ b/app/views/layouts/post.html.haml @@ -44,6 +44,8 @@ = set_asset_host = translation_missing_warnings = current_user_atom_tag + - if @post.present? + %link{:rel => 'alternate', :type => "application/json+oembed", :href => "#{oembed_url(:url => post_url(@post))}"} = yield(:head) = csrf_meta_tag diff --git a/config/environments/test.rb b/config/environments/test.rb index 0a01b0369..1aaa3b27a 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -47,4 +47,6 @@ Diaspora::Application.configure do # This is necessary if your schema can't be completely dumped by the schema dumper, # like if you have constraints or database-specific column types # config.active_record.schema_format = :sql + + config.default_url_options = { :host => AppConfig[:pod_uri].host} end diff --git a/config/routes.rb b/config/routes.rb index d556997cb..daee08957 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ Diaspora::Application.routes.draw do mount RailsAdmin::Engine => '/admin_panel', :as => 'rails_admin' + get 'oembed' => 'posts#oembed', :as => 'oembed' # Posting and Reading resources :reshares diff --git a/spec/presenters/o_embed_presenter_spec.rb b/spec/presenters/o_embed_presenter_spec.rb index cb24c955d..7182b6a1a 100644 --- a/spec/presenters/o_embed_presenter_spec.rb +++ b/spec/presenters/o_embed_presenter_spec.rb @@ -1,6 +1,35 @@ require 'spec_helper' describe OEmbedPresenter do - it 'works' do - OEmbedPresenter.new(Factory(:status_message)).to_json.should_not be_nil + before do + @oembed = OEmbedPresenter.new(Factory(:status_message)) + end + + it 'is a hash' do + @oembed.as_json.should be_a Hash + end + + context 'required options from oembed spec' do + it 'supports maxheight + maxwidth(required)' do + oembed = OEmbedPresenter.new(Factory(:status_message), :maxwidth => 200, :maxheight => 300).as_json + oembed[:width].should == 200 + oembed[:height].should == 300 + end + end + + describe '#iframe_html' do + it 'passes the height options to post_iframe_url' do + @oembed.should_receive(:post_iframe_url).with(instance_of(Fixnum), instance_of(Hash)) + @oembed.iframe_html + end + end + + describe '.id_from_url' do + it 'takes a long post url and gives you the id' do + OEmbedPresenter.id_from_url('http://localhost:400/posts/1').should == "1" + end + + it 'takes a short post url and gives you the id' do + OEmbedPresenter.id_from_url('http://localhost:400/p/1').should == "1" + end end end \ No newline at end of file