1 |
|
2 |
|
3 |
|
4 | var baseUrl = "https://api.box.com/2.0/events";
|
5 |
|
6 | var Trigger = {
|
7 | id: "box",
|
8 | title: "Box"
|
9 | };
|
10 |
|
11 | module.exports = Trigger;
|
12 |
|
13 | Trigger.input = {
|
14 | "type": "object",
|
15 | "title": "Select Trigger",
|
16 | "properties": {
|
17 | "auth": {
|
18 | "type": "string",
|
19 | "title": "Box Access Token",
|
20 | "minLength": 1,
|
21 | "oauth": "box",
|
22 | "propertyOrder": 1
|
23 | },
|
24 | "dir_name": {
|
25 | "type": "string",
|
26 | "title": "Directory Name",
|
27 | "minLength": 1,
|
28 | "default": "All Files",
|
29 | "propertyOrder": 2,
|
30 | "description": "Enter directory name"
|
31 | },
|
32 | "customFilters": {
|
33 | "type": "array",
|
34 | "title": "Custom Filters",
|
35 | "items": {
|
36 | "type": "object",
|
37 | "title": "filter",
|
38 | "properties": {
|
39 | "input": {
|
40 | "type": "string",
|
41 | "title": "Input",
|
42 | "minLength": 1
|
43 | },
|
44 | "operator": {
|
45 | "type": "string",
|
46 | "title": "Condition",
|
47 | "enum": [
|
48 | "Equals",
|
49 | "Equals(Number)",
|
50 | "GreaterThan",
|
51 | "LessThan",
|
52 | "Contains",
|
53 | "DoesNotContains",
|
54 | "ObjectHasKey",
|
55 | "matches",
|
56 | "isNull",
|
57 | "isEmpty",
|
58 | "isNumber",
|
59 | "isObject",
|
60 | "isArray",
|
61 | "isBoolean",
|
62 | "isDate",
|
63 | "isUndefined"
|
64 | ]
|
65 | },
|
66 | "expected": {
|
67 | "type": "string",
|
68 | "title": "Expected",
|
69 | "minLength": 1
|
70 | }
|
71 | }
|
72 | }
|
73 | }
|
74 | },
|
75 | "oneOf": [
|
76 | {
|
77 | "type": "object",
|
78 | "title": "New/Update File",
|
79 | "description": "Triggers when a new file is added or an existing file is updated",
|
80 | "properties": {
|
81 | "event": {
|
82 | "type": "string",
|
83 | "enum": [
|
84 | "new_file"
|
85 | ],
|
86 | "options": {
|
87 | "hidden": true
|
88 | }
|
89 | }
|
90 | }
|
91 | },
|
92 | {
|
93 | "type": "object",
|
94 | "title": "New Folder",
|
95 | "description": "Triggers when a new folder is created",
|
96 | "properties": {
|
97 | "event": {
|
98 | "type": "string",
|
99 | "enum": [
|
100 | "new_folder"
|
101 | ],
|
102 | "options": {
|
103 | "hidden": true
|
104 | }
|
105 | }
|
106 | }
|
107 | }
|
108 | ]
|
109 | };
|
110 |
|
111 | Trigger.output = {
|
112 | "new_file": {
|
113 | "type": "object",
|
114 | "properties": {
|
115 | "type": {
|
116 | "type": "string",
|
117 | "title": "type",
|
118 | "displayTitle": "Type"
|
119 | },
|
120 | "event_id": {
|
121 | "type": "string",
|
122 | "title": "event_id",
|
123 | "displayTitle": "Event ID"
|
124 | },
|
125 | "created_by": {
|
126 | "type": "object",
|
127 | "title": "created_by",
|
128 | "displayTitle": "Created By",
|
129 | "properties": {
|
130 | "type": {
|
131 | "type": "string",
|
132 | "title": "type",
|
133 | "displayTitle": "Creater Type"
|
134 | },
|
135 | "id": {
|
136 | "type": "string",
|
137 | "title": "id",
|
138 | "displayTitle": "Creater ID"
|
139 | },
|
140 | "name": {
|
141 | "type": "string",
|
142 | "title": "name",
|
143 | "displayTitle": "Creater Name"
|
144 | },
|
145 | "login": {
|
146 | "type": "string",
|
147 | "title": "login",
|
148 | "displayTitle": "Creater EmailId"
|
149 | }
|
150 | }
|
151 | },
|
152 | "created_at": {
|
153 | "type": "boolean",
|
154 | "title": "created_at",
|
155 | "displayTitle": "Created Time"
|
156 | },
|
157 | "recorded_at": {
|
158 | "type": "string",
|
159 | "title": "recorded_at",
|
160 | "displayTitle": "Recorded Time"
|
161 | },
|
162 | "event_type": {
|
163 | "type": "string",
|
164 | "title": "event_type",
|
165 | "displayTitle": "Event Type"
|
166 | },
|
167 | "source": {
|
168 | "type": "object",
|
169 | "title": "source",
|
170 | "displayTitle": "Source",
|
171 | "properties": {
|
172 | "type": {
|
173 | "type": "string",
|
174 | "title": "type",
|
175 | "displayTitle": "Source File Type"
|
176 | },
|
177 | "id": {
|
178 | "type": "string",
|
179 | "title": "id",
|
180 | "displayTitle": "Source File ID"
|
181 | },
|
182 | "name": {
|
183 | "type": "string",
|
184 | "title": "name",
|
185 | "displayTitle": "Source File Name"
|
186 | },
|
187 | "description": {
|
188 | "type": "string",
|
189 | "title": "description",
|
190 | "displayTitle": "Source File Description"
|
191 | },
|
192 | "size": {
|
193 | "type": "string",
|
194 | "title": "size",
|
195 | "displayTitle": "Source File Size"
|
196 | },
|
197 | "created_at": {
|
198 | "type": "string",
|
199 | "title": "created_at",
|
200 | "displayTitle": "Source File Created Time"
|
201 | },
|
202 | "modified_at": {
|
203 | "type": "string",
|
204 | "title": "modified_at",
|
205 | "displayTitle": "Source File Modified At"
|
206 | },
|
207 | "created_by": {
|
208 | "type": "object",
|
209 | "title": "created_by",
|
210 | "displayTitle": "Source Created By",
|
211 | "properties": {}
|
212 | },
|
213 | "modified_by": {
|
214 | "type": "object",
|
215 | "title": "modified_by",
|
216 | "displayTitle": "Source Modified By",
|
217 | "properties": {}
|
218 | },
|
219 | "owned_by": {
|
220 | "type": "object",
|
221 | "title": "owned_by",
|
222 | "displayTitle": "Source Owned By",
|
223 | "properties": {}
|
224 | },
|
225 | "parent": {
|
226 | "type": "object",
|
227 | "title": "parent",
|
228 | "displayTitle": "Source Parent",
|
229 | "properties": {}
|
230 | },
|
231 | "item_status": {
|
232 | "type": "string",
|
233 | "title": "item_status",
|
234 | "displayTitle": "Source File Item Status"
|
235 | },
|
236 | "synced": {
|
237 | "type": "boolean",
|
238 | "title": "synced",
|
239 | "displayTitle": "Source File Synced"
|
240 | }
|
241 | }
|
242 | }
|
243 | }
|
244 | },
|
245 | "new_folder": {
|
246 | "type": "object",
|
247 | "properties": {
|
248 | "type": {
|
249 | "type": "string",
|
250 | "title": "type",
|
251 | "displayTitle": "Type"
|
252 | },
|
253 | "event_id": {
|
254 | "type": "string",
|
255 | "title": "event_id",
|
256 | "displayTitle": "Event ID"
|
257 | },
|
258 | "created_by": {
|
259 | "type": "object",
|
260 | "title": "created_by",
|
261 | "displayTitle": "Created By",
|
262 | "properties": {
|
263 | "type": {
|
264 | "type": "string",
|
265 | "title": "type",
|
266 | "displayTitle": "Creater Type"
|
267 | },
|
268 | "id": {
|
269 | "type": "string",
|
270 | "title": "id",
|
271 | "displayTitle": "Creater ID"
|
272 | },
|
273 | "name": {
|
274 | "type": "string",
|
275 | "title": "name",
|
276 | "displayTitle": "Creater Name"
|
277 | },
|
278 | "login": {
|
279 | "type": "string",
|
280 | "title": "login",
|
281 | "displayTitle": "Creater EmailId"
|
282 | }
|
283 | }
|
284 | },
|
285 | "created_at": {
|
286 | "type": "boolean",
|
287 | "title": "created_at",
|
288 | "displayTitle": "Created Time"
|
289 | },
|
290 | "recorded_at": {
|
291 | "type": "string",
|
292 | "title": "recorded_at",
|
293 | "displayTitle": "Recorded Time"
|
294 | },
|
295 | "event_type": {
|
296 | "type": "string",
|
297 | "title": "event_type",
|
298 | "displayTitle": "Event Type"
|
299 | },
|
300 | "source": {
|
301 | "type": "object",
|
302 | "title": "source",
|
303 | "displayTitle": "Source",
|
304 | "properties": {
|
305 | "type": {
|
306 | "type": "string",
|
307 | "title": "type",
|
308 | "displayTitle": "Source File Type"
|
309 | },
|
310 | "id": {
|
311 | "type": "string",
|
312 | "title": "id",
|
313 | "displayTitle": "Source File ID"
|
314 | },
|
315 | "name": {
|
316 | "type": "string",
|
317 | "title": "name",
|
318 | "displayTitle": "Source File Name"
|
319 | },
|
320 | "description": {
|
321 | "type": "string",
|
322 | "title": "description",
|
323 | "displayTitle": "Source File Description"
|
324 | },
|
325 | "size": {
|
326 | "type": "string",
|
327 | "title": "size",
|
328 | "displayTitle": "Source File Size"
|
329 | },
|
330 | "created_at": {
|
331 | "type": "string",
|
332 | "title": "created_at",
|
333 | "displayTitle": "Source File Created Time"
|
334 | },
|
335 | "modified_at": {
|
336 | "type": "string",
|
337 | "title": "modified_at",
|
338 | "displayTitle": "Source File Modified At"
|
339 | },
|
340 | "created_by": {
|
341 | "type": "object",
|
342 | "title": "created_by",
|
343 | "displayTitle": "Source Created By",
|
344 | "properties": {}
|
345 | },
|
346 | "modified_by": {
|
347 | "type": "object",
|
348 | "title": "modified_by",
|
349 | "displayTitle": "Source Modified By",
|
350 | "properties": {}
|
351 | },
|
352 | "owned_by": {
|
353 | "type": "object",
|
354 | "title": "owned_by",
|
355 | "displayTitle": "Source Owned By",
|
356 | "properties": {}
|
357 | },
|
358 | "parent": {
|
359 | "type": "object",
|
360 | "title": "parent",
|
361 | "displayTitle": "Source Parent",
|
362 | "properties": {}
|
363 | },
|
364 | "item_status": {
|
365 | "type": "string",
|
366 | "title": "item_status",
|
367 | "displayTitle": "Source File Item Status"
|
368 | },
|
369 | "synced": {
|
370 | "type": "boolean",
|
371 | "title": "synced",
|
372 | "displayTitle": "Source File Synced"
|
373 | }
|
374 | }
|
375 | }
|
376 | }
|
377 | }
|
378 | };
|
379 |
|
380 | var mock_data = {
|
381 | "new_file": {
|
382 | "type": "event",
|
383 | "event_id": "099e980927d9fa450b236985540964",
|
384 | "created_by": {
|
385 | "type": "user",
|
386 | "id": "245466",
|
387 | "name": "Mike",
|
388 | "login": "mike@example.com"
|
389 | },
|
390 | "created_at": "2014-08-28T03:17:07-07:00",
|
391 | "recorded_at": "2014-08-28T03:17:07-07:00",
|
392 | "event_type": "ITEM_UPLOAD",
|
393 | "session_id": null,
|
394 | "source": {
|
395 | "type": "file",
|
396 | "id": "35607886",
|
397 | "name": "test.txt",
|
398 | "description": "Test doc",
|
399 | "size": 21333,
|
400 | "created_at": "2015-08-28T03:17:07-07:00",
|
401 | "modified_at": "2015-08-28T03:17:07-07:00",
|
402 | "created_by": {
|
403 | "type": "user",
|
404 | "id": "24566",
|
405 | "name": "Mike",
|
406 | "login": "mike@example.com"
|
407 | },
|
408 | "modified_by": {
|
409 | "type": "user",
|
410 | "id": "24566",
|
411 | "name": "Mike",
|
412 | "login": "mike@example.com"
|
413 | },
|
414 | "owned_by": {
|
415 | "type": "user",
|
416 | "id": "24566",
|
417 | "name": "Mike",
|
418 | "login": "mike@example.com"
|
419 | },
|
420 | "parent": {
|
421 | "type": "folder",
|
422 | "id": "0",
|
423 | "sequence_id": null,
|
424 | "etag": null,
|
425 | "name": "All Files"
|
426 | },
|
427 | "item_status": "active",
|
428 | "synced": true
|
429 | }
|
430 | },
|
431 | "new_folder": {
|
432 | "type": "event",
|
433 | "event_id": "572603a5ada74d5aa31a66e3b57f60",
|
434 | "created_by": {
|
435 | "type": "user",
|
436 | "id": "245882466",
|
437 | "name": "Mike",
|
438 | "login": "mike@example.com"
|
439 | },
|
440 | "created_at": "2014-08-28T03:25:17-07:00",
|
441 | "recorded_at": "2014-08-28T03:25:18-07:00",
|
442 | "event_type": "ITEM_CREATE",
|
443 | "source": {
|
444 | "type": "folder",
|
445 | "id": "4335998",
|
446 | "name": "Data",
|
447 | "description": "<Test></Test>",
|
448 | "size": 21333,
|
449 | "created_at": "2014-08-28T03:25:17-07:00",
|
450 | "modified_at": "2014-08-28T03:25:17-07:00",
|
451 | "created_by": {
|
452 | "type": "user",
|
453 | "id": "24566",
|
454 | "name": "Mike",
|
455 | "login": "mike@example.com"
|
456 | },
|
457 | "modified_by": {
|
458 | "type": "user",
|
459 | "id": "24566",
|
460 | "name": "Mike",
|
461 | "login": "mike@example.com"
|
462 | },
|
463 | "owned_by": {
|
464 | "type": "user",
|
465 | "id": "24566",
|
466 | "name": "Mike",
|
467 | "login": "mike@example.com"
|
468 | },
|
469 | "parent": {
|
470 | "type": "folder",
|
471 | "id": "0",
|
472 | "sequence_id": null,
|
473 | "etag": null,
|
474 | "name": "All Files"
|
475 | },
|
476 | "item_status": "active",
|
477 | "synced": false
|
478 | }
|
479 | }
|
480 | };
|
481 |
|
482 | Trigger.execute = execute;
|
483 | Trigger.validate = validate;
|
484 | Trigger.activate = activate;
|
485 |
|
486 | function activate(input, options, output) {
|
487 | var next = 0;
|
488 | if (options.meta !== undefined &&
|
489 | options.meta.next_stream_position !== undefined) {
|
490 | next = options.meta.next_stream_position;
|
491 | }
|
492 | request({
|
493 | url: baseUrl + "?stream_position=" + next + "&stream_type=changes",
|
494 | headers: {
|
495 | Authorization: "Bearer " + input.auth
|
496 | }
|
497 | }, function(err, res, body) {
|
498 | if (err || res.statusCode !== 200) {
|
499 | return output("Enter valid Authentication details!!");
|
500 | }
|
501 | if (body && typeof body == "string") {
|
502 | body = JSON.parse(body);
|
503 | }
|
504 | if (body.next_stream_position) {
|
505 | options.setMeta({
|
506 | next_stream_position: body.next_stream_position
|
507 | });
|
508 | }
|
509 | output(null, true);
|
510 | })
|
511 | }
|
512 |
|
513 | function validate(input, options, output) {
|
514 | request({
|
515 | url: baseUrl + "?stream_position=0&stream_type=changes",
|
516 | headers: {
|
517 | Authorization: "Bearer " + input.auth
|
518 | }
|
519 | }, function(err, res, body) {
|
520 | if (err || res.statusCode !== 200) {
|
521 | return output("Enter valid Authentication details!!");
|
522 | }
|
523 | if (body && typeof body === "string") {
|
524 | body = JSON.parse(body);
|
525 | }
|
526 | if (body.next_stream_position &&
|
527 | (options.meta == undefined || options.meta.next_stream_position == undefined)) {
|
528 | options.setMeta({
|
529 | next_stream_position: body.next_stream_position
|
530 | });
|
531 | }
|
532 | output(null, true);
|
533 | })
|
534 | }
|
535 |
|
536 | function execute(input, options, output) {
|
537 | run(input, options, function(err, data) {
|
538 | if (err || !data) {
|
539 | return output(err || "empty");
|
540 | }
|
541 | customFilters.filter(input.customFilters, data, output);
|
542 | });
|
543 | }
|
544 |
|
545 | function run(input, options, output) {
|
546 |
|
547 | var url = baseUrl + "?stream_position=" + options.meta.next_stream_position + "&stream_type=changes&limit=799";
|
548 |
|
549 | var opt = {
|
550 | input: input,
|
551 | opsn: options,
|
552 | url: url
|
553 | };
|
554 | doRequest(opt, output);
|
555 | }
|
556 |
|
557 | function doRequest(options, callback) {
|
558 | request({
|
559 | url: options.url,
|
560 | headers: {
|
561 | Authorization: "Bearer " + options.input.auth
|
562 | }
|
563 | }, function(err, res, body) {
|
564 | if (err) {
|
565 | return callback(err);
|
566 | }
|
567 | if (!res || !res.statusCode || res.statusCode !== 200) {
|
568 | return callback(body);
|
569 | }
|
570 | if (typeof(body) === "string") {
|
571 | body = JSON.parse(body);
|
572 | }
|
573 | if (body && body !== null) {
|
574 | switch (options.input.event) {
|
575 | case "new_file":
|
576 | return findNewFile(body, options, callback);
|
577 | case "new_folder":
|
578 | return findNewFolder(body, options, callback);
|
579 |
|
580 | }
|
581 | }
|
582 | callback(null, []);
|
583 | });
|
584 | }
|
585 |
|
586 | function findNewFile(body, options, callback) {
|
587 | var lastTime = (options.opsn.meta.last_time || 0);
|
588 |
|
589 |
|
590 | if (!body.entries.length) {
|
591 | options.opsn.setMeta({
|
592 | next_stream_position: body.next_stream_position,
|
593 | last_time: lastTime
|
594 | });
|
595 | return callback(null, []);
|
596 | }
|
597 |
|
598 | var filteredData = body.entries.filter(function(entry) {
|
599 | if (entry.event_type === "ITEM_UPLOAD" &&
|
600 | options.input.dir_name.trim() === entry.source.parent.name &&
|
601 | (new Date(entry.created_at)).getTime() > lastTime &&
|
602 | entry.source.type === "file") {
|
603 | return true;
|
604 | }
|
605 | });
|
606 |
|
607 | filteredData = filteredData.sort(function(a, b) {
|
608 | return a < b;
|
609 | });
|
610 |
|
611 | lastTime = filteredData.length ? (new Date(filteredData[0].created_at)).getTime() : (options.opsn.meta.last_time || 0);
|
612 |
|
613 | options.opsn.setMeta({
|
614 | next_stream_position: body.next_stream_position,
|
615 | last_time: lastTime
|
616 | });
|
617 |
|
618 |
|
619 | callback(null, filteredData);
|
620 | filteredData = null;
|
621 | }
|
622 |
|
623 | function findNewFolder(body, options, callback) {
|
624 |
|
625 | var lastTime = (options.opsn.meta.last_time || 0);
|
626 |
|
627 |
|
628 |
|
629 | if (!body.entries.length) {
|
630 | options.opsn.setMeta({
|
631 | next_stream_position: body.next_stream_position,
|
632 | last_time: lastTime
|
633 | });
|
634 | return callback(null, []);
|
635 | }
|
636 |
|
637 | var filteredData = body.entries.filter(function(entry) {
|
638 | if (entry.event_type === "ITEM_CREATE" &&
|
639 | options.input.dir_name.trim() === entry.source.parent.name &&
|
640 | (new Date(entry.created_at)).getTime() > lastTime &&
|
641 | entry.source.type === "folder") {
|
642 | return true;
|
643 | }
|
644 | });
|
645 |
|
646 |
|
647 | filteredData = filteredData.sort(function(a, b) {
|
648 | return a < b;
|
649 | });
|
650 |
|
651 | lastTime = filteredData.length ? (new Date(filteredData[0].created_at)).getTime() : (options.opsn.meta.last_time || 0);
|
652 |
|
653 | options.opsn.setMeta({
|
654 | next_stream_position: body.next_stream_position,
|
655 | last_time: lastTime
|
656 | });
|
657 |
|
658 | callback(null, filteredData);
|
659 | filteredData = null;
|
660 | } |
\ | No newline at end of file |