@@ -32,126 +32,20 @@ module.exports = function(RED) {
3232 ) ;
3333 }
3434
35-
36- function SwarmNode ( n ) {
35+ function FoursquareNode ( n ) {
3736 RED . nodes . createNode ( this , n ) ;
3837 }
3938
40- RED . nodes . registerType ( "swarm -credentials" , SwarmNode , {
39+ RED . nodes . registerType ( "foursquare -credentials" , FoursquareNode , {
4140 credentials : {
4241 displayname : { type : "text" } ,
4342 clientid : { type : "password" } ,
4443 clientsecret : { type : "password" } ,
4544 accesstoken : { type : "password" }
4645 }
4746 } ) ;
48-
49- /**
50- * Swarm input node - will return the most recent check-in since
51- * the node has been initialized. The node will only populate msg.payload
52- * with the JSON of the check-in if a new check-in has been made within
53- * the polling interval.
54- */
55- function SwarmInNode ( n ) {
56- RED . nodes . createNode ( this , n ) ;
57- var credentials = RED . nodes . getCredentials ( n . swarm ) ;
58- var node = this ;
59- var credentialsOk = checkCredentials ( node , credentials ) ;
60- if ( credentialsOk ) {
61- var repeat = 900000 ; // 15 mins
62- var now = Math . floor ( ( ( new Date ( ) ) . getTime ( ) ) / 1000 ) ; // time now in seconds since epoch
63- var afterTimestamp = now ;
64- var lastcheckin = null ;
65-
66- var interval = setInterval ( function ( ) {
67- node . emit ( "input" , { } ) ;
68- } , repeat ) ;
69-
70- this . on ( "input" , function ( msg ) {
71- getCheckinsAfterTimestamp ( node , "self" , afterTimestamp , credentials , msg , function ( msg ) {
72- var latestcheckin = JSON . stringify ( msg ) ;
73- if ( latestcheckin != lastcheckin ) {
74- lastcheckin = latestcheckin ;
75- afterTimestamp = msg . payload . createdAt ;
76- node . send ( msg ) ;
77- }
78- } ) ;
79- } ) ;
80-
81- this . on ( "close" , function ( ) {
82- if ( interval != null ) {
83- clearInterval ( interval ) ;
84- }
85- } ) ;
86-
87- }
88- }
89-
90- RED . nodes . registerType ( "swarm in" , SwarmInNode ) ;
91-
92- /**
93- * Swarm query node - will return the most recent check-in since
94- * the node has been initialized. If a check-in exists the node will always return
95- * the most recent even if no new check-ins have happened since the previous query.
96- * The node only populates msg.payload when a check-in is found.
97- */
98- function SwarmQueryNode ( n ) {
99- RED . nodes . createNode ( this , n ) ;
100- var node = this ;
101- this . request = n . request || "get-most-recent-checkin" ;
102- var credentials = RED . nodes . getCredentials ( n . swarm ) ;
103- var credentialsOk = checkCredentials ( node , credentials ) ;
104- if ( credentialsOk ) {
105- var now = Math . floor ( ( ( new Date ( ) ) . getTime ( ) ) / 1000 ) ; // time now in seconds since epoch (rounded down)
106- var afterTimestamp = now ;
107-
108- this . on ( "input" , function ( msg ) {
109- if ( node . request === "get-most-recent-checkin" ) {
110- getCheckinsAfterTimestamp ( node , "self" , afterTimestamp , credentials , msg , function ( msg ) {
111- afterTimestamp = msg . payload . createdAt - 2 ;
112- node . send ( msg ) ;
113- } ) ;
114- }
115- } ) ;
116- }
117- }
118-
119- RED . nodes . registerType ( "swarm" , SwarmQueryNode ) ;
120-
121- function checkCredentials ( node , credentials ) {
122- if ( credentials && credentials . clientid && credentials . clientsecret && credentials . accesstoken ) {
123- return true ;
124- } else {
125- node . error ( "problem with credentials being set: " + credentials + ", " ) ;
126- node . status ( { fill :"red" , shape :"ring" , text :"failed" } ) ;
127- return false ;
128- }
129- }
130-
131- function getCheckinsAfterTimestamp ( node , userid , afterTimestamp , credentials , msg , callback ) {
132- var apiUrl = "https://api.foursquare.com/v2/users/" + userid + "/checkins?oauth_token=" + credentials . accesstoken + "&v=20141016&afterTimestamp=" + afterTimestamp + "&sort=newestfirst" ;
133- request . get ( apiUrl , function ( err , httpResponse , body ) {
134- if ( err ) {
135- node . error ( err . toString ( ) ) ;
136- node . status ( { fill :"red" , shape :"ring" , text :"failed" } ) ;
137- } else {
138- var result = JSON . parse ( body ) ;
139- if ( result . meta . code != 200 ) {
140- node . error ( "Error code: " + result . meta . code + ", errorDetail: " + result . meta . errorDetail ) ;
141- node . status ( { fill :"red" , shape :"ring" , text :"failed" } ) ;
142- } else {
143- if ( result . response . checkins . items . length !== 0 ) {
144- var latest = result . response . checkins . items [ 0 ] ;
145- msg . payload = { } ;
146- msg . payload = latest ;
147- callback ( msg ) ;
148- }
149- }
150- }
151- } ) ;
152- }
15347
154- RED . httpAdmin . get ( '/swarm -credentials/auth' , function ( req , res ) {
48+ RED . httpAdmin . get ( '/foursquare -credentials/auth' , function ( req , res ) {
15549 if ( ! req . query . clientid || ! req . query . clientsecret || ! req . query . id || ! req . query . callback ) {
15650 return res . status ( 400 ) . send ( 'ERROR: request does not contain the required parameters' ) ;
15751 }
@@ -175,7 +69,7 @@ module.exports = function(RED) {
17569 res . redirect ( url ) ;
17670 } ) ;
17771
178- RED . httpAdmin . get ( '/swarm -credentials/auth/callback' , function ( req , res ) {
72+ RED . httpAdmin . get ( '/foursquare -credentials/auth/callback' , function ( req , res ) {
17973 if ( req . query . error ) {
18074 return res . send ( "ERROR: " + req . query . error + ": " + req . query . error_description ) ;
18175 }
0 commit comments