Skip to content

Commit

Permalink
Not wrapping now safe Drive collections
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Jan 16, 2025
1 parent bd3b5fa commit 00a5abe
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 304 deletions.
12 changes: 6 additions & 6 deletions src/backend/utils/SafeDriveService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export type {
} from "./SafeDriveService/SafeFilesCollection";

export class SafeDriveService_ {
public readonly Comments: SafeCommentsCollection_;
public readonly Drives: SafeDrivesCollection_;
public readonly Files: SafeFilesCollection_;
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() {
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;
}
}
99 changes: 44 additions & 55 deletions src/backend/utils/SafeDriveService/SafeCommentsCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,77 +20,66 @@ interface SafeUser {
me: boolean;
}

export class SafeCommentsCollection_ {
private readonly unsafeComments: GoogleAppsScript.Drive_v3.Drive.V3.Collection.CommentsCollection;

public constructor() {
// TODO: Remove and access directly
this.unsafeComments = Drive.Comments;
}

private static commentIsSafe(
comment: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment,
): comment is SafeComment {
return (
comment.author !== undefined &&
SafeCommentsCollection_.userIsSafe(comment.author) &&
comment.id !== undefined &&
comment.content !== undefined &&
comment.replies?.every((reply) =>
SafeCommentsCollection_.commentReplyIsSafe(reply),
) === true
);
}
function commentIsSafe_(
comment: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment,
): comment is SafeComment {
return (
comment.author !== undefined &&
userIsSafe_(comment.author) &&
comment.id !== undefined &&
comment.content !== undefined &&
comment.replies?.every((reply) => commentReplyIsSafe_(reply)) === true
);
}

private static commentListIsSafe(
commentList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.CommentList,
): commentList is SafeCommentList {
return (
commentList.comments?.every((comment) =>
SafeCommentsCollection_.commentIsSafe(comment),
) === true
);
}
function commentListIsSafe_(
commentList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.CommentList,
): commentList is SafeCommentList {
return (
commentList.comments?.every((comment) => commentIsSafe_(comment)) === true
);
}

private static commentReplyIsSafe(
commentReply: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Reply,
): commentReply is SafeReply {
return (
commentReply.author !== undefined &&
SafeCommentsCollection_.userIsSafe(commentReply.author) &&
commentReply.content !== undefined
);
}
function commentReplyIsSafe_(
commentReply: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Reply,
): commentReply is SafeReply {
return (
commentReply.author !== undefined &&
userIsSafe_(commentReply.author) &&
commentReply.content !== undefined
);
}

private static userIsSafe(
user: GoogleAppsScript.Drive_v3.Drive.V3.Schema.User,
): user is SafeUser {
return user.me !== undefined && user.displayName !== undefined;
}
function userIsSafe_(
user: GoogleAppsScript.Drive_v3.Drive.V3.Schema.User,
): user is SafeUser {
return user.me !== undefined && user.displayName !== undefined;
}

public create(
export const SafeCommentsCollection_ = {
create: (
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment,
fileId: string,
): SafeComment {
const ret = this.unsafeComments.create(resource, fileId);
if (!SafeCommentsCollection_.commentIsSafe(ret)) {
): SafeComment => {
const ret = Drive.Comments.create(resource, fileId);
if (!commentIsSafe_(ret)) {
throw new Error("");
}
return ret;
}
},

public list(
list: (
fileId: string,
optionalArgs: {
fields?: string;
maxResults?: number;
pageToken?: string | undefined;
} = {},
): SafeCommentList {
const ret = this.unsafeComments.list(fileId, optionalArgs);
if (!SafeCommentsCollection_.commentListIsSafe(ret)) {
): SafeCommentList => {
const ret = Drive.Comments.list(fileId, optionalArgs);
if (!commentListIsSafe_(ret)) {
throw new Error("");
}
return ret;
}
}
},
};
63 changes: 26 additions & 37 deletions src/backend/utils/SafeDriveService/SafeDrivesCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,60 +18,49 @@ const safeDriveKeys: DeepKeyof<SafeDrive> = {
name: true,
};

export class SafeDrivesCollection_ {
private readonly unsafeDrives: GoogleAppsScript.Drive_v3.Drive.V3.Collection.DrivesCollection;

public constructor() {
// TODO: Remove and access directly
this.unsafeDrives = Drive.Drives;
function driveIsSafe_<F extends DeepKeyof<SafeDrive>>(
drive: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive,
keys: F | null,
): drive is typeof keys extends null ? SafeDrive : DeepPick<SafeDrive, F> {
if (keys === null) {
return driveIsSafe_(drive, safeDriveKeys);
}

private static driveIsSafe<F extends DeepKeyof<SafeDrive>>(
drive: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive,
keys: F | null,
): drive is typeof keys extends null ? SafeDrive : DeepPick<SafeDrive, F> {
if (keys === null) {
return SafeDrivesCollection_.driveIsSafe(drive, safeDriveKeys);
for (const key in keys) {
if (!Object.prototype.hasOwnProperty.call(keys, key)) {
continue;

Check warning on line 30 in src/backend/utils/SafeDriveService/SafeDrivesCollection.ts

View check run for this annotation

Codecov / codecov/patch

src/backend/utils/SafeDriveService/SafeDrivesCollection.ts#L30

Added line #L30 was not covered by tests
}
for (const key in keys) {
if (!Object.prototype.hasOwnProperty.call(keys, key)) {
continue;
}
if (drive[key as keyof DeepKeyof<SafeDrive>] === undefined) {
return false;
}
if (drive[key as keyof DeepKeyof<SafeDrive>] === undefined) {
return false;
}
return true;
}
return true;
}

private static driveListIsSafe<F extends DeepKeyof<SafeDrive>>(
driveList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.DriveList,
keys: F | null,
): driveList is SafeDriveList<F> {
return (
driveList.drives?.every((file) =>
SafeDrivesCollection_.driveIsSafe(file, keys),
) === true
);
}
function driveListIsSafe_<F extends DeepKeyof<SafeDrive>>(
driveList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.DriveList,
keys: F | null,
): driveList is SafeDriveList<F> {
return driveList.drives?.every((file) => driveIsSafe_(file, keys)) === true;
}

public list<F extends DeepKeyof<SafeDrive>>(
export const SafeDrivesCollection_ = {
list: <F extends DeepKeyof<SafeDrive>>(
fields: F | null,
optionalArgs: {
maxResults?: number;
orderBy?: string;
pageToken?: string | undefined;
} = {},
): SafeDriveList<F> {
const ret = this.unsafeDrives.list({
): SafeDriveList<F> => {
const ret = Drive.Drives.list({
...optionalArgs,
...(fields !== null && {
fields: `nextPageToken, drives(${stringifyFields_(fields)})`,
}),
});
if (!SafeDrivesCollection_.driveListIsSafe(ret, fields)) {
if (!driveListIsSafe_(ret, fields)) {
throw new Error("");
}
return ret;
}
}
},
};
Loading

0 comments on commit 00a5abe

Please sign in to comment.