Start moving youtube title getting into the post/comment flow
This commit is contained in:
parent
fef6d84f4b
commit
d5d4a08f03
6 changed files with 66 additions and 33 deletions
|
|
@ -153,32 +153,19 @@ module ApplicationHelper
|
|||
|
||||
if options[:youtube]
|
||||
while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\\-]+)/)
|
||||
videoid = youtube[1]
|
||||
message.gsub!('youtube.com::'+videoid, '<a class="video-link" data-host="youtube.com" data-video-id="' + videoid + '" href="#video">Youtube: ' + youtube_title(videoid) + '</a>')
|
||||
video_id = youtube[1]
|
||||
if options[:youtube_maps] && options[:youtube_maps][video_id]
|
||||
title = options[:youtube_maps][video_id]
|
||||
else
|
||||
title = I18n.t 'application.helper.youtube_title.unknown'
|
||||
end
|
||||
message.gsub!('youtube.com::'+video_id, '<a class="video-link" data-host="youtube.com" data-video-id="' + video_id + '" href="#video">Youtube: ' + title + '</a>')
|
||||
end
|
||||
end
|
||||
|
||||
return message
|
||||
end
|
||||
|
||||
def youtube_title(id)
|
||||
unless @@youtube_title_cache[id] == 'no-title'
|
||||
return @@youtube_title_cache[id]
|
||||
end
|
||||
|
||||
ret = I18n.t 'application.helper.youtube_title.unknown'
|
||||
http = Net::HTTP.new('gdata.youtube.com', 80)
|
||||
path = '/feeds/api/videos/'+id+'?v=2'
|
||||
resp, data = http.get(path, nil)
|
||||
title = data.match(/<title>(.*)<\/title>/)
|
||||
unless title.nil?
|
||||
ret = title.to_s[7..-9]
|
||||
end
|
||||
|
||||
@@youtube_title_cache[id] = ret;
|
||||
return ret
|
||||
end
|
||||
|
||||
def info_text(text)
|
||||
image_tag 'icons/monotone_question.png', :class => 'what_is_this', :title => text
|
||||
end
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ class Comment
|
|||
validates_presence_of :text, :diaspora_handle
|
||||
validates_with HandleValidator
|
||||
|
||||
before_save :get_youtube_title
|
||||
|
||||
timestamps!
|
||||
|
||||
|
|
@ -67,4 +68,23 @@ class Comment
|
|||
verify_signature(creator_signature, person)
|
||||
end
|
||||
|
||||
def get_youtube_title
|
||||
self[:url_maps] ||= {}
|
||||
youtube_match = text.match(/youtube\.com.*?v=([A-Za-z0-9_\\\-]+)/)
|
||||
return unless youtube_match
|
||||
video_id = youtube_match[1]
|
||||
unless self[:url_maps][video_id]
|
||||
ret = I18n.t 'application.helper.youtube_title.unknown'
|
||||
http = Net::HTTP.new('gdata.youtube.com', 80)
|
||||
path = "/feeds/api/videos/#{video_id}?v=2"
|
||||
resp, data = http.get(path, nil)
|
||||
title = data.match(/<title>(.*)<\/title>/)
|
||||
unless title.nil?
|
||||
title = title.to_s[7..-9]
|
||||
end
|
||||
self[:url_maps][video_id] = title
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -7,6 +7,6 @@
|
|||
.content
|
||||
.from
|
||||
= link_to person.real_name, person
|
||||
= markdownify(comment.text)
|
||||
= markdownify(comment.text, :youtube_maps => comment[:url_maps])
|
||||
%div.time
|
||||
= "#{time_ago_in_words(comment.updated_at)} #{t('ago')}"
|
||||
|
|
|
|||
|
|
@ -20,6 +20,24 @@ describe StatusMessagesController do
|
|||
@controller.stub!(:current_user).and_return(user)
|
||||
end
|
||||
|
||||
describe '#show' do
|
||||
before do
|
||||
@video_id = "ABYnqp-bxvg"
|
||||
@url="http://www.youtube.com/watch?v=#{@video_id}&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
||||
@message = user.post :status_message, :message => @url, :to => aspect.id
|
||||
end
|
||||
it 'renders posts with youtube urls' do
|
||||
get :show, :id => @message.id
|
||||
end
|
||||
it 'renders posts with comments with youtube urls' do
|
||||
@comment = user.comment "none", :on => @message
|
||||
@comment.text = @url
|
||||
@comment[:url_maps][@video_id] = "title"
|
||||
@comment.save!
|
||||
get :show, :id => @message.id
|
||||
response.body.should match /Youtube: title/
|
||||
end
|
||||
end
|
||||
describe '#create' do
|
||||
let(:status_message_hash) {
|
||||
{:status_message =>{
|
||||
|
|
|
|||
|
|
@ -77,9 +77,6 @@ describe ApplicationHelper do
|
|||
videoid = "0x__dDWdf23"
|
||||
url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
||||
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/'+videoid+'?v=2', nil).and_return([nil, 'Foobar <title>'+title+'</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
res = markdownify(proto+'://'+url)
|
||||
res.should =~ /data-host="youtube.com"/
|
||||
res.should =~ /data-video-id="#{videoid}"/
|
||||
|
|
@ -90,9 +87,6 @@ describe ApplicationHelper do
|
|||
videoid = "ABYnqp-bxvg"
|
||||
url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
||||
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/'+videoid+'?v=2', nil).and_return([nil, 'Foobar <title>'+title+'</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
res = markdownify(proto+'://'+url)
|
||||
res.should =~ /data-host="youtube.com"/
|
||||
res.should =~ /data-video-id="#{videoid}"/
|
||||
|
|
@ -100,11 +94,6 @@ describe ApplicationHelper do
|
|||
|
||||
it "recognizes multiple links of different types" do
|
||||
message = "http:// Hello World, this is for www.joindiaspora.com and not for http://www.google.com though their Youtube service is neat, take http://www.youtube.com/watch?v=foobar or www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related It is a good idea we finally have youtube, so enjoy this video http://www.youtube.com/watch?v=rickrolld"
|
||||
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/foobar?v=2', nil).and_return([nil, 'Foobar <title>F 007 - the bar is not enough</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
mock_http.should_receive(:get).with('/feeds/api/videos/BARFOO?v=2', nil).and_return([nil, 'Foobar <title>BAR is the new FOO</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
mock_http.should_receive(:get).with('/feeds/api/videos/rickrolld?v=2', nil).and_return([nil, 'Foobar <title>Never gonne give you up</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
res = markdownify(message)
|
||||
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.joindiaspora.com\"/
|
||||
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.google.com\"/
|
||||
|
|
|
|||
|
|
@ -173,7 +173,26 @@ describe Comment do
|
|||
comment.signature_valid?.should be true
|
||||
comment.verify_post_creator_signature.should be false
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe 'youtube' do
|
||||
before do
|
||||
@message = user.post :status_message, :message => "hi", :to => aspect.id
|
||||
end
|
||||
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', nil).and_return(
|
||||
[nil, 'Foobar <title>'+expected_title+'</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
|
||||
comment = user.build_comment url, :on => @message
|
||||
|
||||
comment.save!
|
||||
comment[:url_maps].should == {video_id => expected_title}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue