oembed mostly works
This commit is contained in:
parent
31172a9959
commit
c9e3852de8
7 changed files with 68 additions and 18 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -20,6 +20,6 @@ module PostsHelper
|
|||
def post_iframe_url(post_id, opts={})
|
||||
opts[:width] ||= 516
|
||||
opts[:height] ||= 315
|
||||
"<iframe src='#{Rails.application.routes.url_helpers.post_url(post_id)}' width='#{opts[:width]}px' height='#{opts[:height]}px' frameBorder='0'></iframe>".html_safe
|
||||
"<iframe src='#{Rails.application.routes.url_helpers.post_url(post_id, :host => AppConfig[:pod_uri].host)}' width='#{opts[:width]}px' height='#{opts[:height]}px' frameBorder='0'></iframe>".html_safe
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Reference in a new issue