Skip to content

Commit 0afd8ac

Browse files
committed
add optional watchDrive prop, updates
1 parent d275fd7 commit 0afd8ac

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

components/google_sheets/sources/common/http-based/base.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export default {
3131
props: {
3232
googleSheets,
3333
db: "$.service.db",
34-
http: "$.interface.http",
34+
http: {
35+
type: "$.interface.http",
36+
customResponse: true,
37+
},
3538
timer: {
3639
label: "Push notification renewal schedule",
3740
description:

components/google_sheets/sources/common/http-based/drive.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ export default {
135135
return;
136136
}
137137

138+
this.http.respond({
139+
status: 200,
140+
});
141+
138142
const spreadsheet = await this.getSpreadsheetToProcess(event);
139143

140144
if (!spreadsheet) {

components/google_sheets/sources/common/http-based/sheet.mjs

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,36 @@ import drive from "./drive.mjs";
1212
*/
1313
export default {
1414
...base,
15+
props: {
16+
...base.props,
17+
watchDrive: {
18+
type: "boolean",
19+
label: "Watch Drive",
20+
description: "Set to `true` to watch the drive for changes. May reduce rate limiting.",
21+
optional: true,
22+
},
23+
},
1524
methods: {
1625
...drive.methods,
1726
...base.methods,
27+
_getChangeToken() {
28+
return this.db.get("changeToken");
29+
},
30+
_setChangeToken(changeToken) {
31+
this.db.set("changeToken", changeToken);
32+
},
1833
async activateHook(channelID) {
19-
return this.googleSheets.activateHook(
34+
if (this.watchDrive) {
35+
return this.googleSheets.activateHook(
36+
channelID,
37+
this.http.endpoint,
38+
this.googleSheets.getDriveId(this.watchedDrive),
39+
);
40+
}
41+
return this.googleSheets.activateFileHook(
2042
channelID,
2143
this.http.endpoint,
22-
this.googleSheets.getDriveId(this.watchedDrive),
44+
this.getSheetId(),
2345
);
2446
},
2547
async getAllWorksheetIds(sheetID) {
@@ -29,6 +51,19 @@ export default {
2951
.filter(({ sheetType }) => sheetType === "GRID")
3052
.map(({ sheetId }) => (sheetId.toString()));
3153
},
54+
async isSheetRelevant() {
55+
const pageToken = this._getChangeToken() || this._getPageToken();
56+
const drive = this.googleSheets.drive();
57+
const { data } = await drive.changes.list({
58+
pageToken,
59+
driveId: this.googleSheets.getDriveId(this.watchedDrive),
60+
});
61+
const {
62+
changes, newStartPageToken,
63+
} = data;
64+
this._setChangeToken(newStartPageToken);
65+
return changes.some((change) => change.file.id === this.getSheetId());
66+
},
3267
async renewSubscription() {
3368
// Assume subscription & channelID may all be undefined at
3469
// this point Handle their absence appropriately.
@@ -53,10 +88,37 @@ export default {
5388
});
5489
this._setChannelID(newChannelID);
5590
},
91+
async renewDriveSubscription() {
92+
const subscription = this._getSubscription();
93+
const channelID = this._getChannelID() || uuid();
94+
95+
const {
96+
expiration,
97+
resourceId,
98+
newChannelID,
99+
newPageToken,
100+
} = await this.googleSheets.renewSubscription(
101+
this.watchedDrive,
102+
subscription,
103+
this.http.endpoint,
104+
channelID,
105+
this._getPageToken(),
106+
);
107+
108+
this._setSubscription({
109+
expiration,
110+
resourceId,
111+
});
112+
this._setChannelID(newChannelID);
113+
this._setPageToken(newPageToken);
114+
},
56115
},
57116
async run(event) {
58117
if (event.timestamp) {
59118
// Component was invoked by timer
119+
if (this.watchDrive) {
120+
return this.renewDriveSubscription();
121+
}
60122
return this.renewSubscription();
61123
}
62124

@@ -65,6 +127,15 @@ export default {
65127
return;
66128
}
67129

130+
this.http.respond({
131+
status: 200,
132+
});
133+
134+
if (this.watchDrive && !(await this.isSheetRelevant(event))) {
135+
console.log("Change to unwatched file, exiting early");
136+
return;
137+
}
138+
68139
const spreadsheet = await this.googleSheets.getSpreadsheet(this.sheetID);
69140

70141
return this.processSpreadsheet(spreadsheet);

0 commit comments

Comments
 (0)