-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
new-or-modified-files.mjs
161 lines (149 loc) · 4.84 KB
/
new-or-modified-files.mjs
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
149
150
151
152
153
154
155
156
157
158
159
160
161
// This source processes changes to any files in a user's Google Drive,
// implementing strategy enumerated in the Push Notifications API docs:
// https://developers.google.com/drive/api/v3/push and here:
// https://developers.google.com/drive/api/v3/manage-changes
//
// This source has two interfaces:
//
// 1) The HTTP requests tied to changes in the user's Google Drive
// 2) A timer that runs on regular intervals, renewing the notification channel as needed
import common from "../common-webhook.mjs";
import sampleEmit from "./test-event.mjs";
import {
GOOGLE_DRIVE_NOTIFICATION_ADD,
GOOGLE_DRIVE_NOTIFICATION_CHANGE,
GOOGLE_DRIVE_NOTIFICATION_UPDATE,
} from "../../common/constants.mjs";
import commonDedupeChanges from "../common-dedupe-changes.mjs";
const { googleDrive } = common.props;
export default {
...common,
key: "google_drive-new-or-modified-files",
name: "New or Modified Files (Instant)",
description: "Emit new event when a file in the selected Drive is created, modified or trashed.",
version: "0.3.1",
type: "source",
// Dedupe events based on the "x-goog-message-number" header for the target channel:
// https://developers.google.com/drive/api/v3/push#making-watch-requests
dedupe: "unique",
props: {
...common.props,
folders: {
type: "string[]",
label: "Folder(s)",
description:
"The folder(s) to watch for changes. Leave blank to watch for any new or modified file in the Drive.",
optional: true,
default: [],
options({ prevContext }) {
const { nextPageToken } = prevContext;
const baseOpts = {
q: "mimeType = 'application/vnd.google-apps.folder' and trashed = false",
};
const opts = this.isMyDrive()
? baseOpts
: {
...baseOpts,
corpora: "drive",
driveId: this.getDriveId(),
includeItemsFromAllDrives: true,
supportsAllDrives: true,
};
return this.googleDrive.listFilesOptions(nextPageToken, opts);
},
},
watchForPropertiesChanges: {
propDefinition: [
googleDrive,
"watchForPropertiesChanges",
],
},
...commonDedupeChanges.props,
},
hooks: {
async deploy() {
const daysAgo = new Date();
daysAgo.setDate(daysAgo.getDate() - 30);
const timeString = daysAgo.toISOString();
const args = this.getListFilesOpts({
q: `mimeType != "application/vnd.google-apps.folder" and modifiedTime > "${timeString}" and trashed = false`,
fields: "files",
pageSize: 5,
});
const { files } = await this.googleDrive.listFilesInPage(null, args);
await this.processChanges(files);
},
...common.hooks,
},
methods: {
...common.methods,
shouldProcess(file) {
if (file.mimeType !== "application/vnd.google-apps.folder") {
const watchedFolders = new Set(this.folders);
return (
watchedFolders.size == 0 ||
(file.parents && file.parents.some((p) => watchedFolders.has(p)))
);
}
},
getUpdateTypes() {
return [
GOOGLE_DRIVE_NOTIFICATION_ADD,
GOOGLE_DRIVE_NOTIFICATION_CHANGE,
GOOGLE_DRIVE_NOTIFICATION_UPDATE,
];
},
generateMeta(data, headers) {
const {
id: fileId,
name: summary,
modifiedTime: tsString,
} = data;
const ts = Date.parse(tsString);
const eventId = headers && headers["x-goog-message-number"];
return {
id: `${fileId}-${eventId || ts}`,
summary,
ts,
};
},
async getChanges(headers) {
if (!headers) {
return {
change: { },
};
}
const resourceUri = headers["x-goog-resource-uri"];
const metadata = await this.googleDrive.getFileMetadata(`${resourceUri}&fields=*`);
return {
...metadata,
change: {
state: headers["x-goog-resource-state"],
resourceURI: headers["x-goog-resource-uri"],
changed: headers["x-goog-changed"], // "Additional details about the changes. Possible values: content, parents, children, permissions"
},
};
},
async processChanges(changedFiles, headers) {
const changes = await this.getChanges(headers);
const filteredFiles = this.checkMinimumInterval(changedFiles);
for (const file of filteredFiles) {
file.parents = (await this.googleDrive.getFile(file.id, {
fields: "parents",
})).parents;
console.log(file); // see what file was processed
if (!this.shouldProcess(file)) {
console.log(`Skipping file ${file.name}`);
continue;
}
const eventToEmit = {
file,
...changes,
};
const meta = this.generateMeta(file, headers);
this.$emit(eventToEmit, meta);
}
},
},
sampleEmit,
};