Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 4e0e2b4

Browse files
authored
Merge pull request #1854 from skaut/drive-v3
Updated to Drive API v3
2 parents fd739fc + 186b2dc commit 4e0e2b4

28 files changed

+1416
-1300
lines changed

src/appsscript.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"enabledAdvancedServices": [{
66
"userSymbol": "Drive",
77
"serviceId": "drive",
8-
"version": "v2"
8+
"version": "v3"
99
}]
1010
},
1111
"webapp": {

src/backend/Drive-shim.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/backend/listFolders.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export function listFolders(parentID: google.script.Parameter): ListResponse {
1515
SafeFileList<{
1616
id: true;
1717
mimeType: true;
18+
name: true;
1819
shortcutDetails: { targetId: true };
19-
title: true;
2020
}>,
2121
NamedRecord
2222
>(
@@ -25,8 +25,8 @@ export function listFolders(parentID: google.script.Parameter): ListResponse {
2525
{
2626
id: true,
2727
mimeType: true,
28+
name: true,
2829
shortcutDetails: { targetId: true },
29-
title: true,
3030
},
3131
{
3232
includeItemsFromAllDrives: true,
@@ -37,10 +37,10 @@ export function listFolders(parentID: google.script.Parameter): ListResponse {
3737
},
3838
),
3939
(listResponse) =>
40-
listResponse.items
40+
listResponse.files
4141
.sort((first, second) =>
42-
first.title.localeCompare(
43-
second.title,
42+
first.name.localeCompare(
43+
second.name,
4444
Session.getActiveUserLocale(),
4545
),
4646
)
@@ -49,7 +49,7 @@ export function listFolders(parentID: google.script.Parameter): ListResponse {
4949
item.mimeType === "application/vnd.google-apps.shortcut"
5050
? (item.shortcutDetails?.targetId ?? item.id)
5151
: item.id;
52-
return { id, name: item.title };
52+
return { id, name: item.name };
5353
}),
5454
);
5555
return {

src/backend/listSharedDrives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function listSharedDrives(): ListResponse {
2525
},
2626
),
2727
(listResponse) =>
28-
listResponse.items.map((item) => ({ id: item.id, name: item.name })),
28+
listResponse.drives.map((item) => ({ id: item.id, name: item.name })),
2929
);
3030
return {
3131
response,
Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { DeepPick } from "../utils/DeepPick";
12
import type {
23
SafeComment,
34
SafeCommentList,
@@ -6,43 +7,71 @@ import type {
67

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

10+
interface CommentKeys {
11+
anchor: true;
12+
author: {
13+
displayName: true;
14+
me: true;
15+
};
16+
content: true;
17+
quotedFileContent: true;
18+
replies: true;
19+
resolved: true;
20+
}
21+
922
export function copyFileComments_(
1023
sourceID: string,
1124
destinationID: string,
1225
driveService: SafeDriveService_,
1326
): void {
1427
const comments = listFileComments_(sourceID, driveService);
1528
for (const comment of comments) {
16-
if (!comment.author.isAuthenticatedUser) {
29+
if (!comment.author.me) {
1730
comment.content = `*${comment.author.displayName}:*\n${comment.content}`;
1831
}
1932
const replies = comment.replies;
2033
comment.replies = [];
21-
const commentId = driveService.Comments.insert(
22-
comment,
23-
destinationID,
24-
).commentId;
34+
const commentId = driveService.Comments.create(comment, destinationID, {
35+
id: true,
36+
}).id;
2537
for (const reply of replies) {
26-
if (!reply.author.isAuthenticatedUser) {
38+
if (!reply.author.me) {
2739
reply.content = `*${reply.author.displayName}:*\n${reply.content}`;
2840
}
29-
driveService.Replies.insert(reply, destinationID, commentId);
41+
driveService.Replies.create(reply, destinationID, commentId, {
42+
fields: "id",
43+
});
3044
}
3145
}
3246
}
3347

3448
function listFileComments_(
3549
fileID: string,
3650
driveService: SafeDriveService_,
37-
): Array<SafeComment> {
38-
return paginationHelper_<SafeCommentList, SafeComment>(
51+
): Array<DeepPick<SafeComment, CommentKeys>> {
52+
return paginationHelper_<
53+
SafeCommentList<CommentKeys>,
54+
DeepPick<SafeComment, CommentKeys>
55+
>(
3956
(pageToken) =>
40-
driveService.Comments.list(fileID, {
41-
fields:
42-
"nextPageToken, items(author(isAuthenticatedUser, displayName), content, status, context, anchor, replies(author(isAuthenticatedUser, displayName), content, verb))",
43-
maxResults: 100,
44-
pageToken,
45-
}),
46-
(response) => response.items,
57+
driveService.Comments.list(
58+
fileID,
59+
{
60+
anchor: true,
61+
author: {
62+
displayName: true,
63+
me: true,
64+
},
65+
content: true,
66+
quotedFileContent: true,
67+
replies: true,
68+
resolved: true,
69+
},
70+
{
71+
maxResults: 100,
72+
pageToken,
73+
},
74+
),
75+
(response) => response.comments,
4776
);
4877
}

src/backend/move/folderManagement.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { paginationHelper_ } from "../utils/paginationHelper";
1010
export interface ListFolderContentsFields {
1111
capabilities: { canMoveItemOutOfDrive: true };
1212
id: true;
13-
title: true;
13+
name: true;
1414
}
1515

1616
export function deleteFolderIfEmpty_(
@@ -21,12 +21,9 @@ export function deleteFolderIfEmpty_(
2121
return;
2222
}
2323
const response = driveService.Files.get(folderID, {
24-
userPermission: { role: true },
24+
capabilities: { canDelete: true },
2525
});
26-
if (
27-
response.userPermission.role === "owner" ||
28-
response.userPermission.role === "organizer"
29-
) {
26+
if (response.capabilities.canDelete) {
3027
driveService.Files.remove(folderID);
3128
}
3229
}
@@ -44,7 +41,7 @@ export function isFolderEmpty_(
4441
supportsAllDrives: true,
4542
},
4643
);
47-
return response.items.length === 0;
44+
return response.files.length === 0;
4845
}
4946

5047
export function listFilesInFolder_(
@@ -83,7 +80,7 @@ function listFolderContents_(
8380
{
8481
capabilities: { canMoveItemOutOfDrive: true },
8582
id: true,
86-
title: true,
83+
name: true,
8784
},
8885
{
8986
includeItemsFromAllDrives: true,
@@ -93,6 +90,6 @@ function listFolderContents_(
9390
supportsAllDrives: true,
9491
},
9592
),
96-
(response) => response.items,
93+
(response) => response.files,
9794
);
9895
}

src/backend/move/moveFile.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function moveFile_(
2121
}
2222
moveFileByCopy_(
2323
file.id,
24-
file.title,
24+
file.name,
2525
state,
2626
context,
2727
copyComments,
@@ -42,8 +42,8 @@ function moveFileByCopy_(
4242
() => {
4343
const copy = driveService.Files.copy(
4444
{
45-
parents: [{ id: context.destinationID }],
46-
title: name,
45+
name,
46+
parents: [context.destinationID],
4747
},
4848
fileID,
4949
{ id: true },
@@ -62,9 +62,8 @@ function moveFileDirectly_(
6262
context: MoveContext,
6363
driveService: SafeDriveService_,
6464
): void {
65-
driveService.Files.update({}, fileID, null, {
65+
driveService.Files.update({}, fileID, {}, undefined, {
6666
addParents: context.destinationID,
67-
fields: "",
6867
removeParents: context.sourceID,
6968
supportsAllDrives: true,
7069
});

src/backend/move/moveFolder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function moveFolder_(
2929
state.addPath(
3030
folder.id,
3131
destinationFolder.id,
32-
context.path.concat([folder.title]),
32+
context.path.concat([folder.name]),
3333
);
3434
}
3535
}

src/backend/move/resolveDestinationFolder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ export function resolveDestinationFolder_(
1919
const existingFoldersWithSameName = listFoldersInFolder_(
2020
context.destinationID,
2121
driveService,
22-
).filter((folder) => folder.title === sourceFolder.title);
22+
).filter((folder) => folder.name === sourceFolder.name);
2323
if (existingFoldersWithSameName.length === 1) {
2424
return existingFoldersWithSameName[0];
2525
}
2626
if (existingFoldersWithSameName.length > 1) {
2727
state.logError(
28-
context.path.concat([sourceFolder.title]),
28+
context.path.concat([sourceFolder.name]),
2929
"Coudn't merge with existing folder as there are multiple existing directories with the same name",
3030
);
3131
}
3232
}
33-
return driveService.Files.insert(
33+
return driveService.Files.create(
3434
{
3535
mimeType: "application/vnd.google-apps.folder",
36-
parents: [{ id: context.destinationID }],
37-
title: sourceFolder.title,
36+
name: sourceFolder.name,
37+
parents: [context.destinationID],
3838
},
3939
{ id: true },
4040
undefined,

src/backend/utils/DriveBackedValue.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ export class DriveBackedValue_<T> {
5050
}
5151

5252
private createDriveFolder(): string {
53-
const response = this.driveService.Files.insert(
53+
const response = this.driveService.Files.create(
5454
{
5555
mimeType: "application/vnd.google-apps.folder",
56-
title: DriveBackedValue_.driveFolderName,
56+
name: DriveBackedValue_.driveFolderName,
5757
},
5858
{
5959
id: true,
@@ -85,14 +85,14 @@ export class DriveBackedValue_<T> {
8585
{ id: true },
8686
{
8787
maxResults: 1,
88-
q: `title = "${this.getFileName()}" and "${folderId}" in parents and trashed = false`,
88+
q: `name = "${this.getFileName()}" and "${folderId}" in parents and trashed = false`,
8989
},
9090
);
9191
if (
92-
response.items.length === 1 &&
93-
typeof response.items[0].id === "string"
92+
response.files.length === 1 &&
93+
typeof response.files[0].id === "string"
9494
) {
95-
return response.items[0].id;
95+
return response.files[0].id;
9696
}
9797
return null;
9898
}
@@ -102,14 +102,14 @@ export class DriveBackedValue_<T> {
102102
{ id: true },
103103
{
104104
maxResults: 1,
105-
q: `title = "${DriveBackedValue_.driveFolderName}" and "root" in parents and mimeType = "application/vnd.google-apps.folder" and trashed = false`,
105+
q: `name = "${DriveBackedValue_.driveFolderName}" and "root" in parents and mimeType = "application/vnd.google-apps.folder" and trashed = false`,
106106
},
107107
);
108108
if (
109-
response.items.length === 1 &&
110-
typeof response.items[0].id === "string"
109+
response.files.length === 1 &&
110+
typeof response.files[0].id === "string"
111111
) {
112-
return response.items[0].id;
112+
return response.files[0].id;
113113
}
114114
return null;
115115
}
@@ -126,15 +126,15 @@ export class DriveBackedValue_<T> {
126126
q: `"${folderId}" in parents and trashed = false`,
127127
},
128128
);
129-
return response.items.length === 0;
129+
return response.files.length === 0;
130130
}
131131

132132
private saveAsNewDriveFile(folderId: string, value: T): void {
133-
this.driveService.Files.insert(
133+
this.driveService.Files.create(
134134
{
135135
mimeType: "application/json",
136-
parents: [{ id: folderId }],
137-
title: this.getFileName(),
136+
name: this.getFileName(),
137+
parents: [folderId],
138138
},
139139
{},
140140
Utilities.newBlob(JSON.stringify(value), "application/json"),

0 commit comments

Comments
 (0)