Skip to content

Commit

Permalink
Update twitter node to new credential api
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed Jul 20, 2014
1 parent 4302deb commit 9d48185
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 49 deletions.
48 changes: 19 additions & 29 deletions nodes/core/social/27-twitter.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</script>

<script type="text/javascript">
(function() {
var twitterConfigNodeId = null;
var twitterConfigNodeIntervalId = null;

Expand All @@ -28,9 +29,9 @@
if (pathname.slice(-1) != "/") {
pathname += "/";
}
var callback = encodeURIComponent(location.protocol+"//"+location.hostname+":"+location.port+pathname+"twitter/"+twitterConfigNodeId+"/auth/callback");
var callback = encodeURIComponent(location.protocol+"//"+location.hostname+":"+location.port+pathname+"twitter-credentials/"+twitterConfigNodeId+"/auth/callback");

$("#node-config-twitter-row").html('Click <a id="node-config-twitter-start" href="twitter/'+twitterConfigNodeId+'/auth?callback='+callback+'" target="_blank"><b>here</b></a> to authenticate with Twitter.');
$("#node-config-twitter-row").html('Click <a id="node-config-twitter-start" href="twitter-credentials/'+twitterConfigNodeId+'/auth?callback='+callback+'" target="_blank"><b>here</b></a> to authenticate with Twitter.');
$("#node-config-twitter-start").click(function() {
twitterConfigNodeIntervalId = window.setTimeout(pollTwitterCredentials,2000);
});
Expand All @@ -40,9 +41,9 @@
$("#node-config-twitter-row").html('<label><i class="icon-user"></i> Twitter ID</label><span class="input-xlarge uneditable-input">'+sn+'</span>');
}
function pollTwitterCredentials(e) {
$.getJSON('twitter/'+twitterConfigNodeId,function(data) {
if (data.sn) {
updateTwitterScreenName(data.sn);
$.getJSON('credentials/twitter-credentials/'+twitterConfigNodeId,function(data) {
if (data.screen_name) {
updateTwitterScreenName(data.screen_name);
twitterConfigNodeIntervalId = null;
} else {
twitterConfigNodeIntervalId = window.setTimeout(pollTwitterCredentials,2000);
Expand All @@ -52,10 +53,14 @@
RED.nodes.registerType('twitter-credentials',{
category: 'config',
defaults: {
screen_name: {value:""},
access_token: {value: ""},
access_token_secret: {value:""}
screen_name: {value:""}
},
credentials: {
screen_name: {type:"text"},
access_token: {type: "password"},
access_token_secret: {type:"password"}
},

label: function() {
return this.screen_name;
},
Expand All @@ -65,13 +70,11 @@
if (!this.screen_name || this.screen_name == "") {
showTwitterAuthStart();
} else {
$.getJSON('twitter/'+twitterConfigNodeId,function(data) {
if (data.sn) {
updateTwitterScreenName(data.sn);
} else {
showTwitterAuthStart();
}
});
if (this.credentials.screen_name) {
updateTwitterScreenName(this.credentials.screen_name);
} else {
showTwitterAuthStart();
}
}
},
oneditsave: function() {
Expand All @@ -83,22 +86,9 @@
if (twitterConfigNodeIntervalId) {
window.clearTimeout(twitterConfigNodeIntervalId);
}
if (adding) {
$.ajax({
url: 'twitter/'+this.id,
type: 'DELETE',
success: function(result) {}
});
}
},
ondelete: function() {
$.ajax({
url: 'twitter/'+this.id,
type: 'DELETE',
success: function(result) {}
});
}
});
})();
</script>

<script type="text/x-red" data-template-name="twitter in">
Expand Down
28 changes: 9 additions & 19 deletions nodes/core/social/27-twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
this.screen_name = n.screen_name;
}
RED.nodes.registerType("twitter-credentials",TwitterNode);
RED.nodes.registerType("twitter-credentials",TwitterNode,{
credentials: {
screen_name: {type:"text"},
access_token: {type: "password"},
access_token_secret: {type:"password"}
}
});

function TwitterInNode(n) {
RED.nodes.createNode(this,n);
Expand Down Expand Up @@ -266,23 +272,7 @@ module.exports = function(RED) {
"HMAC-SHA1"
);

var credentials = {};

RED.httpAdmin.get('/twitter/:id', function(req,res) {
var credentials = RED.nodes.getCredentials(req.params.id);
if (credentials) {
res.send(JSON.stringify({sn:credentials.screen_name}));
} else {
res.send(JSON.stringify({}));
}
});

RED.httpAdmin.delete('/twitter/:id', function(req,res) {
RED.nodes.deleteCredentials(req.params.id);
res.send(200);
});

RED.httpAdmin.get('/twitter/:id/auth', function(req, res){
RED.httpAdmin.get('/twitter-credentials/:id/auth', function(req, res){
var credentials = {};
oa.getOAuthRequestToken({
oauth_callback: req.query.callback
Expand All @@ -302,7 +292,7 @@ module.exports = function(RED) {
});
});

RED.httpAdmin.get('/twitter/:id/auth/callback', function(req, res, next){
RED.httpAdmin.get('/twitter-credentials/:id/auth/callback', function(req, res, next){
var credentials = RED.nodes.getCredentials(req.params.id);
credentials.oauth_verifier = req.query.oauth_verifier;

Expand Down
2 changes: 1 addition & 1 deletion public/red/ui/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ RED.editor = function() {
function updateNodeCredentials(node, credDefinition, prefix) {
var changed = false;
if(!node.credentials) {
node.credentials = {};
node.credentials = {_:{}};
}

for (var cred in credDefinition) {
Expand Down

0 comments on commit 9d48185

Please sign in to comment.