diff --git a/components/google_sheets/package.json b/components/google_sheets/package.json index ae17a3c4cb99c..c9352fe50c4d4 100644 --- a/components/google_sheets/package.json +++ b/components/google_sheets/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/google_sheets", - "version": "0.9.0", + "version": "0.9.1", "description": "Pipedream Google_sheets Components", "main": "google_sheets.app.mjs", "keywords": [ @@ -11,7 +11,7 @@ "author": "Pipedream (https://pipedream.com/)", "dependencies": { "@googleapis/sheets": "^0.3.0", - "@pipedream/google_drive": "^1.1.0", + "@pipedream/google_drive": "^1.1.1", "@pipedream/platform": "^3.1.0", "lodash": "^4.17.21", "uuidv4": "^6.2.6", diff --git a/components/google_sheets/sources/common/http-based/base.mjs b/components/google_sheets/sources/common/http-based/base.mjs index 984ffb1572b08..267a5132b7c1c 100644 --- a/components/google_sheets/sources/common/http-based/base.mjs +++ b/components/google_sheets/sources/common/http-based/base.mjs @@ -31,7 +31,10 @@ export default { props: { googleSheets, db: "$.service.db", - http: "$.interface.http", + http: { + type: "$.interface.http", + customResponse: true, + }, timer: { label: "Push notification renewal schedule", description: diff --git a/components/google_sheets/sources/common/http-based/drive.mjs b/components/google_sheets/sources/common/http-based/drive.mjs index e6c7e9957a8d1..81b31d5a1ad27 100644 --- a/components/google_sheets/sources/common/http-based/drive.mjs +++ b/components/google_sheets/sources/common/http-based/drive.mjs @@ -135,6 +135,10 @@ export default { return; } + this.http.respond({ + status: 200, + }); + const spreadsheet = await this.getSpreadsheetToProcess(event); if (!spreadsheet) { diff --git a/components/google_sheets/sources/common/http-based/sheet.mjs b/components/google_sheets/sources/common/http-based/sheet.mjs index 0b1c5ac37dbac..4592543425443 100644 --- a/components/google_sheets/sources/common/http-based/sheet.mjs +++ b/components/google_sheets/sources/common/http-based/sheet.mjs @@ -5,15 +5,39 @@ import { v4 as uuid } from "uuid"; import base from "./base.mjs"; +import drive from "./drive.mjs"; /** * This source watches for changes to a specific spreadsheet in the user's Google Drive. */ export default { ...base, + props: { + ...base.props, + watchDrive: { + type: "boolean", + label: "Watch Drive", + description: "Set to `true` to watch the drive for changes. May reduce rate limiting.", + optional: true, + }, + }, methods: { + ...drive.methods, ...base.methods, + _getChangeToken() { + return this.db.get("changeToken"); + }, + _setChangeToken(changeToken) { + this.db.set("changeToken", changeToken); + }, async activateHook(channelID) { + if (this.watchDrive) { + return this.googleSheets.activateHook( + channelID, + this.http.endpoint, + this.googleSheets.getDriveId(this.watchedDrive), + ); + } return this.googleSheets.activateFileHook( channelID, this.http.endpoint, @@ -27,6 +51,28 @@ export default { .filter(({ sheetType }) => sheetType === "GRID") .map(({ sheetId }) => (sheetId.toString())); }, + async isSheetRelevant() { + const pageToken = this._getChangeToken() || this._getPageToken(); + const drive = this.googleSheets.drive(); + const params = { + pageToken, + }; + const driveId = this.getDriveId(this.watchedDrive); + if (driveId) { + Object.assign(params, { + driveId, + supportsAllDrives: true, + includeItemsFromAllDrives: true, + corpora: "drive", + }); + } + const { data } = await drive.changes.list(params); + const { + changes, newStartPageToken, + } = data; + this._setChangeToken(newStartPageToken); + return changes.some((change) => change.file.id === this.getSheetId()); + }, async renewSubscription() { // Assume subscription & channelID may all be undefined at // this point Handle their absence appropriately. @@ -51,10 +97,37 @@ export default { }); this._setChannelID(newChannelID); }, + async renewDriveSubscription() { + const subscription = this._getSubscription(); + const channelID = this._getChannelID() || uuid(); + + const { + expiration, + resourceId, + newChannelID, + newPageToken, + } = await this.googleSheets.renewSubscription( + this.watchedDrive, + subscription, + this.http.endpoint, + channelID, + this._getPageToken(), + ); + + this._setSubscription({ + expiration, + resourceId, + }); + this._setChannelID(newChannelID); + this._setPageToken(newPageToken); + }, }, async run(event) { if (event.timestamp) { // Component was invoked by timer + if (this.watchDrive) { + return this.renewDriveSubscription(); + } return this.renewSubscription(); } @@ -63,6 +136,15 @@ export default { return; } + this.http.respond({ + status: 200, + }); + + if (this.watchDrive && !(await this.isSheetRelevant(event))) { + console.log("Change to unwatched file, exiting early"); + return; + } + const spreadsheet = await this.googleSheets.getSpreadsheet(this.sheetID); return this.processSpreadsheet(spreadsheet); diff --git a/components/google_sheets/sources/new-comment/new-comment.mjs b/components/google_sheets/sources/new-comment/new-comment.mjs index f02d5c80d683a..1b587baaa0f48 100644 --- a/components/google_sheets/sources/new-comment/new-comment.mjs +++ b/components/google_sheets/sources/new-comment/new-comment.mjs @@ -6,7 +6,7 @@ export default { key: "google_sheets-new-comment", name: "New Comment (Instant)", description: "Emit new event each time a comment is added to a spreadsheet.", - version: "0.1.1", + version: "0.1.2", dedupe: "unique", type: "source", methods: { diff --git a/components/google_sheets/sources/new-row-added-polling/new-row-added-polling.mjs b/components/google_sheets/sources/new-row-added-polling/new-row-added-polling.mjs index c4bd18cb86377..d583f22c6a833 100644 --- a/components/google_sheets/sources/new-row-added-polling/new-row-added-polling.mjs +++ b/components/google_sheets/sources/new-row-added-polling/new-row-added-polling.mjs @@ -8,7 +8,7 @@ export default { key: "google_sheets-new-row-added-polling", name: "New Row Added", description: "Emit new event each time a row or rows are added to the bottom of a spreadsheet.", - version: "0.1.1", + version: "0.1.2", dedupe: "unique", type: "source", props: { diff --git a/components/google_sheets/sources/new-row-added/new-row-added.mjs b/components/google_sheets/sources/new-row-added/new-row-added.mjs index 9abda1dd75782..8909266273a07 100644 --- a/components/google_sheets/sources/new-row-added/new-row-added.mjs +++ b/components/google_sheets/sources/new-row-added/new-row-added.mjs @@ -8,7 +8,7 @@ export default { key: "google_sheets-new-row-added", name: "New Row Added (Instant)", description: "Emit new event each time a row or rows are added to the bottom of a spreadsheet.", - version: "0.2.1", + version: "0.2.2", dedupe: "unique", type: "source", props: { diff --git a/components/google_sheets/sources/new-updates/new-updates.mjs b/components/google_sheets/sources/new-updates/new-updates.mjs index 9ad4b0a4831e2..1ac14eb9fa186 100644 --- a/components/google_sheets/sources/new-updates/new-updates.mjs +++ b/components/google_sheets/sources/new-updates/new-updates.mjs @@ -9,7 +9,7 @@ export default { type: "source", name: "New Updates (Instant)", description: "Emit new event each time a row or cell is updated in a spreadsheet.", - version: "0.3.1", + version: "0.3.2", dedupe: "unique", props: { ...httpBase.props, diff --git a/components/google_sheets/sources/new-worksheet/new-worksheet.mjs b/components/google_sheets/sources/new-worksheet/new-worksheet.mjs index 07ad0c133c292..0b7b7cc09f3b0 100644 --- a/components/google_sheets/sources/new-worksheet/new-worksheet.mjs +++ b/components/google_sheets/sources/new-worksheet/new-worksheet.mjs @@ -9,7 +9,7 @@ export default { type: "source", name: "New Worksheet (Instant)", description: "Emit new event each time a new worksheet is created in a spreadsheet.", - version: "0.2.1", + version: "0.2.2", dedupe: "unique", hooks: { ...httpBase.hooks, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f686711b10b3e..695585dda232b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6098,8 +6098,8 @@ importers: specifier: ^0.3.0 version: 0.3.0 '@pipedream/google_drive': - specifier: ^1.1.0 - version: 1.1.0 + specifier: ^1.1.1 + version: 1.1.1 '@pipedream/platform': specifier: ^3.1.0 version: 3.1.0 @@ -20426,8 +20426,8 @@ packages: '@pipedream/google_drive@1.0.3': resolution: {integrity: sha512-E7MF0E3f/RRyL5PsLaoL51IZtdoyWKNpUIT7RIpQDJ2wh00H0hrISfgugfKAVdv7Tl//BvJezu1FecSLTxhroQ==} - '@pipedream/google_drive@1.1.0': - resolution: {integrity: sha512-ZiLlhIrJc7iUK7jLaQaRj4sf/9VUi6ZyZAGhYd2CfoZ8J7ys1KA9Rxi7oUFZvdKsH0AyJIHTIokCZbcuubS0Pw==} + '@pipedream/google_drive@1.1.1': + resolution: {integrity: sha512-amUo+NYuBMnXlJoIAIjw8vuVhGF4AB8LuLn5G2oq4lgU1Xt0Fqot8WGIh1Ahwx16q1RqKq+4DDdV9jROjm9rSA==} '@pipedream/helper_functions@0.3.13': resolution: {integrity: sha512-67zXT5rMccSYEeQCEKjO0g/U2Wnc3q1ErRWt0sivqJEF/KYqAb3tWmznDYGACbC8L6ML0WWSWRy7nEGt5Vyuaw==} @@ -39144,7 +39144,7 @@ snapshots: - encoding - supports-color - '@pipedream/google_drive@1.1.0': + '@pipedream/google_drive@1.1.1': dependencies: '@googleapis/drive': 2.4.0 '@pipedream/platform': 3.1.0