From 01f7506a8afc3226a6dad7fdc37fe27fabef7313 Mon Sep 17 00:00:00 2001 From: Sacha Greif Date: Thu, 4 Jul 2013 11:16:11 +0900 Subject: [PATCH 1/2] working on bitly link shortening integration --- client/css/screen.css | 4 ++-- client/models/setting.js | 2 ++ client/sass/modules/_forms.scss | 2 +- client/views/posts/post_submit.html | 8 +++++++- client/views/posts/post_submit.js | 25 +++++++++++++++++++++++++ client/views/posts/posts_list.js | 1 - collections/posts.js | 20 ++++++++++++++++++++ lib/helpers.js | 14 ++++++++++++++ 8 files changed, 71 insertions(+), 5 deletions(-) diff --git a/client/css/screen.css b/client/css/screen.css index 56e44e7d0..3ff788720 100644 --- a/client/css/screen.css +++ b/client/css/screen.css @@ -1420,13 +1420,13 @@ form .control-group, .accounts-dialog .control-group { margin-left: 150px; position: relative; } /* line 19, ../sass/modules/_forms.scss */ - form .control-group .controls .get-title-link, .accounts-dialog .control-group .controls .get-title-link { + form .control-group .controls .inline-link, .accounts-dialog .control-group .controls .inline-link { position: absolute; display: block; top: 2px; right: 8px; } /* line 24, ../sass/modules/_forms.scss */ - form .control-group .controls .get-title-link.loading, .accounts-dialog .control-group .controls .get-title-link.loading { + form .control-group .controls .inline-link.loading, .accounts-dialog .control-group .controls .inline-link.loading { background: url(/img/loading.gif) center center no-repeat; height: 22px; width: 18px; diff --git a/client/models/setting.js b/client/models/setting.js index 1ba1a435d..dfc41ce80 100644 --- a/client/models/setting.js +++ b/client/models/setting.js @@ -25,6 +25,7 @@ Setting = FormModel.extend({ clickyId:'', goSquaredId: '', embedlyId: '', + bitlyToken: '', footerCode: '', extraCode: '', notes: '' @@ -40,6 +41,7 @@ Setting = FormModel.extend({ this.overwriteTitle('mixpanelId', 'Mixpanel ID'); this.overwriteTitle('clickyId', 'Clicky ID'); this.overwriteTitle('goSquaredId', 'GoSquared ID'); + this.overwriteTitle('bitlyToken', 'Bitly Token'); this.overwriteTitle('logoUrl', 'Logo URL'); this.overwriteType('footerCode', 'textarea'); this.overwriteType('extraCode', 'textarea'); diff --git a/client/sass/modules/_forms.scss b/client/sass/modules/_forms.scss index 698d515d1..a7e51c88d 100644 --- a/client/sass/modules/_forms.scss +++ b/client/sass/modules/_forms.scss @@ -16,7 +16,7 @@ form, .accounts-dialog{ .controls{ margin-left:150px; position:relative; - .get-title-link{ + .inline-link{ position:absolute; display:block; top:2px; diff --git a/client/views/posts/post_submit.html b/client/views/posts/post_submit.html index 728991b6b..28a6e6f2f 100644 --- a/client/views/posts/post_submit.html +++ b/client/views/posts/post_submit.html @@ -6,9 +6,15 @@
+ {{#if shorten}} +
+ + +
+ {{/if}}
- +
diff --git a/client/views/posts/post_submit.js b/client/views/posts/post_submit.js index ecf27cd6a..887d8043c 100644 --- a/client/views/posts/post_submit.js +++ b/client/views/posts/post_submit.js @@ -11,6 +11,9 @@ Template.post_submit.helpers({ isSelected: function(){ var post=Posts.findOne(Session.get('selectedPostId')); return post && this._id == post.userId; + }, + shorten: function(){ + return !!getSetting('bitlyToken'); } }); @@ -36,6 +39,7 @@ Template.post_submit.events = { var title= $('#title').val(); var url = $('#url').val(); + var shortUrl = $('#short-url').val(); var body = instance.editor.exportFile(); var categories=[]; var sticky=!!$('#sticky').attr('checked'); @@ -50,6 +54,7 @@ Template.post_submit.events = { var properties = { headline: title , body: body + , shortUrl: shortUrl , categories: categories , sticky: sticky , submitted: submitted @@ -95,4 +100,24 @@ Template.post_submit.events = { $(".get-title-link").removeClass("loading"); } } + + ,'click .get-short-url': function(e){ + e.preventDefault(); + var url=$("#url").val(); + $(".get-short-url").addClass("loading"); + if(url){ + console.log(url) + getShortUrl(url, function(url){ + if (url){ + $("#short-url").val(url); + }else{ + alert("Sorry, couldn't find a short URL..."); + } + $(".get-short-url").removeClass("loading"); + }); + }else{ + alert("Please fill in an URL first!"); + $(".get-short-url").removeClass("loading"); + } + } }; diff --git a/client/views/posts/posts_list.js b/client/views/posts/posts_list.js index 2b2bb163f..1e5727bc9 100644 --- a/client/views/posts/posts_list.js +++ b/client/views/posts/posts_list.js @@ -16,7 +16,6 @@ Template.posts_list.helpers({ return this.fetch(); }, postsReady: function() { - console.log('checking postsReady', this.ready(), this); return this.ready(); }, allPostsLoaded: function(){ diff --git a/collections/posts.js b/collections/posts.js index 35983df62..c02a73772 100644 --- a/collections/posts.js +++ b/collections/posts.js @@ -44,6 +44,26 @@ Meteor.methods({ throw new Meteor.Error(605, 'Sorry, you cannot submit more than '+maxPostsPer24Hours+' posts per day'); } + // shorten URL + if(token=getSetting('bitlyToken')){ + Meteor.http.get( + "https://api-ssl.bitly.com/v3/shorten?callback=?", + { + data:{ + "format": "json", + "access_token": token, + "longUrl": post.url + } + }, + function(response){ + if(response.status_code == 200){ + console.log(response) + // post.shortUrl = response.data.url + } + } + ); + } + post = _.extend(post, { headline: headline, body: body, diff --git a/lib/helpers.js b/lib/helpers.js index 58f3ccd62..abc02adc2 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -78,6 +78,20 @@ slugify = function(text) { text = text.toLowerCase(); return text; } +getShortUrl = function(url, func){ + $.getJSON( + "https://api-ssl.bitly.com/v3/shorten?callback=?", + { + "format": "json", + "access_token": getSetting('bitlyToken'), + "longUrl": url + }, + function(response){ + func(response.data.url); + } + ); +} + // ---------------------------------- String Helper Functions ----------------------------------- // cleanUp = function(s){ return stripHTML(s); From bb61e843c9743dfc6e6941e5f9aeff67d5618fd6 Mon Sep 17 00:00:00 2001 From: Sacha Greif Date: Thu, 4 Jul 2013 12:05:09 +0900 Subject: [PATCH 2/2] moved url shortening server-side --- client/views/posts/post_edit.html | 6 ++++++ client/views/posts/post_edit.js | 7 ++++++- client/views/posts/post_submit.html | 6 ------ client/views/posts/post_submit.js | 22 ---------------------- collections/posts.js | 17 +++++++---------- 5 files changed, 19 insertions(+), 39 deletions(-) diff --git a/client/views/posts/post_edit.html b/client/views/posts/post_edit.html index c2ce18acc..526f244c5 100644 --- a/client/views/posts/post_edit.html +++ b/client/views/posts/post_edit.html @@ -14,6 +14,12 @@
+ {{#if shorten}} +
+ +
+
+ {{/if}}
diff --git a/client/views/posts/post_edit.js b/client/views/posts/post_edit.js index cf8997b39..dd7869066 100644 --- a/client/views/posts/post_edit.js +++ b/client/views/posts/post_edit.js @@ -51,7 +51,10 @@ Template.post_edit.helpers({ }, hasStatusRejected: function(){ return this.status == STATUS_REJECTED ? 'checked' : ''; - }, + }, + shorten: function(){ + return !!getSetting('bitlyToken'); + } }); Template.post_edit.rendered = function(){ @@ -78,6 +81,7 @@ Template.post_edit.events = { var post = Posts.findOne(selectedPostId); var categories = []; var url = $('#url').val(); + var shortUrl = $('#short-url').val(); var status = parseInt($('input[name=status]:checked').val()); $('input[name=category]:checked').each(function() { @@ -88,6 +92,7 @@ Template.post_edit.events = { var properties = { headline: $('#title').val(), + shortUrl: shortUrl, body: instance.editor.exportFile(), categories: categories, }; diff --git a/client/views/posts/post_submit.html b/client/views/posts/post_submit.html index 28a6e6f2f..f1f16891e 100644 --- a/client/views/posts/post_submit.html +++ b/client/views/posts/post_submit.html @@ -6,12 +6,6 @@
- {{#if shorten}} -
- - -
- {{/if}}
diff --git a/client/views/posts/post_submit.js b/client/views/posts/post_submit.js index 887d8043c..46bb85ab3 100644 --- a/client/views/posts/post_submit.js +++ b/client/views/posts/post_submit.js @@ -11,9 +11,6 @@ Template.post_submit.helpers({ isSelected: function(){ var post=Posts.findOne(Session.get('selectedPostId')); return post && this._id == post.userId; - }, - shorten: function(){ - return !!getSetting('bitlyToken'); } }); @@ -101,23 +98,4 @@ Template.post_submit.events = { } } - ,'click .get-short-url': function(e){ - e.preventDefault(); - var url=$("#url").val(); - $(".get-short-url").addClass("loading"); - if(url){ - console.log(url) - getShortUrl(url, function(url){ - if (url){ - $("#short-url").val(url); - }else{ - alert("Sorry, couldn't find a short URL..."); - } - $(".get-short-url").removeClass("loading"); - }); - }else{ - alert("Please fill in an URL first!"); - $(".get-short-url").removeClass("loading"); - } - } }; diff --git a/collections/posts.js b/collections/posts.js index c02a73772..8c8e84bf3 100644 --- a/collections/posts.js +++ b/collections/posts.js @@ -45,23 +45,20 @@ Meteor.methods({ } // shorten URL - if(token=getSetting('bitlyToken')){ - Meteor.http.get( - "https://api-ssl.bitly.com/v3/shorten?callback=?", + if(!this.isSimulation && (token=getSetting('bitlyToken'))){ + var shortenResponse = Meteor.http.get( + "https://api-ssl.bitly.com/v3/shorten?", { - data:{ + timeout: 5000, + params:{ "format": "json", "access_token": token, "longUrl": post.url } - }, - function(response){ - if(response.status_code == 200){ - console.log(response) - // post.shortUrl = response.data.url - } } ); + if(shortenResponse.statusCode == 200) + post.shortUrl = shortenResponse.data.data.url } post = _.extend(post, {