From fb1fc8a046c5697d262d59c1ead9231068e515c5 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 6 Dec 2010 13:37:57 -0800 Subject: [PATCH 1/3] Use short API-style error responses for LinkPreview's oembed proxy --- plugins/LinkPreview/oembedproxyaction.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/LinkPreview/oembedproxyaction.php b/plugins/LinkPreview/oembedproxyaction.php index 470f780731..bc80ee5cf9 100644 --- a/plugins/LinkPreview/oembedproxyaction.php +++ b/plugins/LinkPreview/oembedproxyaction.php @@ -50,6 +50,9 @@ class OembedproxyAction extends OembedAction function handle($args) { + // Trigger short error responses; not a human-readable web page. + StatusNet::setApi(true); + // We're not a general oEmbed proxy service; limit to valid sessions. $token = $this->trimmed('token'); if (!$token || $token != common_session_token()) { From bb50e773e62dd5ebfdee8e45ad0adb14a1e0000a Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 6 Dec 2010 14:49:24 -0800 Subject: [PATCH 2/3] Ticket #2921: cleanup on LinkPreview to make fewer requests while typing. Will now wait for each link's preview request to complete (successfully or unsuccessfully) before re-running it) --- plugins/LinkPreview/linkpreview.js | 95 +++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 21 deletions(-) diff --git a/plugins/LinkPreview/linkpreview.js b/plugins/LinkPreview/linkpreview.js index 0c0eb734ec..407934c5ae 100644 --- a/plugins/LinkPreview/linkpreview.js +++ b/plugins/LinkPreview/linkpreview.js @@ -3,6 +3,9 @@ */ (function() { + /** + * Quickie wrapper around ooembed JSON lookup + */ var oEmbed = { api: 'http://oohembed.com/oohembed', width: 100, @@ -57,14 +60,24 @@ maxheight: oEmbed.height, token: $('#token').val() }; - $.get(oEmbed.api, params, function(data, xhr) { - callback(data); - }, 'json'); + $.ajax({ + url: oEmbed.api, + data: params, + dataType: 'json', + success: function(data, xhr) { + callback(data); + }, + error: function(xhr, textStatus, errorThrown) { + callback(null); + } + }); } }; var LinkPreview = { links: [], + state: [], + refresh: [], /** * Find URL links from the source text that may be interesting. @@ -88,22 +101,26 @@ * Start looking up info for a link preview... * May start async data loads. * - * @param {String} id - * @param {String} url + * @param {number} col: column number to insert preview into */ - prepLinkPreview: function(id, url) + prepLinkPreview: function(col) { + var id = 'link-preview-' + col; + var url = LinkPreview.links[col]; + LinkPreview.refresh[col] = false; + LinkPreview.markLoading(col); + oEmbed.lookup(url, function(data) { var thumb = null; var width = 100; - if (typeof data.thumbnail_url == "string") { + if (data && typeof data.thumbnail_url == "string") { thumb = data.thumbnail_url; if (typeof data.thumbnail_width !== "undefined") { if (data.thumbnail_width < width) { width = data.thumbnail_width; } } - } else if (data.type == 'photo' && typeof data.url == "string") { + } else if (data && data.type == 'photo' && typeof data.url == "string") { thumb = data.url; if (typeof data.width !== "undefined") { if (data.width < width) { @@ -111,6 +128,7 @@ } } } + if (thumb) { var link = $(''); link.find('a') @@ -121,7 +139,19 @@ .attr('src', thumb) .attr('width', width) .attr('title', data.title || data.url || url); + $('#' + id).empty(); $('#' + id).append(link); + } else { + // No thumbnail available or error retriving it. + LinkPreview.clearLink(col); + } + + if (LinkPreview.refresh[col]) { + // Darn user has typed more characters. + // Go fetch another link! + LinkPreview.prepLinkPreview(col); + } else { + LinkPreview.markDone(col); } }); }, @@ -134,34 +164,57 @@ */ previewLinks: function(text) { + var i; var old = LinkPreview.links; var links = LinkPreview.findLinks(text); + LinkPreview.links = links; // Check for existing common elements... - for (var i = 0; i < old.length && i < links.length; i++) { + for (i = 0; i < old.length && i < links.length; i++) { if (links[i] != old[i]) { - // Change an existing entry! - var id = 'link-preview-' + i; - $('#' + id).html(''); - LinkPreview.prepLinkPreview(id, links[i]); + if (LinkPreview.state[i] == "loading") { + // Slate this column for a refresh when this one's done. + LinkPreview.refresh[i] = true; + } else { + // Change an existing entry! + LinkPreview.prepLinkPreview(i); + } } } if (links.length > old.length) { // Adding new entries, whee! - for (var i = old.length; i < links.length; i++) { - var id = 'link-preview-' + i; - $('#link-preview').append(''); - LinkPreview.prepLinkPreview(id, links[i]); + for (i = old.length; i < links.length; i++) { + LinkPreview.addPreviewArea(i); + LinkPreview.prepLinkPreview(i); } } else if (old.length > links.length) { // Remove preview entries for links that have been removed. - for (var i = links.length; i < old.length; i++) { - var id = 'link-preview-' + i; - $('#' + id).remove(); + for (i = links.length; i < old.length; i++) { + LinkPreview.clearLink(i); } } + }, - LinkPreview.links = links; + addPreviewArea: function(col) { + var id = 'link-preview-' + col; + $('#link-preview').append(''); + }, + + clearLink: function(col) { + var id = 'link-preview-' + col; + $('#' + id).html(''); + }, + + markLoading: function(col) { + LinkPreview.state[col] = "loading"; + var id = 'link-preview-' + col; + $('#' + id).attr('style', 'opacity: 0.5'); + }, + + markDone: function(col) { + LinkPreview.state[col] = "done"; + var id = 'link-preview-' + col; + $('#' + id).removeAttr('style'); }, /** From c40fde900a6b966752fb56eb8b23cf4c983fe349 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 6 Dec 2010 15:11:42 -0800 Subject: [PATCH 3/3] minify LinkPreview JS code --- plugins/LinkPreview/LinkPreviewPlugin.php | 2 +- plugins/LinkPreview/linkpreview.min.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 plugins/LinkPreview/linkpreview.min.js diff --git a/plugins/LinkPreview/LinkPreviewPlugin.php b/plugins/LinkPreview/LinkPreviewPlugin.php index 39d2c9bf39..65f896ca27 100644 --- a/plugins/LinkPreview/LinkPreviewPlugin.php +++ b/plugins/LinkPreview/LinkPreviewPlugin.php @@ -51,7 +51,7 @@ class LinkPreviewPlugin extends Plugin { $user = common_current_user(); if ($user && common_config('attachments', 'process_links')) { - $action->script('plugins/LinkPreview/linkpreview.js'); + $action->script('plugins/LinkPreview/linkpreview.min.js'); $data = json_encode(array( 'api' => common_local_url('oembedproxy'), 'width' => common_config('attachments', 'thumbwidth'), diff --git a/plugins/LinkPreview/linkpreview.min.js b/plugins/LinkPreview/linkpreview.min.js new file mode 100644 index 0000000000..a6fb9dba83 --- /dev/null +++ b/plugins/LinkPreview/linkpreview.min.js @@ -0,0 +1 @@ +(function(){var a={api:"http://oohembed.com/oohembed",width:100,height:75,cache:{},callbacks:{},lookup:function(c,d){if(typeof a.cache[c]=="object"){d(a.cache[c])}else{if(typeof a.callbacks[c]=="undefined"){a.callbacks[c]=[d];a.rawLookup(c,function(g){a.cache[c]=g;var f=a.callbacks[c];a.callbacks[c]=undefined;for(var e=0;e');h.find("a").attr("href",c).attr("target","_blank").last().find("img").attr("src",f).attr("width",g).attr("title",i.title||i.url||c);$("#"+e).empty();$("#"+e).append(h)}else{b.clearLink(d)}if(b.refresh[d]){b.prepLinkPreview(d)}else{b.markDone(d)}})},previewLinks:function(f){var e;var c=b.links;var d=b.findLinks(f);b.links=d;for(e=0;ec.length){for(e=c.length;ed.length){for(e=d.length;e')},clearLink:function(c){var d="link-preview-"+c;$("#"+d).html("")},markLoading:function(c){b.state[c]="loading";var d="link-preview-"+c;$("#"+d).attr("style","opacity: 0.5")},markDone:function(c){b.state[c]="done";var d="link-preview-"+c;$("#"+d).removeAttr("style")},clear:function(){b.links=[];$("#link-preview").empty()}};SN.Init.LinkPreview=function(c){if(c.api){a.api=c.api}if(c.width){a.width=c.width}if(c.height){a.height=c.height}$("#form_notice").append('').bind("reset",function(){b.clear()});var d=SN.U.Counter;SN.U.Counter=function(e){b.previewLinks($("#notice_data-text").val());return d(e)}}})(); \ No newline at end of file