Merge remote branch 'archi/332-quickfix'
This commit is contained in:
commit
3436371a96
3 changed files with 78 additions and 4 deletions
|
|
@ -3,6 +3,8 @@
|
|||
# the COPYRIGHT file.
|
||||
|
||||
module StatusMessagesHelper
|
||||
@@youtube_title_cache = Hash.new("no-title")
|
||||
|
||||
def my_latest_message
|
||||
unless @latest_status_message.nil?
|
||||
return @latest_status_message.message
|
||||
|
|
@ -16,8 +18,33 @@ module StatusMessagesHelper
|
|||
|
||||
# next line is important due to XSS! (h is rail's make_html_safe-function)
|
||||
message = h(message).html_safe
|
||||
message.gsub!(/( |^)(www\.[^ ]+\.[^ ])/, '\1http://\2');
|
||||
return message.gsub(/(http|ftp):\/\/([^ ]+)/, '<a target="_blank" href="\1://\2">\2</a>');
|
||||
message.gsub!(/( |^)(www\.[^ ]+\.[^ ])/, '\1http://\2')
|
||||
message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/, '\1youtube.com::\2')
|
||||
message.gsub!(/(https|http|ftp):\/\/([^ ]+)/, '<a target="_blank" href="\1://\2">\2</a>')
|
||||
|
||||
while youtube = message.match(/youtube\.com::([A-Za-z0-9_]+)/)
|
||||
videoid = youtube[1]
|
||||
message.gsub!('youtube.com::'+videoid, '<a onclick="openVideo(\'youtube.com\', \'' + videoid + '\', this)" href="#video">Youtube: ' + youtube_title(videoid) + '</a>')
|
||||
end
|
||||
return message
|
||||
end
|
||||
|
||||
def youtube_title(id)
|
||||
unless @@youtube_title_cache[id] == 'no-title'
|
||||
return @@youtube_title_cache[id]
|
||||
end
|
||||
|
||||
ret = 'Unknown Video Title' #TODO add translation
|
||||
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
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -72,3 +72,21 @@ $.fn.clearForm = function() {
|
|||
});
|
||||
};
|
||||
|
||||
var video_active_container = null;
|
||||
|
||||
function openVideo(type, videoid, link) {
|
||||
var container = document.createElement('div');
|
||||
if(type == 'youtube.com') {
|
||||
container.innerHTML = '<a href="http://www.youtube.com/watch?v='+videoid+'" target="_blank">Watch this video on Youtube</a><br><object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/'+videoid+'?fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+videoid+'?fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';
|
||||
} else {
|
||||
container.innerHTML = 'Invalid videotype <i>'+type+'</i> (ID: '+videoid+')';
|
||||
}
|
||||
if(video_active_container != null) {
|
||||
video_active_container.parentNode.removeChild(video_active_container);
|
||||
}
|
||||
video_active_container = container;
|
||||
$(container).hide();
|
||||
link.parentNode.insertBefore(container, this.nextSibling);
|
||||
$(container).slideDown('fast', function() { });
|
||||
link.onclick = function() { $(container).slideToggle('fast', function() { } ); }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ describe StatusMessagesHelper do
|
|||
|
||||
it "should recognize basic http links (2/3)" do
|
||||
proto="http"
|
||||
url="www.youtube.com/watch?v=b15yaPYNDRU"
|
||||
url="webmail.example.com?~()!*/"
|
||||
make_links(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
||||
end
|
||||
|
||||
|
|
@ -26,7 +26,36 @@ describe StatusMessagesHelper do
|
|||
url="127.0.0.1:3000/users/sign_in"
|
||||
make_links(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
||||
end
|
||||
|
||||
|
||||
it "should recognize secure https links" do
|
||||
proto="https"
|
||||
url="127.0.0.1:3000/users/sign_in"
|
||||
make_links(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
||||
end
|
||||
|
||||
it "should recognize youtube links" do
|
||||
proto="http"
|
||||
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 = make_links(proto+'://'+url)
|
||||
res.should == "<a onclick=\"openVideo('youtube.com', '"+videoid+"', this)\" href=\"#video\">Youtube: "+title+"</a>"
|
||||
end
|
||||
|
||||
it "should recognize a bunch of different links" 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 = make_links(message)
|
||||
res.should == "http:// Hello World, this is for <a target=\"_blank\" href=\"http://www.joindiaspora.com\">www.joindiaspora.com</a> and not for <a target=\"_blank\" href=\"http://www.google.com\">www.google.com</a> though their Youtube service is neat, take <a onclick=\"openVideo('youtube.com', 'foobar', this)\" href=\"#video\">Youtube: F 007 - the bar is not enough</a> or <a onclick=\"openVideo('youtube.com', 'BARFOO', this)\" href=\"#video\">Youtube: BAR is the new FOO</a> It is a good idea we finally have youtube, so enjoy this video <a onclick=\"openVideo('youtube.com', 'rickrolld', this)\" href=\"#video\">Youtube: Never gonne give you up</a>"
|
||||
end
|
||||
|
||||
it "should recognize basic ftp links" do
|
||||
proto="ftp"
|
||||
url="ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4"
|
||||
|
|
|
|||
Loading…
Reference in a new issue