Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to Drive API v3 #1854

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/appsscript.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"enabledAdvancedServices": [{
"userSymbol": "Drive",
"serviceId": "drive",
"version": "v2"
"version": "v3"
}]
},
"webapp": {
Expand Down
2 changes: 0 additions & 2 deletions src/backend/Drive-shim.d.ts

This file was deleted.

12 changes: 6 additions & 6 deletions src/backend/listFolders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export function listFolders(parentID: google.script.Parameter): ListResponse {
SafeFileList<{
id: true;
mimeType: true;
name: true;
shortcutDetails: { targetId: true };
title: true;
}>,
NamedRecord
>(
Expand All @@ -25,8 +25,8 @@ export function listFolders(parentID: google.script.Parameter): ListResponse {
{
id: true,
mimeType: true,
name: true,
shortcutDetails: { targetId: true },
title: true,
},
{
includeItemsFromAllDrives: true,
Expand All @@ -37,10 +37,10 @@ export function listFolders(parentID: google.script.Parameter): ListResponse {
},
),
(listResponse) =>
listResponse.items
listResponse.files
.sort((first, second) =>
first.title.localeCompare(
second.title,
first.name.localeCompare(
second.name,
Session.getActiveUserLocale(),
),
)
Expand All @@ -49,7 +49,7 @@ export function listFolders(parentID: google.script.Parameter): ListResponse {
item.mimeType === "application/vnd.google-apps.shortcut"
? (item.shortcutDetails?.targetId ?? item.id)
: item.id;
return { id, name: item.title };
return { id, name: item.name };
}),
);
return {
Expand Down
2 changes: 1 addition & 1 deletion src/backend/listSharedDrives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function listSharedDrives(): ListResponse {
},
),
(listResponse) =>
listResponse.items.map((item) => ({ id: item.id, name: item.name })),
listResponse.drives.map((item) => ({ id: item.id, name: item.name })),
);
return {
response,
Expand Down
61 changes: 45 additions & 16 deletions src/backend/move/copyFileComments.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DeepPick } from "../utils/DeepPick";
import type {
SafeComment,
SafeCommentList,
Expand All @@ -6,43 +7,71 @@ import type {

import { paginationHelper_ } from "../utils/paginationHelper";

interface CommentKeys {
anchor: true;
author: {
displayName: true;
me: true;
};
content: true;
quotedFileContent: true;
replies: true;
resolved: true;
}

export function copyFileComments_(
sourceID: string,
destinationID: string,
driveService: SafeDriveService_,
): void {
const comments = listFileComments_(sourceID, driveService);
for (const comment of comments) {
if (!comment.author.isAuthenticatedUser) {
if (!comment.author.me) {
comment.content = `*${comment.author.displayName}:*\n${comment.content}`;
}
const replies = comment.replies;
comment.replies = [];
const commentId = driveService.Comments.insert(
comment,
destinationID,
).commentId;
const commentId = driveService.Comments.create(comment, destinationID, {
id: true,
}).id;
for (const reply of replies) {
if (!reply.author.isAuthenticatedUser) {
if (!reply.author.me) {
reply.content = `*${reply.author.displayName}:*\n${reply.content}`;
}
driveService.Replies.insert(reply, destinationID, commentId);
driveService.Replies.create(reply, destinationID, commentId, {
fields: "id",
});
}
}
}

function listFileComments_(
fileID: string,
driveService: SafeDriveService_,
): Array<SafeComment> {
return paginationHelper_<SafeCommentList, SafeComment>(
): Array<DeepPick<SafeComment, CommentKeys>> {
return paginationHelper_<
SafeCommentList<CommentKeys>,
DeepPick<SafeComment, CommentKeys>
>(
(pageToken) =>
driveService.Comments.list(fileID, {
fields:
"nextPageToken, items(author(isAuthenticatedUser, displayName), content, status, context, anchor, replies(author(isAuthenticatedUser, displayName), content, verb))",
maxResults: 100,
pageToken,
}),
(response) => response.items,
driveService.Comments.list(
fileID,
{
anchor: true,
author: {
displayName: true,
me: true,
},
content: true,
quotedFileContent: true,
replies: true,
resolved: true,
},
{
maxResults: 100,
pageToken,
},
),
(response) => response.comments,
);
}
15 changes: 6 additions & 9 deletions src/backend/move/folderManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { paginationHelper_ } from "../utils/paginationHelper";
export interface ListFolderContentsFields {
capabilities: { canMoveItemOutOfDrive: true };
id: true;
title: true;
name: true;
}

export function deleteFolderIfEmpty_(
Expand All @@ -21,12 +21,9 @@ export function deleteFolderIfEmpty_(
return;
}
const response = driveService.Files.get(folderID, {
userPermission: { role: true },
capabilities: { canDelete: true },
});
if (
response.userPermission.role === "owner" ||
response.userPermission.role === "organizer"
) {
if (response.capabilities.canDelete) {
driveService.Files.remove(folderID);
}
}
Expand All @@ -44,7 +41,7 @@ export function isFolderEmpty_(
supportsAllDrives: true,
},
);
return response.items.length === 0;
return response.files.length === 0;
}

export function listFilesInFolder_(
Expand Down Expand Up @@ -83,7 +80,7 @@ function listFolderContents_(
{
capabilities: { canMoveItemOutOfDrive: true },
id: true,
title: true,
name: true,
},
{
includeItemsFromAllDrives: true,
Expand All @@ -93,6 +90,6 @@ function listFolderContents_(
supportsAllDrives: true,
},
),
(response) => response.items,
(response) => response.files,
);
}
9 changes: 4 additions & 5 deletions src/backend/move/moveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function moveFile_(
}
moveFileByCopy_(
file.id,
file.title,
file.name,
state,
context,
copyComments,
Expand All @@ -42,8 +42,8 @@ function moveFileByCopy_(
() => {
const copy = driveService.Files.copy(
{
parents: [{ id: context.destinationID }],
title: name,
name,
parents: [context.destinationID],
},
fileID,
{ id: true },
Expand All @@ -62,9 +62,8 @@ function moveFileDirectly_(
context: MoveContext,
driveService: SafeDriveService_,
): void {
driveService.Files.update({}, fileID, null, {
driveService.Files.update({}, fileID, {}, undefined, {
addParents: context.destinationID,
fields: "",
removeParents: context.sourceID,
supportsAllDrives: true,
});
Expand Down
2 changes: 1 addition & 1 deletion src/backend/move/moveFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function moveFolder_(
state.addPath(
folder.id,
destinationFolder.id,
context.path.concat([folder.title]),
context.path.concat([folder.name]),
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/backend/move/resolveDestinationFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ export function resolveDestinationFolder_(
const existingFoldersWithSameName = listFoldersInFolder_(
context.destinationID,
driveService,
).filter((folder) => folder.title === sourceFolder.title);
).filter((folder) => folder.name === sourceFolder.name);
if (existingFoldersWithSameName.length === 1) {
return existingFoldersWithSameName[0];
}
if (existingFoldersWithSameName.length > 1) {
state.logError(
context.path.concat([sourceFolder.title]),
context.path.concat([sourceFolder.name]),
"Coudn't merge with existing folder as there are multiple existing directories with the same name",
);
}
}
return driveService.Files.insert(
return driveService.Files.create(
{
mimeType: "application/vnd.google-apps.folder",
parents: [{ id: context.destinationID }],
title: sourceFolder.title,
name: sourceFolder.name,
parents: [context.destinationID],
},
{ id: true },
undefined,
Expand Down
28 changes: 14 additions & 14 deletions src/backend/utils/DriveBackedValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export class DriveBackedValue_<T> {
}

private createDriveFolder(): string {
const response = this.driveService.Files.insert(
const response = this.driveService.Files.create(
{
mimeType: "application/vnd.google-apps.folder",
title: DriveBackedValue_.driveFolderName,
name: DriveBackedValue_.driveFolderName,
},
{
id: true,
Expand Down Expand Up @@ -85,14 +85,14 @@ export class DriveBackedValue_<T> {
{ id: true },
{
maxResults: 1,
q: `title = "${this.getFileName()}" and "${folderId}" in parents and trashed = false`,
q: `name = "${this.getFileName()}" and "${folderId}" in parents and trashed = false`,
},
);
if (
response.items.length === 1 &&
typeof response.items[0].id === "string"
response.files.length === 1 &&
typeof response.files[0].id === "string"
) {
return response.items[0].id;
return response.files[0].id;
}
return null;
}
Expand All @@ -102,14 +102,14 @@ export class DriveBackedValue_<T> {
{ id: true },
{
maxResults: 1,
q: `title = "${DriveBackedValue_.driveFolderName}" and "root" in parents and mimeType = "application/vnd.google-apps.folder" and trashed = false`,
q: `name = "${DriveBackedValue_.driveFolderName}" and "root" in parents and mimeType = "application/vnd.google-apps.folder" and trashed = false`,
},
);
if (
response.items.length === 1 &&
typeof response.items[0].id === "string"
response.files.length === 1 &&
typeof response.files[0].id === "string"
) {
return response.items[0].id;
return response.files[0].id;
}
return null;
}
Expand All @@ -126,15 +126,15 @@ export class DriveBackedValue_<T> {
q: `"${folderId}" in parents and trashed = false`,
},
);
return response.items.length === 0;
return response.files.length === 0;
}

private saveAsNewDriveFile(folderId: string, value: T): void {
this.driveService.Files.insert(
this.driveService.Files.create(
{
mimeType: "application/json",
parents: [{ id: folderId }],
title: this.getFileName(),
name: this.getFileName(),
parents: [folderId],
},
{},
Utilities.newBlob(JSON.stringify(value), "application/json"),
Expand Down
17 changes: 7 additions & 10 deletions src/backend/utils/SafeDriveService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ export type {
} from "./SafeDriveService/SafeFilesCollection";

export class SafeDriveService_ {
public readonly Comments: SafeCommentsCollection_;
public readonly Drives: SafeDrivesCollection_;
public readonly Files: SafeFilesCollection_;
public readonly Replies: GoogleAppsScript.Drive.Collection.RepliesCollection;
public readonly Comments: typeof SafeCommentsCollection_;
public readonly Drives: typeof SafeDrivesCollection_;
public readonly Files: typeof SafeFilesCollection_;
public readonly Replies: GoogleAppsScript.Drive_v3.Drive.V3.Collection.RepliesCollection;

public constructor() {
if (Drive.Replies === undefined) {
throw new Error();
}
this.Comments = new SafeCommentsCollection_();
this.Drives = new SafeDrivesCollection_();
this.Files = new SafeFilesCollection_();
this.Comments = SafeCommentsCollection_;
this.Drives = SafeDrivesCollection_;
this.Files = SafeFilesCollection_;
this.Replies = Drive.Replies;
}
}
Loading
Loading