Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions foursquare/foursquare.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<!--
Copyright 2014 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<script type="text/x-red" data-template-name="foursquare-credentials">
<div id="node-config-foursquare-app-keys">
<div class="form-row">
<p style="margin-top: 10px;"><b>1.</b> Create your own app at <a href="https://foursquare.com/developers/apps" target="_blank" style="text-decoration:underline;">foursquare.com</a></p>
</div>
<div class="form-tips" id="node-config-foursquare-tooltip">
</div>
<div class="form-row">
<p style="margin-top: 10px;"><b>2.</b> Copy the app details here:</p>
</div>
<div class="form-row">
<label style="margin-left: 10px; margin-right: -10px;" for="node-config-input-client_id"><i class="fa fa-user"></i> Client ID</label>
<input type="password" id="node-config-input-client_id">
</div>
<div class="form-row">
<label style="margin-left: 10px; margin-right: -10px;" for="node-config-input-client_secret"><i class="fa fa-key"></i> Client Secret</label>
<input type="password" id="node-config-input-client_secret">
</div>
<div class="form-row">
<label>&nbsp;</label>
<a class="btn" id="node-config-start-auth" href="#" target="_blank">Authenticate with Foursquare</a>
</div>
</div>
<div id="node-config-foursquare-user">
<div class="form-row">
<label><i class="fa fa-user"></i> Foursquare User</label><span id="node-config-foursquare-displayname" class="input-xlarge uneditable-input"></span>
</div>
<input type="hidden" id="node-config-input-displayname">
</div>
</script>

<script type="text/javascript">
(function() {
RED.nodes.registerType('foursquare-credentials',{
category: 'config',
defaults: {
displayname: {value:""}
},
credentials: {
displayname: {type:"text", required: true},
clientid: {type:"password",required:true},
clientsecret: {type: "password",required:true},
accesstoken: {type: "password", required:true}
},
label: function() {
return this.displayname;
},
exportable: false,
oneditprepare: function() {
var id = this.id;

var pathname = document.location.pathname;
if (pathname.slice(-1) != "/") {
pathname += "/";
}
var callback = location.protocol + "//" +
location.hostname + ":" + location.port +
pathname + "foursquare-credentials";
$("#node-config-foursquare-tooltip").html("<p>Please configure the authorized <b>Redirect URIs</b> of your app to include the following url:</p>\n<code>"+callback+"</code>");

function updateFoursquareAuthButton() {
var v1 = $("#node-config-input-client_id").val();
var v2 = $("#node-config-input-client_secret").val();
$("#node-config-start-auth").toggleClass("disabled",(v1.length === 0 || v2.length === 0));
}
$("#node-config-input-client_id").on('change keydown paste input',updateFoursquareAuthButton);
$("#node-config-input-client_secret").on('change keydown paste input',updateFoursquareAuthButton);

function updateFoursquareScreenName(sn) {
$("#node-config-foursquare-app-keys").hide();
$("#node-config-foursquare-user").show();
$("#node-config-input-displayname").val(sn);
$("#node-config-foursquare-displayname").html(sn);
}

function pollFoursquareCredentials(e) {
$.getJSON('credentials/foursquare-credentials/'+id,function(data) {
if (data.displayname) {
$("#node-config-dialog-ok").button("enable");
updateFoursquareScreenName(data.displayname);
delete window.foursquareConfigNodeIntervalId;
} else {
window.foursquareConfigNodeIntervalId = window.setTimeout(pollFoursquareCredentials,2000);
}
});
}

updateFoursquareAuthButton();

if (this.displayname) {
updateFoursquareScreenName(this.displayname);
} else {
$("#node-config-foursquare-app-keys").show();
$("#node-config-foursquare-user").hide();
$("#node-config-dialog-ok").button("disable");
}

$("#node-config-start-auth").mousedown(function(e) {
var client_id = $("#node-config-input-client_id").val();
var client_secret = $("#node-config-input-client_secret").val();
var pathname = document.location.pathname;
if (pathname.slice(-1) != "/") {
pathname += "/";
}
var callback = encodeURIComponent(location.protocol+"//"+location.hostname+":"+location.port+pathname+"foursquare-credentials/auth/callback");
var url = 'foursquare-credentials/auth?id='+id+'&callback='+callback+'&clientid='+client_id+"&clientsecret="+client_secret;
$(this).attr("href",url);
window.foursquareConfigNodeIntervalId = window.setTimeout(pollFoursquareCredentials,2000);
});
$("#node-config-start-auth").click(function(e) {
var key = $("#node-config-input-client_id").val();
var secret = $("#node-config-input-client_secret").val();
if (key === "" || secret === "") {
e.preventDefault();
}
});

},

oneditsave: function() {
if (window.foursquareConfigNodeIntervalId) {
window.clearTimeout(window.foursquareConfigNodeIntervalId);
delete window.foursquareConfigNodeIntervalId;
}
},
oneditcancel: function(adding) {
if (window.foursquareConfigNodeIntervalId) {
window.clearTimeout(window.foursquareConfigNodeIntervalId);
delete window.foursquareConfigNodeIntervalId;
}
}

});
})();
</script>




