Skip to content

Commit

Permalink
Applied new perfectionist rules
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Nov 25, 2024
1 parent 5198565 commit f87d92b
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 160 deletions.
22 changes: 11 additions & 11 deletions __tests__/test-utils/stub-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ declare global {
}
}

declare function _logEndpointCall(
name: string,
params: Array<google.script.Parameter>,
): void;
type EndpointStub =
| { delay?: number; status: "failure"; value: Error }
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- The return value of the API can be anything
| { delay?: number; status: "success"; value: any };

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- From google.script types
type FailureHandlerType = (error: Error, object?: any) => void;

type Run = google.script.PublicEndpoints & google.script.RunnerFunctions;

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- From google.script types
type SuccessHandlerType = (value?: any, object?: any) => void;

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- From google.script types
type FailureHandlerType = (error: Error, object?: any) => void;

type EndpointStub =
| { delay?: number; status: "failure"; value: Error }
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- The return value of the API can be anything
| { delay?: number; status: "success"; value: any };
declare function _logEndpointCall(
name: string,
params: Array<google.script.Parameter>,
): void;

export async function setup(
page: Page,
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import js from "@eslint/js";
import eslintComments from "@eslint-community/eslint-plugin-eslint-comments";
import commentsConfig from "@eslint-community/eslint-plugin-eslint-comments/configs";
import js from "@eslint/js";
import jest from "eslint-plugin-jest";
import perfectionist from "eslint-plugin-perfectionist";
import playwright from "eslint-plugin-playwright";
Expand Down
32 changes: 16 additions & 16 deletions src/backend/move/copyFileComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,6 @@ import type {

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

function listFileComments_(
fileID: string,
driveService: SafeDriveService_,
): Array<SafeComment> {
return paginationHelper_<SafeCommentList, SafeComment>(
(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,
);
}

export function copyFileComments_(
sourceID: string,
destinationID: string,
Expand All @@ -46,3 +30,19 @@ export function copyFileComments_(
}
}
}

function listFileComments_(
fileID: string,
driveService: SafeDriveService_,
): Array<SafeComment> {
return paginationHelper_<SafeCommentList, SafeComment>(
(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,
);
}
104 changes: 52 additions & 52 deletions src/backend/move/folderManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,38 @@ export interface ListFolderContentsFields {
title: true;
}

function listFolderContents_(
export function deleteFolderIfEmpty_(
folderID: string,
mimeTypeCondition: string,
driveService: SafeDriveService_,
): Array<DeepPick<SafeFile, ListFolderContentsFields>> {
return paginationHelper_<
SafeFileList<ListFolderContentsFields>,
DeepPick<SafeFile, ListFolderContentsFields>
>(
(pageToken) =>
driveService.Files.list(
{
capabilities: { canMoveItemOutOfDrive: true },
id: true,
title: true,
},
{
includeItemsFromAllDrives: true,
maxResults: 1000,
pageToken,
q: `"${folderID}" in parents and mimeType ${mimeTypeCondition} and trashed = false`,
supportsAllDrives: true,
},
),
(response) => response.items,
): void {
if (!isFolderEmpty_(folderID, driveService)) {
return;
}
const response = driveService.Files.get(folderID, {
userPermission: { role: true },
});
if (
response.userPermission.role === "owner" ||
response.userPermission.role === "organizer"
) {
driveService.Files.remove(folderID);
}
}

export function isFolderEmpty_(
folderID: string,
driveService: SafeDriveService_,
): boolean {
const response = driveService.Files.list(
{ id: true },
{
includeItemsFromAllDrives: true,
maxResults: 1,
q: `"${folderID}" in parents and trashed = false`,
supportsAllDrives: true,
},
);
return response.items.length === 0;
}

export function listFilesInFolder_(
Expand All @@ -63,36 +69,30 @@ export function listFoldersInFolder_(
);
}

export function isFolderEmpty_(
function listFolderContents_(
folderID: string,
mimeTypeCondition: string,
driveService: SafeDriveService_,
): boolean {
const response = driveService.Files.list(
{ id: true },
{
includeItemsFromAllDrives: true,
maxResults: 1,
q: `"${folderID}" in parents and trashed = false`,
supportsAllDrives: true,
},
): Array<DeepPick<SafeFile, ListFolderContentsFields>> {
return paginationHelper_<
SafeFileList<ListFolderContentsFields>,
DeepPick<SafeFile, ListFolderContentsFields>
>(
(pageToken) =>
driveService.Files.list(
{
capabilities: { canMoveItemOutOfDrive: true },
id: true,
title: true,
},
{
includeItemsFromAllDrives: true,
maxResults: 1000,
pageToken,
q: `"${folderID}" in parents and mimeType ${mimeTypeCondition} and trashed = false`,
supportsAllDrives: true,
},
),
(response) => response.items,
);
return response.items.length === 0;
}

export function deleteFolderIfEmpty_(
folderID: string,
driveService: SafeDriveService_,
): void {
if (!isFolderEmpty_(folderID, driveService)) {
return;
}
const response = driveService.Files.get(folderID, {
userPermission: { role: true },
});
if (
response.userPermission.role === "owner" ||
response.userPermission.role === "organizer"
) {
driveService.Files.remove(folderID);
}
}
52 changes: 26 additions & 26 deletions src/backend/move/moveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ import type { ListFolderContentsFields } from "./folderManagement";

import { copyFileComments_ } from "./copyFileComments";

function moveFileDirectly_(
fileID: string,
export function moveFile_(
file: DeepPick<SafeFile, ListFolderContentsFields>,
state: MoveState_,
context: MoveContext,
copyComments: boolean,
driveService: SafeDriveService_,
): void {
driveService.Files.update({}, fileID, null, {
addParents: context.destinationID,
fields: "",
removeParents: context.sourceID,
supportsAllDrives: true,
});
if (file.capabilities.canMoveItemOutOfDrive) {
try {
moveFileDirectly_(file.id, context, driveService);
return;
} catch (e) {} // eslint-disable-line no-empty -- Handled by moving by copying
}
moveFileByCopy_(
file.id,
file.title,
state,
context,
copyComments,
driveService,
);
}

function moveFileByCopy_(
Expand Down Expand Up @@ -47,25 +57,15 @@ function moveFileByCopy_(
);
}

export function moveFile_(
file: DeepPick<SafeFile, ListFolderContentsFields>,
state: MoveState_,
function moveFileDirectly_(
fileID: string,
context: MoveContext,
copyComments: boolean,
driveService: SafeDriveService_,
): void {
if (file.capabilities.canMoveItemOutOfDrive) {
try {
moveFileDirectly_(file.id, context, driveService);
return;
} catch (e) {} // eslint-disable-line no-empty -- Handled by moving by copying
}
moveFileByCopy_(
file.id,
file.title,
state,
context,
copyComments,
driveService,
);
driveService.Files.update({}, fileID, null, {
addParents: context.destinationID,
fields: "",
removeParents: context.sourceID,
supportsAllDrives: true,
});
}
34 changes: 17 additions & 17 deletions src/backend/move/moveFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,6 @@ import { listFilesInFolder_, listFoldersInFolder_ } from "./folderManagement";
import { moveFile_ } from "./moveFile";
import { resolveDestinationFolder_ } from "./resolveDestinationFolder";

function moveFolderContentsFiles_(
state: MoveState_,
context: MoveContext,
copyComments: boolean,
driveService: SafeDriveService_,
): void {
const files = state.tryOrLog(context, () =>
listFilesInFolder_(context.sourceID, driveService),
);
if (files === null) {
return;
}
for (const file of files) {
moveFile_(file, state, context, copyComments, driveService);
}
}

export function moveFolder_(
state: MoveState_,
context: MoveContext,
Expand Down Expand Up @@ -53,3 +36,20 @@ export function moveFolder_(
state.removePath(context);
state.saveState();
}

function moveFolderContentsFiles_(
state: MoveState_,
context: MoveContext,
copyComments: boolean,
driveService: SafeDriveService_,
): void {
const files = state.tryOrLog(context, () =>
listFilesInFolder_(context.sourceID, driveService),
);
if (files === null) {
return;
}
for (const file of files) {
moveFile_(file, state, context, copyComments, driveService);
}
}
62 changes: 31 additions & 31 deletions src/backend/utils/DriveBackedValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@ export class DriveBackedValue_<T> {
.join("");
}

public deleteValue(): void {
const folderId = this.getExistingDriveFolderId();
if (folderId === null) {
return;
}
const fileId = this.getExistingDriveFileId(folderId);
if (fileId !== null) {
this.deleteExistingDriveFile(fileId);
}
if (this.isExistingDriveFolderEmpty(folderId)) {
// This function works with folders as well
this.deleteExistingDriveFile(folderId);
}
}

public loadValue(): T | null {
const folderId = this.getExistingDriveFolderId();
if (folderId === null) {
return null;
}
const fileId = this.getExistingDriveFileId(folderId);
if (fileId === null) {
return null;
}
return this.getExistingDriveFileContents(fileId);
}

public saveValue(value: T): void {
this.saveDriveFile(this.getDriveFolderId(), value);
}

private createDriveFolder(): string {
const response = this.driveService.Files.insert(
{
Expand Down Expand Up @@ -127,35 +158,4 @@ export class DriveBackedValue_<T> {
Utilities.newBlob(JSON.stringify(value), "application/json"),
);
}

public deleteValue(): void {
const folderId = this.getExistingDriveFolderId();
if (folderId === null) {
return;
}
const fileId = this.getExistingDriveFileId(folderId);
if (fileId !== null) {
this.deleteExistingDriveFile(fileId);
}
if (this.isExistingDriveFolderEmpty(folderId)) {
// This function works with folders as well
this.deleteExistingDriveFile(folderId);
}
}

public loadValue(): T | null {
const folderId = this.getExistingDriveFolderId();
if (folderId === null) {
return null;
}
const fileId = this.getExistingDriveFileId(folderId);
if (fileId === null) {
return null;
}
return this.getExistingDriveFileContents(fileId);
}

public saveValue(value: T): void {
this.saveDriveFile(this.getDriveFolderId(), value);
}
}
Loading

0 comments on commit f87d92b

Please sign in to comment.