diff --git a/app/models/comment.rb b/app/models/comment.rb index 16edcf412d35044de7a9439aaab9d7d02368abd7..2b67f792191d32c40653f11d54c5d4e967865c64 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -4,8 +4,6 @@ class Comment < ActiveRecord::Base require File.join(Rails.root, 'lib/diaspora/web_socket') - require File.join(Rails.root, 'lib/youtube_titles') - include YoutubeTitles include ROXML include Diaspora::Webhooks @@ -30,7 +28,6 @@ class Comment < ActiveRecord::Base validates :text, :presence => true, :length => { :maximum => 2500 } validates :parent, :presence => true #should be in relayable (pending on fixing Message) - serialize :youtube_titles, Hash scope :including_author, includes(:author => :profile) diff --git a/app/models/status_message.rb b/app/models/status_message.rb index abcb91dacbd344149c0ffeb125b0dd7da06a7921..af72500692550dfd8bbfbdafb08856f3ef865b7d 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -6,8 +6,6 @@ class StatusMessage < Post include Diaspora::Socketable include Diaspora::Taggable - include YoutubeTitles - require File.join(Rails.root, 'lib/youtube_titles') include ActionView::Helpers::TextHelper include PeopleHelper @@ -26,7 +24,6 @@ class StatusMessage < Post attr_accessible :text, :provider_display_name attr_accessor :oembed_url - serialize :youtube_titles, Hash after_create :create_mentions diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml index 78dcc265f36f5fe5740f5b8016d3c4c633253b1e..312ca48aef161b8d4317e7c6142c482426ebef7d 100644 --- a/app/views/comments/_comment.html.haml +++ b/app/views/comments/_comment.html.haml @@ -12,7 +12,7 @@ = person_link(comment.author, :class => "hovercardable") %span{:class => [direction_for(comment.text), 'collapsible']} - = markdownify(comment, :oembed => true, :youtube_maps => comment.youtube_titles) + = markdownify(comment, :oembed => true) .comment_info %time.timeago{:datetime => comment.created_at} diff --git a/app/views/comments/_comment.mobile.haml b/app/views/comments/_comment.mobile.haml index 5b622dfa6e06b8c99b0b294c0bae7ffa68e83a67..3a6bdd586544d953c2d1bd3080cdd50a224e33b6 100644 --- a/app/views/comments/_comment.mobile.haml +++ b/app/views/comments/_comment.mobile.haml @@ -12,5 +12,5 @@ = comment.created_at ? time_ago_in_words(comment.created_at) : time_ago_in_words(Time.now) %div{:class => direction_for(comment.text)} - = markdownify(comment, :youtube_maps => comment[:youtube_titles]) + = markdownify(comment) diff --git a/app/views/status_messages/_status_message.haml b/app/views/status_messages/_status_message.haml index 6048f085a73711a9bd27edd2e136821e4b030d04..9cfd533f0e5ccef8700bdb8f7b4d9e9249664ea8 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, :youtube_maps => post[:youtube_titles]) + != markdownify(post) diff --git a/app/views/status_messages/_status_message.html.haml b/app/views/status_messages/_status_message.html.haml index f86d4c7838d26e3275f37a134a02c81bd084fcd6..a250b1f5aba82f47e7469f893eea5a991611731c 100644 --- a/app/views/status_messages/_status_message.html.haml +++ b/app/views/status_messages/_status_message.html.haml @@ -16,6 +16,6 @@ = 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), 'collapsible']} - != markdownify(post, :youtube_maps => post[:youtube_titles]) + != markdownify(post) - if post.o_embed_cache_id.present? = o_embed_html(post.o_embed_cache) diff --git a/app/views/status_messages/_status_message.mobile.haml b/app/views/status_messages/_status_message.mobile.haml index 8a34f126f5c676d712818ce0a82eeeffeb28b36f..00d743ed2314e471f0df857ce2d29cab949e872f 100644 --- a/app/views/status_messages/_status_message.mobile.haml +++ b/app/views/status_messages/_status_message.mobile.haml @@ -15,4 +15,4 @@ = image_tag post.image_url %div{:class => direction_for(post.text)} - != markdownify(post, :youtube_maps => post[:youtube_titles]) + != markdownify(post) diff --git a/lib/youtube_titles.rb b/lib/youtube_titles.rb deleted file mode 100644 index fcd177d5041f75732fc0c7bce39fe80112298aef..0000000000000000000000000000000000000000 --- a/lib/youtube_titles.rb +++ /dev/null @@ -1,46 +0,0 @@ -module YoutubeTitles - def self.included(model) - model.class_eval do - before_save do - get_youtube_title text - end - end if model.respond_to?(:before_save) - end - - def youtube_title_for video_id - http = Net::HTTP.new('gdata.youtube.com', 80) - path = "/feeds/api/videos/#{video_id}?v=2" - resp, data = http.get(path) - title = data.match(/<title>(.*)<\/title>/) - unless title.nil? - title = title.to_s[7..-9] - end - title || I18n.t('application.helper.video_title.unknown') - end - - def get_youtube_title text - self.youtube_titles = {} - youtube_match = text.enum_for(:scan, YOUTUBE_ID_REGEX).map { Regexp.last_match } - return if youtube_match.empty? - - matches = {} - youtube_match.each do |match_data| - matches[match_data[1]] = CGI::escape(youtube_title_for(match_data[1])) - end - - self.youtube_titles = matches unless matches.empty? - end - - def unserialize_attribute attr_name - if attr_name == "youtube_titles" - begin - super - rescue ActiveRecord::SerializationTypeMismatch - {} - end - else - super - end - end - YOUTUBE_ID_REGEX = /(?:https?:\/\/)(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#!|.+&|.+&)v=)([\w-]{11})(?:\S*(#[^ ]+)|\S+)?/im unless defined? YOUTUBE_ID_REGEX -end diff --git a/spec/lib/youtube_titles_spec.rb b/spec/lib/youtube_titles_spec.rb deleted file mode 100644 index 20e527b3118920b52a45e9006f6e7552835a1c9a..0000000000000000000000000000000000000000 --- a/spec/lib/youtube_titles_spec.rb +++ /dev/null @@ -1,78 +0,0 @@ -require 'spec_helper' -require 'youtube_titles' - -describe YoutubeTitles do - include YoutubeTitles - - before do - @video_id = "ABYnqp-bxvg" - @url="http://www.youtube.com/watch?v=#{@video_id}&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1" - @api_path = "/feeds/api/videos/#{@video_id}?v=2" - end - - describe '#youtube_title_for' do - before do - @expected_title = "UP & down & UP & down &" - @mock_http = mock("http") - Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(@mock_http) - end - - it 'gets a youtube title corresponding to an id' do - @mock_http.should_receive(:get).with(@api_path).and_return( - [nil, "Foobar <title>#{@expected_title}</title> hallo welt <asd><dasdd><a>dsd</a>"]) - youtube_title_for(@video_id).should == @expected_title - end - - it 'returns a fallback for videos with no title' do - @mock_http.should_receive(:get).with(@api_path).and_return( - [nil, "Foobar #{@expected_title}</title> hallo welt <asd><dasdd><a>dsd</a>"]) - youtube_title_for(@video_id).should == I18n.t('application.helper.video_title.unknown') - end - end - - describe 'serialization and marshalling' do - before do - @expected_title = '""Procrastination"" Tales Of Mere Existence' - mock_http = mock("http") - Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http) - mock_http.should_receive(:get).with(@api_path).and_return( - [nil, "Foobar <title>#{@expected_title}</title> hallo welt <asd><dasdd><a>dsd</a>"]) - @post = Factory.create(:status_message, :text => @url) - end - - it 'can be re-marshalled' do - lambda { - StatusMessage.find(@post.id).youtube_titles - }.should_not raise_error - end - it 'can be re-marshalled if it is serializaed incorrectly' do - StatusMessage.where(:id => @post.id).update_all(:youtube_titles => "this is not yaml") - lambda { - StatusMessage.find(@post.id).youtube_titles - }.should_not raise_error - end - end - - describe "YOUTUBE_ID_REGEX" do - specify "normal url" do - url = "http://www.youtube.com/watch?v=dQw4w9WgXcQ" - matched_data = url.match(YoutubeTitles::YOUTUBE_ID_REGEX) - matched_data[0].should == url - matched_data[1].should == "dQw4w9WgXcQ" - end - - specify "https url" do - url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" - matched_data = url.match(YoutubeTitles::YOUTUBE_ID_REGEX) - matched_data[0].should == url - matched_data[1].should == "dQw4w9WgXcQ" - end - - specify "url with extra query params" do - url = "http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=related" - matched_data = url.match(YoutubeTitles::YOUTUBE_ID_REGEX) - matched_data[0].should == url - matched_data[1].should == "dQw4w9WgXcQ" - end - end -end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index be158ca61519189ad6e6241d71fefde2979d69cd..778a5ca8c1956dc3ddb1d33514bfcf8aebb6c6c6 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -99,29 +99,6 @@ describe Comment do end end - # NOTE(move this to the youtube module spec) - describe 'youtube' do - before do - @message = alice.post :status_message, :text => "hi", :to => @alices_aspect.id - end - - it 'should process youtube titles on the way in' do - first_video_id = "ABYnqp-1111" - second_video_id = "ABYnqp-2222" - url = "http://www.youtube.com/watch?v=#{first_video_id} http://www.youtube.com/watch?v=#{second_video_id}" - expected_title = "UP & down & UP & down &" - - mock_http = mock("http") - Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).twice.and_return(mock_http) - mock_http.should_receive(:get).with(/\/feeds\/api\/videos/).twice.and_return( - [nil, 'Foobar <title>'+expected_title+'</title> hallo welt <asd><dasdd><a>dsd</a>']) - - comment = alice.build_comment :text => url, :post => @message - comment.save! - - Comment.find(comment.id).youtube_titles.should == { first_video_id => CGI::escape(expected_title), second_video_id => CGI::escape(expected_title) } - end - end describe 'it is relayable' do before do diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index 5cbb587d66d9fcb84e7be01874f1d1768ab513b6..9c3450249874f8b02a9de3ac7d6dcd3b4de36f61 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -275,23 +275,6 @@ STR end end - describe 'youtube' do - it 'should process youtube titles on the way in' do - video_id = "ABYnqp-bxvg" - url="http://www.youtube.com/watch?v=#{video_id}&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1" - expected_title = "UP & down & UP & down &" - - mock_http = mock("http") - Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http) - mock_http.should_receive(:get).with('/feeds/api/videos/'+video_id+'?v=2').and_return( - [nil, 'Foobar <title>'+expected_title+'</title> hallo welt <asd><dasdd><a>dsd</a>']) - - post = @user.build_post :status_message, :text => url, :to => @aspect.id - - post.save! - Post.find(post.id).youtube_titles.should == {video_id => CGI::escape(expected_title)} - end - end describe '#after_dispatch' do before do @photos = [alice.build_post(:photo, :pending => true, :user_file=> File.open(photo_fixture_name)),