Skip to content

Commit 053e45e

Browse files
committed
Not wrapping now safe Drive collections
1 parent ca9e834 commit 053e45e

File tree

7 files changed

+184
-304
lines changed

7 files changed

+184
-304
lines changed

src/backend/utils/SafeDriveService.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ export type {
1616
} from "./SafeDriveService/SafeFilesCollection";
1717

1818
export class SafeDriveService_ {
19-
public readonly Comments: SafeCommentsCollection_;
20-
public readonly Drives: SafeDrivesCollection_;
21-
public readonly Files: SafeFilesCollection_;
19+
public readonly Comments: typeof SafeCommentsCollection_;
20+
public readonly Drives: typeof SafeDrivesCollection_;
21+
public readonly Files: typeof SafeFilesCollection_;
2222
public readonly Replies: GoogleAppsScript.Drive_v3.Drive.V3.Collection.RepliesCollection;
2323

2424
public constructor() {
25-
this.Comments = new SafeCommentsCollection_();
26-
this.Drives = new SafeDrivesCollection_();
27-
this.Files = new SafeFilesCollection_();
25+
this.Comments = SafeCommentsCollection_;
26+
this.Drives = SafeDrivesCollection_;
27+
this.Files = SafeFilesCollection_;
2828
this.Replies = Drive.Replies;
2929
}
3030
}

src/backend/utils/SafeDriveService/SafeCommentsCollection.ts

Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,77 +20,66 @@ interface SafeUser {
2020
me: boolean;
2121
}
2222