114 changes: 4 additions & 110 deletions swarm/swarm.js → foursquare/foursquare.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,126 +32,20 @@ module.exports = function(RED) {
);
}


function SwarmNode(n) {
function FoursquareNode(n) {
RED.nodes.createNode(this,n);
}

RED.nodes.registerType("swarm-credentials", SwarmNode, {
RED.nodes.registerType("foursquare-credentials", FoursquareNode, {
credentials: {
displayname: {type: "text"},
clientid: {type: "password"},
clientsecret: {type: "password"},
accesstoken: {type: "password"}
}
});

/**
* Swarm input node - will return the most recent check-in since
* the node has been initialized. The node will only populate msg.payload
* with the JSON of the check-in if a new check-in has been made within
* the polling interval.
*/
function SwarmInNode(n) {
RED.nodes.createNode(this, n);
var credentials = RED.nodes.getCredentials(n.swarm);
var node = this;
var credentialsOk = checkCredentials(node, credentials);
if (credentialsOk) {
var repeat = 900000; // 15 mins
var now = Math.floor(((new Date()).getTime())/1000); // time now in seconds since epoch
var afterTimestamp = now;
var lastcheckin = null;

var interval = setInterval(function() {
node.emit("input", {});
}, repeat );

this.on("input", function(msg) {
getCheckinsAfterTimestamp(node, "self", afterTimestamp, credentials, msg, function(msg) {
var latestcheckin = JSON.stringify(msg);
if (latestcheckin != lastcheckin) {
lastcheckin = latestcheckin;
afterTimestamp = msg.payload.createdAt;
node.send(msg);
}
});
});

this.on("close", function() {
if (interval != null) {
clearInterval(interval);
}
});

}
}

RED.nodes.registerType("swarm in", SwarmInNode);

/**
* Swarm query node - will return the most recent check-in since
* the node has been initialized. If a check-in exists the node will always return
* the most recent even if no new check-ins have happened since the previous query.
* The node only populates msg.payload when a check-in is found.
*/
function SwarmQueryNode(n) {
RED.nodes.createNode(this, n);
var node = this;
this.request = n.request || "get-most-recent-checkin";
var credentials = RED.nodes.getCredentials(n.swarm);
var credentialsOk = checkCredentials(node, credentials);
if (credentialsOk) {
var now = Math.floor(((new Date()).getTime())/1000); // time now in seconds since epoch (rounded down)
var afterTimestamp = now;

this.on("input", function(msg) {
if (node.request === "get-most-recent-checkin") {
getCheckinsAfterTimestamp(node, "self", afterTimestamp, credentials, msg, function(msg) {
afterTimestamp = msg.payload.createdAt - 2;
node.send(msg);
});
}
});
}
}

RED.nodes.registerType("swarm", SwarmQueryNode);

function checkCredentials(node, credentials) {
if (credentials && credentials.clientid && credentials.clientsecret && credentials.accesstoken) {
return true;
} else {
node.error("problem with credentials being set: " + credentials + ", ");
node.status({fill:"red",shape:"ring",text:"failed"});
return false;
}
}

function getCheckinsAfterTimestamp(node, userid, afterTimestamp, credentials, msg, callback) {
var apiUrl = "https://api.foursquare.com/v2/users/" + userid + "/checkins?oauth_token=" + credentials.accesstoken + "&v=20141016&afterTimestamp=" + afterTimestamp+"&sort=newestfirst";
request.get(apiUrl,function(err, httpResponse, body) {
if (err) {
node.error(err.toString());
node.status({fill:"red",shape:"ring",text:"failed"});
} else {
var result = JSON.parse(body);
if (result.meta.code != 200) {
node.error("Error code: " + result.meta.code + ", errorDetail: " + result.meta.errorDetail);
node.status({fill:"red",shape:"ring",text:"failed"});
} else {
if (result.response.checkins.items.length !== 0) {
var latest = result.response.checkins.items[0];
msg.payload = {};
msg.payload = latest;
callback(msg);
}
}
}
});
}

RED.httpAdmin.get('/swarm-credentials/auth', function(req, res){
RED.httpAdmin.get('/foursquare-credentials/auth', function(req, res){
if (!req.query.clientid || !req.query.clientsecret || !req.query.id || !req.query.callback) {
return res.status(400).send('ERROR: request does not contain the required parameters');
}
Expand All @@ -175,7 +69,7 @@ module.exports = function(RED) {
res.redirect(url);
});

RED.httpAdmin.get('/swarm-credentials/auth/callback', function(req, res){
RED.httpAdmin.get('/foursquare-credentials/auth/callback', function(req, res){
if (req.query.error) {
return res.send("ERROR: " + req.query.error + ": " + req.query.error_description);
}
Expand Down
File renamed without changes
Loading