mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 22:39:26 +00:00
feat(asset): Recently Deleted previews, retention countdown, and version stack delete/restore (#418)
* feat(asset): improve Recently Deleted previews and retention indicators - Include soft-deleted children when building folder previews for the Recently Deleted view, and resolve deleted version stacks to their latest version so a trashed folder shows what was inside it. - Add a "days left" badge (clock + Nd) to file and folder cards in the Recently Deleted view, placed to the left of the comments badge. - Fix an issue where deleting or restoring a version stack from the file list did nothing because the WebUI dropped version_stack items before calling the API. - Share the 30-day trash retention window through TRASH_RETENTION_DAYS. * test(asset): use typed MediaInfo in recently-deleted preview tests Replace the explicit any cast with a fully typed MediaInfo object so the new tests comply with the no-explicit-any policy.
This commit is contained in:
@@ -2530,6 +2530,209 @@ describe('AssetService', () => {
|
||||
expect(updatedEntry?.sessionId).toBe(session.id)
|
||||
})
|
||||
})
|
||||
|
||||
describe('listChildren with showDeleted', () => {
|
||||
const createImageFile = async (params: {
|
||||
name: string
|
||||
projectId: string
|
||||
parentId: string
|
||||
creatorId: string
|
||||
thumbnailKey: string
|
||||
duration?: number
|
||||
sortIndex?: string
|
||||
}) => {
|
||||
return prisma.asset.create({
|
||||
data: {
|
||||
name: params.name,
|
||||
type: AssetType.file,
|
||||
status: AssetStatus.processed,
|
||||
isDeleted: true,
|
||||
projectId: params.projectId,
|
||||
parentId: params.parentId,
|
||||
creatorId: params.creatorId,
|
||||
sizeByte: 100,
|
||||
sortIndex: params.sortIndex,
|
||||
mediaType: 'image/png',
|
||||
media: {
|
||||
duration: 0,
|
||||
filesize: 0,
|
||||
frames: 0,
|
||||
imageTranscodes: [],
|
||||
videoTranscodes: [],
|
||||
finishedAt: new Date(0).toISOString(),
|
||||
metadata:
|
||||
params.duration === undefined
|
||||
? null
|
||||
: {
|
||||
originalHeight: 0,
|
||||
originalWidth: 0,
|
||||
hasAudio: false,
|
||||
duration: params.duration,
|
||||
bitRate: 0,
|
||||
frameRate: 0,
|
||||
totalFrames: 0,
|
||||
startTimecode: '00:00:00:00',
|
||||
format: null,
|
||||
},
|
||||
original: null,
|
||||
proxyType: 'image',
|
||||
thumbnail: {
|
||||
key: params.thumbnailKey,
|
||||
width: 0,
|
||||
height: 0,
|
||||
quality: 0,
|
||||
format: 'png',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
it('returns previews for children of a folder trashed via deleteAssets', async () => {
|
||||
const { project, user, assets } = await setupBasicAssets()
|
||||
|
||||
const folder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Trashed Folder',
|
||||
type: AssetType.folder,
|
||||
status: AssetStatus.uploaded,
|
||||
projectId: project.id,
|
||||
parentId: assets.root.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
|
||||
await createImageFile({
|
||||
name: 'inside-1.png',
|
||||
projectId: project.id,
|
||||
parentId: folder.id,
|
||||
creatorId: user.id,
|
||||
thumbnailKey: 'thumb-1',
|
||||
})
|
||||
await createImageFile({
|
||||
name: 'inside-2.png',
|
||||
projectId: project.id,
|
||||
parentId: folder.id,
|
||||
creatorId: user.id,
|
||||
thumbnailKey: 'thumb-2',
|
||||
})
|
||||
|
||||
await assetService.deleteAssets([folder.id])
|
||||
|
||||
const result = await assetService.listChildren({
|
||||
projectId: project.id,
|
||||
showDeleted: true,
|
||||
assetType: 'folder',
|
||||
})
|
||||
|
||||
expect(result.data).toHaveLength(1)
|
||||
expect(result.data[0].id).toBe(folder.id)
|
||||
expect(result.data[0].deletedAt).not.toBeNull()
|
||||
expect(result.data[0].latestChildren).toHaveLength(2)
|
||||
expect(result.data[0].latestChildren?.every((c) => c.preview?.thumbnailUrl)).toBe(true)
|
||||
})
|
||||
|
||||
it('resolves the latest deleted version for a trashed version stack child', async () => {
|
||||
const { project, user, assets } = await setupBasicAssets()
|
||||
|
||||
const folder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Trashed Folder With Stack',
|
||||
type: AssetType.folder,
|
||||
status: AssetStatus.uploaded,
|
||||
projectId: project.id,
|
||||
parentId: assets.root.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'clip',
|
||||
type: AssetType.version_stack,
|
||||
status: AssetStatus.processed,
|
||||
projectId: project.id,
|
||||
parentId: folder.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
|
||||
await createImageFile({
|
||||
name: 'clip-v1.png',
|
||||
projectId: project.id,
|
||||
parentId: stack.id,
|
||||
creatorId: user.id,
|
||||
thumbnailKey: 'v1-thumb',
|
||||
duration: 11,
|
||||
sortIndex: 'a1',
|
||||
})
|
||||
await createImageFile({
|
||||
name: 'clip-v2.png',
|
||||
projectId: project.id,
|
||||
parentId: stack.id,
|
||||
creatorId: user.id,
|
||||
thumbnailKey: 'v2-thumb',
|
||||
duration: 42,
|
||||
sortIndex: 'a0',
|
||||
})
|
||||
|
||||
await assetService.deleteAssets([folder.id])
|
||||
|
||||
const result = await assetService.listChildren({
|
||||
projectId: project.id,
|
||||
showDeleted: true,
|
||||
assetType: 'folder',
|
||||
})
|
||||
|
||||
const previews = result.data[0].latestChildren ?? []
|
||||
expect(previews).toHaveLength(1)
|
||||
// The latest version (lowest sortIndex) must be the one used for the preview
|
||||
expect(previews[0].preview?.duration).toBe(42)
|
||||
expect(previews[0].preview?.thumbnailUrl).toBeDefined()
|
||||
})
|
||||
|
||||
it('still hides soft-deleted children when browsing a folder normally', async () => {
|
||||
const { project, user, assets } = await setupBasicAssets()
|
||||
|
||||
const folder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Active Folder',
|
||||
type: AssetType.folder,
|
||||
status: AssetStatus.uploaded,
|
||||
projectId: project.id,
|
||||
parentId: assets.root.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.asset.create({
|
||||
data: {
|
||||
name: 'active.png',
|
||||
type: AssetType.file,
|
||||
status: AssetStatus.processed,
|
||||
isDeleted: false,
|
||||
projectId: project.id,
|
||||
parentId: folder.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
await prisma.asset.create({
|
||||
data: {
|
||||
name: 'deleted.png',
|
||||
type: AssetType.file,
|
||||
status: AssetStatus.processed,
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
projectId: project.id,
|
||||
parentId: folder.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
|
||||
const folderInfo = await assetService.getAsset({ assetId: folder.id })
|
||||
expect(folderInfo.latestChildren).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('AssetService — natural sort by name', () => {
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
ListRecentsRequest,
|
||||
CommentExportFormat,
|
||||
PreviewFormat,
|
||||
TRASH_RETENTION_DAYS,
|
||||
} from '@shumai/dtos'
|
||||
import {
|
||||
exportCommentsToFormat,
|
||||
@@ -98,6 +99,31 @@ export type AssetWithIncludes = Prisma.AssetGetPayload<{
|
||||
include: typeof assetInclude
|
||||
}>
|
||||
|
||||
/**
|
||||
* Same shape as {@link assetInclude}, but keeps soft-deleted children.
|
||||
*
|
||||
* The "Recently Deleted" view needs this because deleting a folder soft-deletes
|
||||
* all of its descendants by cascade, so filtering out deleted children leaves
|
||||
* the folder card with no previews of what was inside it.
|
||||
*/
|
||||
export const assetIncludeIncludingDeletedChildren = {
|
||||
...assetInclude,
|
||||
target: {
|
||||
...assetInclude.target,
|
||||
include: {
|
||||
...assetInclude.target.include,
|
||||
children: {
|
||||
...assetInclude.target.include.children,
|
||||
where: {},
|
||||
},
|
||||
},
|
||||
},
|
||||
children: {
|
||||
...assetInclude.children,
|
||||
where: {},
|
||||
},
|
||||
} as const
|
||||
|
||||
type CommentWithIncludes = Prisma.AssetCommentGetPayload<{
|
||||
include: {
|
||||
creator: true
|
||||
@@ -698,7 +724,7 @@ export class AssetService {
|
||||
throw new Error('ProjectID is required when ShowDeleted is true')
|
||||
}
|
||||
const thirtyDaysAgo = new Date()
|
||||
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30)
|
||||
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - TRASH_RETENTION_DAYS)
|
||||
|
||||
where = {
|
||||
projectId: req.projectId,
|
||||
@@ -751,11 +777,13 @@ export class AssetService {
|
||||
break
|
||||
}
|
||||
|
||||
const include = req.showDeleted ? assetIncludeIncludingDeletedChildren : assetInclude
|
||||
|
||||
const { data: assets, pageInfo } = await paginateQuery(
|
||||
async (skip, take) => {
|
||||
return this.prismaClient.asset.findMany({
|
||||
where,
|
||||
include: assetInclude,
|
||||
include,
|
||||
orderBy,
|
||||
skip,
|
||||
take,
|
||||
@@ -765,7 +793,9 @@ export class AssetService {
|
||||
req,
|
||||
)
|
||||
|
||||
const infos = await this.toAssetInfos(assets)
|
||||
const infos = await this.toAssetInfos(assets, undefined, {
|
||||
includeDeletedChildren: req.showDeleted,
|
||||
})
|
||||
|
||||
return { data: infos, pageInfo }
|
||||
}
|
||||
@@ -1441,7 +1471,7 @@ export class AssetService {
|
||||
* Cascade the 'pending_purge' status to all their descendants.
|
||||
*/
|
||||
private async expireTrashedAssets() {
|
||||
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000)
|
||||
const thirtyDaysAgo = new Date(Date.now() - TRASH_RETENTION_DAYS * 24 * 60 * 60 * 1000)
|
||||
|
||||
// Find up to 100 expired roots, ordered by oldest first to avoid starvation
|
||||
const expiredRoots = await this.prismaClient.asset.findMany({
|
||||
@@ -2123,7 +2153,9 @@ export class AssetService {
|
||||
async toAssetInfos(
|
||||
assets: AssetWithIncludes[],
|
||||
previewFormat?: PreviewFormat,
|
||||
options?: { includeDeletedChildren?: boolean },
|
||||
): Promise<AssetInfo[]> {
|
||||
const includeDeletedChildren = options?.includeDeletedChildren ?? false
|
||||
const stackIds = new Set<string>()
|
||||
|
||||
for (const a of assets) {
|
||||
@@ -2154,7 +2186,10 @@ export class AssetService {
|
||||
const versionsMap = new Map<string, AssetWithIncludes['children'][0][]>()
|
||||
if (stackIds.size > 0) {
|
||||
const allVersions = await this.prismaClient.asset.findMany({
|
||||
where: { parentId: { in: Array.from(stackIds) }, isDeleted: false },
|
||||
where: {
|
||||
parentId: { in: Array.from(stackIds) },
|
||||
...(includeDeletedChildren ? {} : { isDeleted: false }),
|
||||
},
|
||||
include: {
|
||||
creator: true,
|
||||
agent: { include: { user: true } },
|
||||
@@ -2280,7 +2315,7 @@ export class AssetService {
|
||||
folderForPreview.children
|
||||
) {
|
||||
for (const child of folderForPreview.children) {
|
||||
if (child.isDeleted) continue
|
||||
if (!includeDeletedChildren && child.isDeleted) continue
|
||||
let previewAsset = child as Asset
|
||||
let mediaType = child.mediaType
|
||||
|
||||
|
||||
Reference in New Issue
Block a user