Skip to content
Extraits de code Groupes Projets
Valider d5d4a08f rédigé par Raphael's avatar Raphael
Parcourir les fichiers

Start moving youtube title getting into the post/comment flow

parent fef6d84f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -153,32 +153,19 @@ module ApplicationHelper ...@@ -153,32 +153,19 @@ module ApplicationHelper
if options[:youtube] if options[:youtube]
while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\\-]+)/) while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\\-]+)/)
videoid = youtube[1] video_id = 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>') 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
end end
return message return message
end 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) def info_text(text)
image_tag 'icons/monotone_question.png', :class => 'what_is_this', :title => text image_tag 'icons/monotone_question.png', :class => 'what_is_this', :title => text
end end
......
...@@ -34,6 +34,7 @@ class Comment ...@@ -34,6 +34,7 @@ class Comment
validates_presence_of :text, :diaspora_handle validates_presence_of :text, :diaspora_handle
validates_with HandleValidator validates_with HandleValidator
before_save :get_youtube_title
timestamps! timestamps!
...@@ -67,4 +68,23 @@ class Comment ...@@ -67,4 +68,23 @@ class Comment
verify_signature(creator_signature, person) verify_signature(creator_signature, person)
end 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 end
...@@ -7,6 +7,6 @@ ...@@ -7,6 +7,6 @@
.content .content
.from .from
= link_to person.real_name, person = link_to person.real_name, person
= markdownify(comment.text) = markdownify(comment.text, :youtube_maps => comment[:url_maps])
%div.time %div.time
= "#{time_ago_in_words(comment.updated_at)} #{t('ago')}" = "#{time_ago_in_words(comment.updated_at)} #{t('ago')}"
...@@ -20,6 +20,24 @@ describe StatusMessagesController do ...@@ -20,6 +20,24 @@ describe StatusMessagesController do
@controller.stub!(:current_user).and_return(user) @controller.stub!(:current_user).and_return(user)
end 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 describe '#create' do
let(:status_message_hash) { let(:status_message_hash) {
{:status_message =>{ {:status_message =>{
......
...@@ -77,9 +77,6 @@ describe ApplicationHelper do ...@@ -77,9 +77,6 @@ describe ApplicationHelper do
videoid = "0x__dDWdf23" videoid = "0x__dDWdf23"
url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1" url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
title = "UP & down & UP & down &amp;" title = "UP & down & UP & down &amp;"
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 = markdownify(proto+'://'+url)
res.should =~ /data-host="youtube.com"/ res.should =~ /data-host="youtube.com"/
res.should =~ /data-video-id="#{videoid}"/ res.should =~ /data-video-id="#{videoid}"/
...@@ -90,9 +87,6 @@ describe ApplicationHelper do ...@@ -90,9 +87,6 @@ describe ApplicationHelper do
videoid = "ABYnqp-bxvg" videoid = "ABYnqp-bxvg"
url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1" url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
title = "UP & down & UP & down &amp;" title = "UP & down & UP & down &amp;"
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 = markdownify(proto+'://'+url)
res.should =~ /data-host="youtube.com"/ res.should =~ /data-host="youtube.com"/
res.should =~ /data-video-id="#{videoid}"/ res.should =~ /data-video-id="#{videoid}"/
...@@ -100,11 +94,6 @@ describe ApplicationHelper do ...@@ -100,11 +94,6 @@ describe ApplicationHelper do
it "recognizes multiple links of different types" 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" 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 = markdownify(message)
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.joindiaspora.com\"/ res.should =~ /a target=\"_blank\" href=\"http:\/\/www.joindiaspora.com\"/
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.google.com\"/ res.should =~ /a target=\"_blank\" href=\"http:\/\/www.google.com\"/
......
...@@ -173,7 +173,26 @@ describe Comment do ...@@ -173,7 +173,26 @@ describe Comment do
comment.signature_valid?.should be true comment.signature_valid?.should be true
comment.verify_post_creator_signature.should be false comment.verify_post_creator_signature.should be false
end end
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 &amp;"
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 end
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter