From 31172a9959a4ee6bdc3235929a874be9612583e3 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Tue, 17 Apr 2012 23:14:45 -0700 Subject: [PATCH 1/3] basic oembed provider set up... too tried to finish this now --- app/helpers/posts_helper.rb | 2 +- app/presenters/o_embed_presenter.rb | 44 +++++++++++++++++++++++ spec/presenters/o_embed_presenter_spec.rb | 6 ++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 app/presenters/o_embed_presenter.rb create mode 100644 spec/presenters/o_embed_presenter_spec.rb diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb index 35e981a1c..d8a2bd17e 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 new file mode 100644 index 000000000..a5a312a3c --- /dev/null +++ b/app/presenters/o_embed_presenter.rb @@ -0,0 +1,44 @@ +class OEmbedPresenter + include PostsHelper + + def initialize(post, opts = {}) + @post = post + @opts = opts + end + + def to_json(opts={}) + as_json(opts).to_json + end + + def as_json(opts) + { + :provider_name => "Diaspora", + :provider_url => AppConfig[:pod_url], + :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), + :html => iframe_html + } + end + + private + + def post_title + @post.text + end + + def post_author + @post.author.name + end + + def post_author_url + Rails.application.routes.url_helpers.person_url(@post.author) + end + + def iframe_html + post_iframe_url(@post.id) + end +end \ No newline at end of file diff --git a/spec/presenters/o_embed_presenter_spec.rb b/spec/presenters/o_embed_presenter_spec.rb new file mode 100644 index 000000000..cb24c955d --- /dev/null +++ b/spec/presenters/o_embed_presenter_spec.rb @@ -0,0 +1,6 @@ +require 'spec_helper' +describe OEmbedPresenter do + it 'works' do + OEmbedPresenter.new(Factory(:status_message)).to_json.should_not be_nil + end +end \ No newline at end of file From c9e3852de8c63659698c4ddda9f4775d21a7ebed Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Wed, 18 Apr 2012 11:54:14 -0700 Subject: [PATCH 2/3] oembed mostly works --- app/controllers/posts_controller.rb | 27 +++++++++++++------ app/helpers/posts_helper.rb | 2 +- app/presenters/o_embed_presenter.rb | 19 ++++++++----- app/views/layouts/post.html.haml | 2 ++ config/environments/test.rb | 2 ++ config/routes.rb | 1 + spec/presenters/o_embed_presenter_spec.rb | 33 +++++++++++++++++++++-- 7 files changed, 68 insertions(+), 18 deletions(-) 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 From ab48ca260bf62c6367280bd337226700a5d68821 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Wed, 18 Apr 2012 15:43:38 -0700 Subject: [PATCH 3/3] kinda ugly hack for ports in url with iframes --- app/controllers/posts_controller.rb | 8 ++++++-- app/helpers/posts_helper.rb | 3 ++- spec/controllers/posts_controller_spec.rb | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 81e4e8726..60d0ec9f3 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -56,8 +56,12 @@ class PostsController < ApplicationController 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 + if post.present? + oembed = OEmbedPresenter.new(post, params.slice(:format, :maxheight, :minheight)) + render :json => oembed + else + render :nothing => true, :status => 404 + end end def destroy diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb index 9f5c4aeaf..e12ed5901 100644 --- a/app/helpers/posts_helper.rb +++ b/app/helpers/posts_helper.rb @@ -20,6 +20,7 @@ module PostsHelper def post_iframe_url(post_id, opts={}) opts[:width] ||= 516 opts[:height] ||= 315 - "".html_safe + host = AppConfig[:pod_uri].port ==80 ? AppConfig[:pod_uri].host : "#{AppConfig[:pod_uri].host}:#{AppConfig[:pod_uri].port}" + "".html_safe end end diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index 2a53a661b..221beaca8 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -120,6 +120,19 @@ describe PostsController do end end + describe 'oembed' do + it 'works when you can see it' do + sign_in alice + get :oembed, :url => "/posts/#{@message.id}" + response.body.should match /iframe/ + end + + it 'returns a 404 response when the post is not found' do + get :oembed, :url => "/posts/#{@message.id}" + response.should_not be_success + end + end + describe '#destroy' do before do sign_in alice