|
| 1 | +import common from "../common-webhook.mjs"; |
| 2 | +import { |
| 3 | + MY_DRIVE_VALUE, |
| 4 | + GOOGLE_DRIVE_NOTIFICATION_ADD, |
| 5 | + GOOGLE_DRIVE_NOTIFICATION_CHANGE, |
| 6 | + GOOGLE_DRIVE_NOTIFICATION_UPDATE, |
| 7 | +} from "../../common/constants.mjs"; |
| 8 | +import commonDedupeChanges from "../common-dedupe-changes.mjs"; |
| 9 | +import { stashFile } from "../../common/utils.mjs"; |
| 10 | + |
| 11 | +export default { |
| 12 | + ...common, |
| 13 | + key: "google_drive-changes-to-files-in-drive", |
| 14 | + name: "Changes to Files in Drive", |
| 15 | + description: "Watches for changes to specific files in the user's drive, emitting an event when a change is made to one of those files. [See the documentation](https://developers.google.com/drive/api/v3/reference/changes/watch)", |
| 16 | + version: "0.0.1", |
| 17 | + type: "source", |
| 18 | + dedupe: "unique", |
| 19 | + props: { |
| 20 | + infoAlert: { |
| 21 | + type: "alert", |
| 22 | + alertType: "info", |
| 23 | + content: "This source uses `changes.watch` and supports watching 10+ files. To watch for changes to fewer than 10 files, you may want to use the **Changes to Specific Files** source instead (uses `files.watch`).", |
| 24 | + }, |
| 25 | + ...common.props, |
| 26 | + drive: { |
| 27 | + type: "string", |
| 28 | + label: "Drive", |
| 29 | + description: "Defaults to `My Drive`. To use a [Shared Drive](https://support.google.com/a/users/answer/9310351), use the **Changes to Specific Files (Shared Drive)** source instead.", |
| 30 | + optional: true, |
| 31 | + default: MY_DRIVE_VALUE, |
| 32 | + }, |
| 33 | + files: { |
| 34 | + type: "string[]", |
| 35 | + label: "Files", |
| 36 | + description: "The files you want to watch for changes.", |
| 37 | + options({ prevContext }) { |
| 38 | + const { nextPageToken } = prevContext; |
| 39 | + return this.googleDrive.listFilesOptions(nextPageToken, this.getListFilesOpts()); |
| 40 | + }, |
| 41 | + }, |
| 42 | + includeLink: { |
| 43 | + label: "Include Link", |
| 44 | + type: "boolean", |
| 45 | + description: "Upload file to your File Stash and emit temporary download link to the file. Google Workspace documents will be converted to PDF. See [the docs](https://pipedream.com/docs/connect/components/files) to learn more about working with files in Pipedream.", |
| 46 | + default: false, |
| 47 | + optional: true, |
| 48 | + }, |
| 49 | + dir: { |
| 50 | + type: "dir", |
| 51 | + accessMode: "write", |
| 52 | + optional: true, |
| 53 | + }, |
| 54 | + ...commonDedupeChanges.props, |
| 55 | + }, |
| 56 | + hooks: { |
| 57 | + async deploy() { |
| 58 | + const daysAgo = new Date(); |
| 59 | + daysAgo.setDate(daysAgo.getDate() - 30); |
| 60 | + const timeString = daysAgo.toISOString(); |
| 61 | + |
| 62 | + const args = this.getListFilesOpts({ |
| 63 | + q: `mimeType != "application/vnd.google-apps.folder" and modifiedTime > "${timeString}" and trashed = false`, |
| 64 | + fields: "files", |
| 65 | + pageSize: 5, |
| 66 | + }); |
| 67 | + |
| 68 | + const { files } = await this.googleDrive.listFilesInPage(null, args); |
| 69 | + |
| 70 | + await this.processChanges(files); |
| 71 | + }, |
| 72 | + ...common.hooks, |
| 73 | + }, |
| 74 | + methods: { |
| 75 | + ...common.methods, |
| 76 | + getUpdateTypes() { |
| 77 | + return [ |
| 78 | + GOOGLE_DRIVE_NOTIFICATION_ADD, |
| 79 | + GOOGLE_DRIVE_NOTIFICATION_CHANGE, |
| 80 | + GOOGLE_DRIVE_NOTIFICATION_UPDATE, |
| 81 | + ]; |
| 82 | + }, |
| 83 | + generateMeta(data, headers) { |
| 84 | + const { |
| 85 | + id: fileId, |
| 86 | + name: fileName, |
| 87 | + modifiedTime: tsString, |
| 88 | + } = data; |
| 89 | + const ts = Date.parse(tsString); |
| 90 | + const eventId = headers && headers["x-goog-message-number"]; |
| 91 | + const resourceState = headers && headers["x-goog-resource-state"]; |
| 92 | + |
| 93 | + const summary = resourceState |
| 94 | + ? `${resourceState.toUpperCase()} - ${fileName || "Untitled"}` |
| 95 | + : fileName || "Untitled"; |
| 96 | + |
| 97 | + return { |
| 98 | + id: `${fileId}-${eventId || ts}`, |
| 99 | + summary, |
| 100 | + ts, |
| 101 | + }; |
| 102 | + }, |
| 103 | + isFileRelevant(file) { |
| 104 | + return this.files.includes(file.id); |
| 105 | + }, |
| 106 | + getChanges(headers) { |
| 107 | + if (!headers) { |
| 108 | + return { |
| 109 | + change: { }, |
| 110 | + }; |
| 111 | + } |
| 112 | + return { |
| 113 | + change: { |
| 114 | + state: headers["x-goog-resource-state"], |
| 115 | + resourceURI: headers["x-goog-resource-uri"], |
| 116 | + changed: headers["x-goog-changed"], // "Additional details about the changes. Possible values: content, parents, children, permissions" |
| 117 | + }, |
| 118 | + }; |
| 119 | + }, |
| 120 | + async processChange(file, headers) { |
| 121 | + const changes = this.getChanges(headers); |
| 122 | + if (this.includeLink) { |
| 123 | + file.file = await stashFile(file, this.googleDrive, this.dir); |
| 124 | + } |
| 125 | + const eventToEmit = { |
| 126 | + file, |
| 127 | + ...changes, |
| 128 | + }; |
| 129 | + const meta = this.generateMeta(file, headers); |
| 130 | + this.$emit(eventToEmit, meta); |
| 131 | + }, |
| 132 | + async processChanges(changedFiles, headers) { |
| 133 | + console.log(`Processing ${changedFiles.length} changed files`); |
| 134 | + console.log(`Changed files: ${JSON.stringify(changedFiles, null, 2)}!!!`); |
| 135 | + console.log(`Files: ${this.files}!!!`); |
| 136 | + |
| 137 | + const filteredFiles = this.checkMinimumInterval(changedFiles); |
| 138 | + for (const file of filteredFiles) { |
| 139 | + if (!this.isFileRelevant(file)) { |
| 140 | + console.log(`Skipping event for irrelevant file ${file.id}`); |
| 141 | + continue; |
| 142 | + } |
| 143 | + await this.processChange(file, headers); |
| 144 | + } |
| 145 | + }, |
| 146 | + }, |
| 147 | +}; |
0 commit comments