-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFTTTNodeTrigger.js
148 lines (116 loc) · 6.17 KB
/
IFTTTNodeTrigger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
var ifttt = require('ifttt');
var iftttNodeTriggerField = require('./IFTTTNodeTriggerField');
var util = require('util');
var utility = require('./utility');
module.exports = {
class: function(node) {
function trigger() {
trigger.super_.call(this, node.endpoint);
}
util.inherits(trigger, ifttt.Trigger);
trigger.prototype.node = node;
trigger.prototype._getResponseData = function(request, requestPayload, callback) {
var fields = node.fields;
if (!Array.isArray(fields)) {
return trigger.generateErrorResponse('fields is unexpected type \'' + typeof fields + '\'', true, callback);
}
const createResult = function(node, date) {
const timestamp = Math.floor(date.getTime() / 1000);
return {
'meta': {
'id': node.endpoint + '_' + timestamp,
'timestamp': timestamp
}
};
};
const handleTest = function(node, responseLimit, callback) {
var results = [];
// TODO: find a place for 50 to be shared
for (var count = 0; count < (responseLimit || 50); count ++) { // IFTTT defaults to max 50 responses if no limit is defined
var result = createResult(node, new Date(new Date().getTime() - count * 1000));
for(var index = 0; index < fields.length; index ++) {
const field = fields[index];
if (typeof field === 'object' && typeof field.name === 'string') {
const fieldValue = requestPayload.getField(field.name);
if (field.sampleDataInvalid && fieldValue === field.sampleDataInvalid) {
// TODO: this isn't really supposed to happen apparently
// TODO: think about what to do here
} else if (field.sampleDataValid && fieldValue === field.sampleDataValid) {
result[field.name] = 'processed';
} else {
// TODO: should report unexpected
}
}
}
// TODO: ensure we have all ingredients configured for node expectation and IFTTT expectation
for(var index = 0; index < node.ingredients.length; index ++) {
const ingredient = node.ingredients[index];
if (typeof ingredient === 'object' && typeof ingredient.slug === 'string') {
result[ingredient.slug] = 'This is a test.';
}
}
results.push(result);
}
return callback(null, results);
};
const responseLimit = requestPayload.getLimit();
if (utility.isTestMode(request)) {
return handleTest(this.node, responseLimit, callback);
}
var results = [];
// TODO: notify if we're going to drop results that haven't been sent
if (this.node.triggerEventQueue.length > 50) {
this.node.triggerEventQueue.length = 50;
}
for(var index = 0; index < this.node.triggerEventQueue.length; index ++) {
const event = this.node.triggerEventQueue[index];
var result = createResult(this.node, event.createdAt);
// TODO: ensure we have all fields configured for node expectation and IFTTT expectation
for(var fieldsIndex = 0; fieldsIndex < fields.length; fieldsIndex ++) {
const field = fields[fieldsIndex];
if (typeof field === 'object' && typeof field.name === 'string') {
const fieldValue = requestPayload.getField(field.name);
if (typeof fieldValue === 'undefined') {
return action.generateErrorResponse('Value required for trigger field \'' + field.name + '\'', true, callback);
}
// TODO: filter via match, regex, etc
// TODO: custom function processing
result[field.name] = 'processed';
}
}
var missingIngredients = [];
// TODO: ensure we have all ingredients configured for node expectation and IFTTT expectation
for(var ingredientsIndex = 0; ingredientsIndex < this.node.ingredients.length; ingredientsIndex ++) {
const ingredient = this.node.ingredients[ingredientsIndex];
if (typeof ingredient === 'object' && typeof ingredient.slug === 'string') {
if (typeof event.payload[ingredient.slug] !== 'undefined') {
result[ingredient.slug] = event.payload[ingredient.slug];
} else {
missingIngredients.push(ingredient.slug);
}
}
}
// TODO: should potentially be handled downstream at queue?
if (missingIngredients.length === 0) {
results.push(result);
event.sent = true;
} else {
this.node.error("Trigger event payload missing ingredients: '" + missingIngredients + "'")
}
}
this.node.log("Sending IFTTT " + results.length + " trigger events");
this.node.updateNodeStatus();
return callback(null, results);
}
return trigger;
},
createDefault: function(node) {
const result = new (this.class(node))();
if (node.field !== undefined) {
const triggerFieldInstance = iftttNodeTriggerField.createDefault(node.field);
result.registerField(triggerFieldInstance);
}
return result;
}
};