From f676e284f7d6c8f382549d82e62625af03b9b1af Mon Sep 17 00:00:00 2001
From: archi <archi@loki.localnet>
Date: Sun, 17 Oct 2010 16:14:01 +0200
Subject: [PATCH] Added Youtube links - Still TODO: Caching of titles!

---
 app/helpers/status_messages_helper.rb | 27 +++++++++++++++++++++++++--
 public/javascripts/view.js            | 15 +++++++++------
 2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb
index f44ffe42e5..398b265fde 100644
--- a/app/helpers/status_messages_helper.rb
+++ b/app/helpers/status_messages_helper.rb
@@ -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
@@ -17,10 +19,31 @@ 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')
-    message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch.*v=([A-Za-z0-9_]+)[^ ]*/, '\1youtube::\2')
+    message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch.*v=([A-Za-z0-9_]+)[^ ]*/, '\1youtube.com::\2')
     message.gsub!(/(http|ftp):\/\/([^ ]+)/, '<a target="_blank" href="\1://\2">\2</a>')
-    message.gsub!(/youtube::([A-Za-z0-9_]+)/, '<a name="\1" onclick="openYoutube(\'\1\', this)" href="#\1">Youtube: \1</a>')
+    youtube = message.match(/youtube\.com::([A-Za-z0-9_]+)/)
+    youtube.to_a.each do |videoid|
+      if videoid.match('::').nil?
+        message.gsub!('youtube.com::'+videoid, '<a onclick="openVideo(\'youtube.com\', \'' + videoid + '\', this)" href="#video">Youtube: ' + youtube_title(videoid) + '</a>')
+      end
+    end
     return message
   end
 
+  def youtube_title(id)
+    #TODO Check if id is cached, and return cached value
+
+    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
+    
+    #TODO Cache the value of ret for id
+    return ret
+  end
+
 end
diff --git a/public/javascripts/view.js b/public/javascripts/view.js
index 3625432436..0df04a66e8 100644
--- a/public/javascripts/view.js
+++ b/public/javascripts/view.js
@@ -72,12 +72,15 @@ $.fn.clearForm = function() {
   });
 };
 
-function openYoutube(videoid, link) {
+function openVideo(type, videoid, link) {
   var container = document.createElement('div');
-  container.innerHTML = '<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><br><a href="#'+videoid+'" onclick="closeYoutube(this)">Close</a> <a href="http://www.youtube.com/watch?v='+videoid+'" target="_blank">Watch on Youtube</a>';
+  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+')';
+  }
+  $(container).hide();
   link.parentNode.insertBefore(container, this.nextSibling);
-}
-
-function closeYoutube(link) {
-  link.parentNode.parentNode.removeChild(link.parentNode);
+  $(container).slideDown('fast', function() { });
+  link.onclick = function() { $(container).slideToggle('fast', function() { } ); }
 }
-- 
GitLab