23-
export class SafeCommentsCollection_ {
24-
private readonly unsafeComments: GoogleAppsScript.Drive_v3.Drive.V3.Collection.CommentsCollection;
25-
26-
public constructor() {
27-
// TODO: Remove and access directly
28-
this.unsafeComments = Drive.Comments;
29-
}
30-
31-
private static commentIsSafe(
32-
comment: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment,
33-
): comment is SafeComment {
34-
return (
35-
comment.author !== undefined &&
36-
SafeCommentsCollection_.userIsSafe(comment.author) &&
37-
comment.id !== undefined &&
38-
comment.content !== undefined &&
39-
comment.replies?.every((reply) =>
40-
SafeCommentsCollection_.commentReplyIsSafe(reply),
41-
) === true
42-
);
43-
}
23+
function commentIsSafe_(
24+
comment: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment,
25+
): comment is SafeComment {
26+
return (
27+
comment.author !== undefined &&
28+
userIsSafe_(comment.author) &&
29+
comment.id !== undefined &&
30+
comment.content !== undefined &&
31+
comment.replies?.every((reply) => commentReplyIsSafe_(reply)) === true
32+
);
33+
}
4434

45-
private static commentListIsSafe(
46-
commentList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.CommentList,
47-
): commentList is SafeCommentList {
48-
return (
49-
commentList.comments?.every((comment) =>
50-
SafeCommentsCollection_.commentIsSafe(comment),
51-
) === true
52-
);
53-
}
35+
function commentListIsSafe_(
36+
commentList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.CommentList,
37+
): commentList is SafeCommentList {
38+
return (
39+
commentList.comments?.every((comment) => commentIsSafe_(comment)) === true
40+
);
41+
}
5442

55-
private static commentReplyIsSafe(
56-
commentReply: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Reply,
57-
): commentReply is SafeReply {
58-
return (
59-
commentReply.author !== undefined &&
60-
SafeCommentsCollection_.userIsSafe(commentReply.author) &&
61-
commentReply.content !== undefined
62-
);
63-
}
43+
function commentReplyIsSafe_(
44+
commentReply: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Reply,
45+
): commentReply is SafeReply {
46+
return (
47+
commentReply.author !== undefined &&
48+
userIsSafe_(commentReply.author) &&
49+
commentReply.content !== undefined
50+
);
51+
}
6452

65-
private static userIsSafe(
66-
user: GoogleAppsScript.Drive_v3.Drive.V3.Schema.User,
67-
): user is SafeUser {
68-
return user.me !== undefined && user.displayName !== undefined;
69-
}
53+
function userIsSafe_(
54+
user: GoogleAppsScript.Drive_v3.Drive.V3.Schema.User,
55+
): user is SafeUser {
56+
return user.me !== undefined && user.displayName !== undefined;
57+
}
7058

71-
public create(
59+
export const SafeCommentsCollection_ = {
60+
create: (
7261
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment,
7362
fileId: string,
74-
): SafeComment {
75-
const ret = this.unsafeComments.create(resource, fileId);
76-
if (!SafeCommentsCollection_.commentIsSafe(ret)) {
63+
): SafeComment => {
64+
const ret = Drive.Comments.create(resource, fileId);
65+
if (!commentIsSafe_(ret)) {
7766
throw new Error("");
7867
}
7968
return ret;
80-
}
69+
},
8170

82-
public list(
71+
list: (
8372
fileId: string,
8473
optionalArgs: {
8574
fields?: string;
8675
maxResults?: number;
8776
pageToken?: string | undefined;
8877
} = {},
89-
): SafeCommentList {
90-
const ret = this.unsafeComments.list(fileId, optionalArgs);
91-
if (!SafeCommentsCollection_.commentListIsSafe(ret)) {
78+
): SafeCommentList => {
79+
const ret = Drive.Comments.list(fileId, optionalArgs);
80+
if (!commentListIsSafe_(ret)) {
9281
throw new Error("");
9382
}
9483
return ret;
95-
}
96-
}
84+
},
85+
};

src/backend/utils/SafeDriveService/SafeDrivesCollection.ts

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,60 +18,49 @@ const safeDriveKeys: DeepKeyof<SafeDrive> = {
1818
name: true,
1919
};
2020

21-
export class SafeDrivesCollection_ {
22-
private readonly unsafeDrives: GoogleAppsScript.Drive_v3.Drive.V3.Collection.DrivesCollection;
23-
24-
public constructor() {
25-
// TODO: Remove and access directly
26-
this.unsafeDrives = Drive.Drives;
21+
function driveIsSafe_<F extends DeepKeyof<SafeDrive>>(
22+
drive: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive,
23+
keys: F | null,
24+
): drive is typeof keys extends null ? SafeDrive : DeepPick<SafeDrive, F> {
25+
if (keys === null) {
26+
return driveIsSafe_(drive, safeDriveKeys);
2727
}
28-
29-
private static driveIsSafe<F extends DeepKeyof<SafeDrive>>(
30-
drive: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive,
31-
keys: F | null,
32-
): drive is typeof keys extends null ? SafeDrive : DeepPick<SafeDrive, F> {
33-
if (keys === null) {
34-
return SafeDrivesCollection_.driveIsSafe(drive, safeDriveKeys);
28+
for (const key in keys) {
29+
if (!Object.prototype.hasOwnProperty.call(keys, key)) {
30+
continue;
3531
}
36-
for (const key in keys) {
37-
if (!Object.prototype.hasOwnProperty.call(keys, key)) {
38-
continue;
39-
}
40-
if (drive[key as keyof DeepKeyof<SafeDrive>] === undefined) {
41-
return false;
42-
}
32+
if (drive[key as keyof DeepKeyof<SafeDrive>] === undefined) {
33+
return false;
4334
}
44-
return true;
4535
}
36+
return true;
37+
}
4638

47-
private static driveListIsSafe<F extends DeepKeyof<SafeDrive>>(
48-
driveList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.DriveList,
49-
keys: F | null,
50-
): driveList is SafeDriveList<F> {
51-
return (
52-
driveList.drives?.every((file) =>
53-
SafeDrivesCollection_.driveIsSafe(file, keys),
54-
) === true
55-
);
56-
}
39+
function driveListIsSafe_<F extends DeepKeyof<SafeDrive>>(
40+
driveList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.DriveList,
41+
keys: F | null,
42+
): driveList is SafeDriveList<F> {
43+
return driveList.drives?.every((file) => driveIsSafe_(file, keys)) === true;
44+
}
5745

58-
public list<F extends DeepKeyof<SafeDrive>>(
46+
export const SafeDrivesCollection_ = {
47+
list: <F extends DeepKeyof<SafeDrive>>(
5948
fields: F | null,
6049
optionalArgs: {
6150
maxResults?: number;
6251
orderBy?: string;
6352
pageToken?: string | undefined;
6453
} = {},
65-
): SafeDriveList<F> {
66-
const ret = this.unsafeDrives.list({
54+
): SafeDriveList<F> => {
55+
const ret = Drive.Drives.list({
6756
...optionalArgs,
6857
...(fields !== null && {
6958
fields: `nextPageToken, drives(${stringifyFields_(fields)})`,
7059
}),
7160
});
72-
if (!SafeDrivesCollection_.driveListIsSafe(ret, fields)) {
61+
if (!driveListIsSafe_(ret, fields)) {
7362
throw new Error("");
7463
}
7564
return ret;
76-
}
77-
}
65+
},
66+
};

0 commit comments

Comments
 (0)