From 3151d071d955674c8a1bfd105dc2c5483b978166 Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Fri, 10 Nov 2023 13:18:12 +0800 Subject: [PATCH] fix(media): change media sorting to addedTime descending (#1019) * fix(media): change media sorting to addedTime descending * test(media): adjust tests to account for new addedTime attribute --- src/services/db/GitFileSystemService.ts | 1 + src/services/db/RepoService.ts | 13 ++++++++----- .../db/__tests__/GitFileSystemService.spec.ts | 16 ++++++++++++++++ src/services/db/__tests__/RepoService.spec.ts | 6 ++++++ src/types/gitfilesystem.ts | 1 + 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/services/db/GitFileSystemService.ts b/src/services/db/GitFileSystemService.ts index fbdea33bf..0fa608247 100644 --- a/src/services/db/GitFileSystemService.ts +++ b/src/services/db/GitFileSystemService.ts @@ -929,6 +929,7 @@ export default class GitFileSystemService { sha, path, size: type === "dir" ? 0 : stats.size, + addedTime: stats.birthtimeMs, } return okAsync(result) diff --git a/src/services/db/RepoService.ts b/src/services/db/RepoService.ts index b9eeb1335..c6bcf909e 100644 --- a/src/services/db/RepoService.ts +++ b/src/services/db/RepoService.ts @@ -1,4 +1,3 @@ -import { GrowthBook } from "@growthbook/growthbook" import { AxiosCacheInstance } from "axios-cache-interceptor" import _ from "lodash" @@ -10,7 +9,6 @@ import GithubSessionData from "@root/classes/GithubSessionData" import UserWithSiteSessionData from "@root/classes/UserWithSiteSessionData" import { FEATURE_FLAGS, STAGING_BRANCH } from "@root/constants" import { GitHubCommitData } from "@root/types/commitData" -import { FeatureFlags } from "@root/types/featureFlags" import type { GitCommitResult, GitDirectoryItem, @@ -43,7 +41,12 @@ const getPaginatedDirectoryContents = ( (item) => item.type === "file" && item.name !== PLACEHOLDER_FILE_NAME ) const paginatedFiles = _(files) - .sortBy(["name"]) + // Note: We are sorting by name here to maintain compatibility for + // GitHub-login users, since it is very expensive to get the addedTime for + // each file from the GitHub API. The files will be sorted by addedTime in + // milliseconds for GGS users, so they will never see the alphabetical + // sorting. + .orderBy(["addedTime", "name"], ["desc", "asc"]) .drop(page * limit) .take(limit) .value() @@ -588,7 +591,7 @@ export default class RepoService extends GitHubService { { gitTree, message }: any, isStaging: boolean ): Promise { - return await super.updateTree( + return super.updateTree( sessionData, githubSessionData, { @@ -638,6 +641,6 @@ export default class RepoService extends GitHubService { sessionData: any, shouldMakePrivate: any ): Promise { - return await super.changeRepoPrivacy(sessionData, shouldMakePrivate) + return super.changeRepoPrivacy(sessionData, shouldMakePrivate) } } diff --git a/src/services/db/__tests__/GitFileSystemService.spec.ts b/src/services/db/__tests__/GitFileSystemService.spec.ts index a6dcfb495..1b0c0e777 100644 --- a/src/services/db/__tests__/GitFileSystemService.spec.ts +++ b/src/services/db/__tests__/GitFileSystemService.spec.ts @@ -90,6 +90,8 @@ describe("GitFileSystemService", () => { sha: "fake-dir-hash", path: "fake-dir", size: 0, + addedTime: fs.statSync(`${EFS_VOL_PATH_STAGING}/fake-repo/fake-dir`) + .birthtimeMs, } const expectedAnotherFakeDir: GitDirectoryItem = { name: "another-fake-dir", @@ -97,6 +99,9 @@ describe("GitFileSystemService", () => { sha: "another-fake-dir-hash", path: "another-fake-dir", size: 0, + addedTime: fs.statSync( + `${EFS_VOL_PATH_STAGING}/fake-repo/another-fake-dir` + ).birthtimeMs, } const expectedFakeEmptyDir: GitDirectoryItem = { name: "fake-empty-dir", @@ -104,6 +109,9 @@ describe("GitFileSystemService", () => { sha: "fake-empty-dir-hash", path: "fake-empty-dir", size: 0, + addedTime: fs.statSync( + `${EFS_VOL_PATH_STAGING}/fake-repo/fake-empty-dir` + ).birthtimeMs, } const expectedAnotherFakeFile: GitDirectoryItem = { name: "another-fake-file", @@ -111,6 +119,9 @@ describe("GitFileSystemService", () => { sha: "another-fake-file-hash", path: "another-fake-file", size: "Another fake content".length, + addedTime: fs.statSync( + `${EFS_VOL_PATH_STAGING}/fake-repo/another-fake-file` + ).birthtimeMs, } const result = await GitFileSystemService.listDirectoryContents( @@ -150,6 +161,8 @@ describe("GitFileSystemService", () => { sha: "fake-dir-hash", path: "fake-dir", size: 0, + addedTime: fs.statSync(`${EFS_VOL_PATH_STAGING}/fake-repo/fake-dir`) + .birthtimeMs, } const expectedAnotherFakeFile: GitDirectoryItem = { name: "another-fake-file", @@ -157,6 +170,9 @@ describe("GitFileSystemService", () => { sha: "another-fake-file-hash", path: "another-fake-file", size: "Another fake content".length, + addedTime: fs.statSync( + `${EFS_VOL_PATH_STAGING}/fake-repo/another-fake-file` + ).birthtimeMs, } const result = await GitFileSystemService.listDirectoryContents( diff --git a/src/services/db/__tests__/RepoService.spec.ts b/src/services/db/__tests__/RepoService.spec.ts index 989e9ca78..7f482bf60 100644 --- a/src/services/db/__tests__/RepoService.spec.ts +++ b/src/services/db/__tests__/RepoService.spec.ts @@ -341,6 +341,7 @@ describe("RepoService", () => { sha: "test-sha1", path: "test/fake-file.md", size: 100, + addedTime: 3, }, { name: "another-fake-file.md", @@ -348,6 +349,7 @@ describe("RepoService", () => { sha: "test-sha2", path: "another-fake-file.md", size: 100, + addedTime: 2, }, { name: "fake-dir", @@ -355,6 +357,7 @@ describe("RepoService", () => { sha: "test-sha3", path: "fake-dir", size: 0, + addedTime: 1, }, ] gbSpy.mockReturnValueOnce(true) @@ -387,6 +390,7 @@ describe("RepoService", () => { sha: "test-sha1", path: "test/fake-file.md", size: 100, + addedTime: 3, }, { name: "another-fake-file.md", @@ -394,6 +398,7 @@ describe("RepoService", () => { sha: "test-sha2", path: "another-fake-file.md", size: 100, + addedTime: 2, }, { name: "fake-dir", @@ -401,6 +406,7 @@ describe("RepoService", () => { sha: "test-sha3", path: "fake-dir", size: 0, + addedTime: 1, }, ] const gitHubServiceReadDirectory = jest.spyOn( diff --git a/src/types/gitfilesystem.ts b/src/types/gitfilesystem.ts index eac046e0d..1a33359e7 100644 --- a/src/types/gitfilesystem.ts +++ b/src/types/gitfilesystem.ts @@ -13,4 +13,5 @@ export type GitDirectoryItem = { sha: string path: string size: number + addedTime: number }