import common from "../common-webhook.mjs";
import {
GOOGLE_DRIVE_NOTIFICATION_ADD,
GOOGLE_DRIVE_NOTIFICATION_CHANGE,
GOOGLE_DRIVE_NOTIFICATION_UPDATE,
} from "../../constants.mjs";
export default {
...common,
key: "google_drive-new-or-modified-files",
name: "New or Modified Files",
description: "Emit new event any time any file in your linked Google Drive is added, modified, or deleted",
version: "0.1.1",
type: "source",
dedupe: "unique",
hooks: {
async deploy() {
const daysAgo = new Date();
daysAgo.setDate(daysAgo.getDate() - 30);
const timeString = daysAgo.toISOString();
const { data } = await this.googleDrive.drive().files.list({
q: `mimeType != "application/vnd.google-apps.folder" and modifiedTime > "${timeString}" and trashed = false`,
fields: "files",
});
await this.processChanges(data.files);
},
...common.hooks,
},
methods: {
...common.methods,
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;
}
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"],
},
};
},
async processChanges(changedFiles, headers) {
const changes = await this.getChanges(headers);
for (const file of changedFiles) {
const eventToEmit = {
file,
...changes,
};
const meta = this.generateMeta(file, headers);
this.$emit(eventToEmit, meta);
}
},
},
};