wip, but the refactored code works
This commit is contained in:
parent
139ddd726a
commit
cfb28db00f
7 changed files with 56 additions and 58 deletions
32
app/helpers/o_embed_helper.rb
Normal file
32
app/helpers/o_embed_helper.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
module OEmbedHelper
|
||||||
|
def o_embed_html(cache)
|
||||||
|
data = cache.data
|
||||||
|
title = data.fetch('title', 'an awesome post')
|
||||||
|
html ||= link_to(title, cache.url, :target => '_blank')
|
||||||
|
return nil unless data.has_key?('type')
|
||||||
|
case data['type']
|
||||||
|
when 'video', 'rich'
|
||||||
|
if cache.is_trusted_and_has_html?
|
||||||
|
html = data['html']
|
||||||
|
elsif data.has_key?('thumbnail_url')
|
||||||
|
html = link_to_oembed_image(cache)
|
||||||
|
end
|
||||||
|
when 'photo'
|
||||||
|
if data.has_key?('url')
|
||||||
|
img_options = cache.options_hash('')
|
||||||
|
html = link_to_oembed_image(cache, '')
|
||||||
|
end
|
||||||
|
else
|
||||||
|
end
|
||||||
|
|
||||||
|
return html.html_safe
|
||||||
|
end
|
||||||
|
|
||||||
|
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.image_options_hash(prefix))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -5,7 +5,7 @@ class OEmbedCache < ActiveRecord::Base
|
||||||
has_many :posts
|
has_many :posts
|
||||||
|
|
||||||
def self.find_or_create_by_url(url)
|
def self.find_or_create_by_url(url)
|
||||||
cache = OEmbedCache.find_or_build_by_url(url)
|
cache = OEmbedCache.find_or_initialize_by_url(url)
|
||||||
return cache if cache.persisted?
|
return cache if cache.persisted?
|
||||||
cache.fetch_and_save_oembed_data!
|
cache.fetch_and_save_oembed_data!
|
||||||
cache
|
cache
|
||||||
|
|
@ -19,7 +19,24 @@ class OEmbedCache < ActiveRecord::Base
|
||||||
else
|
else
|
||||||
self.data = response.fields
|
self.data = response.fields
|
||||||
self.data['trusted_endpoint_url'] = response.provider.endpoint
|
self.data['trusted_endpoint_url'] = response.provider.endpoint
|
||||||
cache.save
|
self.save
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def is_trusted_and_has_html?
|
||||||
|
self.from_trusted? and self.data.has_key?('html')
|
||||||
|
end
|
||||||
|
|
||||||
|
def from_trusted?
|
||||||
|
SECURE_ENDPOINTS.include?(self.data['trusted_endpoint_url'])
|
||||||
|
end
|
||||||
|
|
||||||
|
def options_hash(prefix = 'thumbnail_')
|
||||||
|
return nil unless self.data.has_key?(prefix + 'url')
|
||||||
|
{
|
||||||
|
:height => self.data.fetch(prefix + 'height', ''),
|
||||||
|
:width => self.data.fetch(prefix + 'width', ''),
|
||||||
|
:alt => self.data.fetch('title', ''),
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ class Post < ActiveRecord::Base
|
||||||
has_many :reshares, :class_name => "Reshare", :foreign_key => :root_guid, :primary_key => :guid
|
has_many :reshares, :class_name => "Reshare", :foreign_key => :root_guid, :primary_key => :guid
|
||||||
has_many :resharers, :class_name => 'Person', :through => :reshares, :source => :author
|
has_many :resharers, :class_name => 'Person', :through => :reshares, :source => :author
|
||||||
|
|
||||||
has_one :o_embed_cache
|
belongs_to :o_embed_cache
|
||||||
|
|
||||||
belongs_to :author, :class_name => 'Person'
|
belongs_to :author, :class_name => 'Person'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -157,7 +157,7 @@ class StatusMessage < Post
|
||||||
end
|
end
|
||||||
|
|
||||||
def queue_gather_oembed_data
|
def queue_gather_oembed_data
|
||||||
Resque.enqueue(Jobs::GatherOEmbedData, self.oembed_url)
|
Resque.enqueue(Jobs::GatherOEmbedData, self.id, self.oembed_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
def contains_oembed_url_in_text?
|
def contains_oembed_url_in_text?
|
||||||
|
|
|
||||||
|
|
@ -16,4 +16,4 @@
|
||||||
= link_to (image_tag photo.url(:thumb_small), :class => 'stream-photo thumb_small', 'data-small-photo' => photo.url(:thumb_medium), 'data-full-photo' => photo.url), photo_path(photo), :class => 'stream-photo-link'
|
= link_to (image_tag photo.url(:thumb_small), :class => 'stream-photo thumb_small', 'data-small-photo' => photo.url(:thumb_medium), 'data-full-photo' => photo.url), photo_path(photo), :class => 'stream-photo-link'
|
||||||
|
|
||||||
%div{:class => direction_for(post.text)}
|
%div{:class => direction_for(post.text)}
|
||||||
!= markdownify(post, :oembed => true, :youtube_maps => post[:youtube_titles])
|
!= markdownify(post, :youtube_maps => post[:youtube_titles])
|
||||||
|
|
|
||||||
|
|
@ -17,3 +17,5 @@
|
||||||
|
|
||||||
%div{:class => direction_for(post.text)}
|
%div{:class => direction_for(post.text)}
|
||||||
!= markdownify(post, :youtube_maps => post[:youtube_titles])
|
!= markdownify(post, :youtube_maps => post[:youtube_titles])
|
||||||
|
- if post.o_embed_cache_id.present?
|
||||||
|
= o_embed_html(post.o_embed_cache)
|
||||||
|
|
|
||||||
|
|
@ -10,58 +10,5 @@ module Diaspora
|
||||||
auto_link(link, :link => :urls, :html => { :target => "_blank" })
|
auto_link(link, :link => :urls, :html => { :target => "_blank" })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class HTMLwithOEmbed < Redcarpet::Render::HTML
|
|
||||||
include ActionView::Helpers::UrlHelper
|
|
||||||
include ActionView::Helpers::TextHelper
|
|
||||||
include ActionView::Helpers::TagHelper
|
|
||||||
include ActionView::Helpers::AssetTagHelper
|
|
||||||
include ActionView::Helpers::RawOutputHelper
|
|
||||||
|
|
||||||
def autolink(link, type)
|
|
||||||
#auto_link(link, :link => :urls, :html => { :target => "_blank" })
|
|
||||||
|
|
||||||
title = link
|
|
||||||
url = auto_link(link, :link => :urls).scan(/href=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/).first.first
|
|
||||||
url = CGI::unescapeHTML(url)
|
|
||||||
|
|
||||||
cache = OEmbedCache.find_by_url(url)
|
|
||||||
if not cache.nil? and cache.data.has_key?('type')
|
|
||||||
case cache.data['type']
|
|
||||||
when 'video', 'rich'
|
|
||||||
if SECURE_ENDPOINTS.include?(cache.data['trusted_endpoint_url']) and cache.data.has_key?('html')
|
|
||||||
rep = raw(cache.data['html'])
|
|
||||||
elsif cache.data.has_key?('thumbnail_url')
|
|
||||||
img_options = {}
|
|
||||||
img_options.merge!({:height => cache.data['thumbnail_height'],
|
|
||||||
:width => cache.data['thumbnail_width']}) if cache.data.has_key?('thumbnail_width') and cache.data.has_key?('thumbnail_height')
|
|
||||||
img_options[:alt] = cache.data['title'] if cache.data.has_key?('title')
|
|
||||||
rep = link_to(image_tag(cache.data['thumbnail_url'], img_options),
|
|
||||||
url, :target => '_blank')
|
|
||||||
end
|
|
||||||
|
|
||||||
when 'photo'
|
|
||||||
if cache.data.has_key?('url')
|
|
||||||
img_options = {}
|
|
||||||
img_options.merge!({:height => cache.data['height'],
|
|
||||||
:width => cache.data['width']}) if cache.data.has_key?('width') and cache.data.has_key?('height')
|
|
||||||
img_options[:alt] = cache.data['title'] if cache.data.has_key?('title')
|
|
||||||
rep = link_to(image_tag(cache.data['url'], img_options),
|
|
||||||
url, :target => '_blank')
|
|
||||||
end
|
|
||||||
else
|
|
||||||
puts "mega derp"
|
|
||||||
end
|
|
||||||
|
|
||||||
title = cache.data['title'] \
|
|
||||||
if cache.data.has_key?('title') and \
|
|
||||||
not cache.data['title'].blank?
|
|
||||||
end
|
|
||||||
|
|
||||||
rep ||= link_to(title, url, :target => '_blank') if rep.blank?
|
|
||||||
return rep
|
|
||||||
end
|
|
||||||
end
|
|
||||||
>>>>>>> wip
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue