From cfb28db00fef93c4899d9f290e2e4f364f28e2f7 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Tue, 11 Oct 2011 14:29:18 -0700 Subject: [PATCH] wip, but the refactored code works --- app/helpers/o_embed_helper.rb | 32 +++++++++++ app/models/o_embed_cache.rb | 21 +++++++- app/models/post.rb | 2 +- app/models/status_message.rb | 2 +- .../status_messages/_status_message.haml | 2 +- .../status_messages/_status_message.html.haml | 2 + lib/diaspora/markdownify.rb | 53 ------------------- 7 files changed, 56 insertions(+), 58 deletions(-) create mode 100644 app/helpers/o_embed_helper.rb diff --git a/app/helpers/o_embed_helper.rb b/app/helpers/o_embed_helper.rb new file mode 100644 index 000000000..aa6559faa --- /dev/null +++ b/app/helpers/o_embed_helper.rb @@ -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 diff --git a/app/models/o_embed_cache.rb b/app/models/o_embed_cache.rb index 6ae29b8a3..277564d8b 100644 --- a/app/models/o_embed_cache.rb +++ b/app/models/o_embed_cache.rb @@ -5,7 +5,7 @@ class OEmbedCache < ActiveRecord::Base has_many :posts 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? cache.fetch_and_save_oembed_data! cache @@ -19,7 +19,24 @@ class OEmbedCache < ActiveRecord::Base else self.data = response.fields self.data['trusted_endpoint_url'] = response.provider.endpoint - cache.save + self.save 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 diff --git a/app/models/post.rb b/app/models/post.rb index 7cf9d4185..9199a5b15 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -28,7 +28,7 @@ class Post < ActiveRecord::Base has_many :reshares, :class_name => "Reshare", :foreign_key => :root_guid, :primary_key => :guid 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' diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 72ac5cf67..9054eaf61 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -157,7 +157,7 @@ class StatusMessage < Post end def queue_gather_oembed_data - Resque.enqueue(Jobs::GatherOEmbedData, self.oembed_url) + Resque.enqueue(Jobs::GatherOEmbedData, self.id, self.oembed_url) end def contains_oembed_url_in_text? diff --git a/app/views/status_messages/_status_message.haml b/app/views/status_messages/_status_message.haml index 2a6347d66..6048f085a 100644 --- a/app/views/status_messages/_status_message.haml +++ b/app/views/status_messages/_status_message.haml @@ -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' %div{:class => direction_for(post.text)} - != markdownify(post, :oembed => true, :youtube_maps => post[:youtube_titles]) + != markdownify(post, :youtube_maps => post[:youtube_titles]) diff --git a/app/views/status_messages/_status_message.html.haml b/app/views/status_messages/_status_message.html.haml index ec8343327..193b13b66 100644 --- a/app/views/status_messages/_status_message.html.haml +++ b/app/views/status_messages/_status_message.html.haml @@ -17,3 +17,5 @@ %div{:class => direction_for(post.text)} != markdownify(post, :youtube_maps => post[:youtube_titles]) + - if post.o_embed_cache_id.present? + = o_embed_html(post.o_embed_cache) diff --git a/lib/diaspora/markdownify.rb b/lib/diaspora/markdownify.rb index 974f97e8a..0349db4ed 100644 --- a/lib/diaspora/markdownify.rb +++ b/lib/diaspora/markdownify.rb @@ -10,58 +10,5 @@ module Diaspora auto_link(link, :link => :urls, :html => { :target => "_blank" }) 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