diff --git a/e2e/src/api/specs/trash.e2e-spec.ts b/e2e/src/api/specs/trash.e2e-spec.ts index 0c28a72825..17bb568c61 100644 --- a/e2e/src/api/specs/trash.e2e-spec.ts +++ b/e2e/src/api/specs/trash.e2e-spec.ts @@ -34,8 +34,11 @@ describe('/trash', () => { const before = await getAssetInfo({ id: assetId }, { headers: asBearerAuth(admin.accessToken) }); expect(before).toStrictEqual(expect.objectContaining({ id: assetId, isTrashed: true })); - const { status } = await request(app).post('/trash/empty').set('Authorization', `Bearer ${admin.accessToken}`); - expect(status).toBe(204); + const { status, body } = await request(app) + .post('/trash/empty') + .set('Authorization', `Bearer ${admin.accessToken}`); + expect(status).toBe(200); + expect(body).toEqual({ count: 1 }); await utils.waitForWebsocketEvent({ event: 'assetDelete', id: assetId }); @@ -51,8 +54,11 @@ describe('/trash', () => { const before = await getAssetInfo({ id: assetId }, { headers: asBearerAuth(admin.accessToken) }); expect(before).toStrictEqual(expect.objectContaining({ id: assetId, isTrashed: true, isArchived: true })); - const { status } = await request(app).post('/trash/empty').set('Authorization', `Bearer ${admin.accessToken}`); - expect(status).toBe(204); + const { status, body } = await request(app) + .post('/trash/empty') + .set('Authorization', `Bearer ${admin.accessToken}`); + expect(status).toBe(200); + expect(body).toEqual({ count: 1 }); await utils.waitForWebsocketEvent({ event: 'assetDelete', id: assetId }); @@ -76,8 +82,11 @@ describe('/trash', () => { const before = await getAssetInfo({ id: assetId }, { headers: asBearerAuth(admin.accessToken) }); expect(before).toStrictEqual(expect.objectContaining({ id: assetId, isTrashed: true })); - const { status } = await request(app).post('/trash/restore').set('Authorization', `Bearer ${admin.accessToken}`); - expect(status).toBe(204); + const { status, body } = await request(app) + .post('/trash/restore') + .set('Authorization', `Bearer ${admin.accessToken}`); + expect(status).toBe(200); + expect(body).toEqual({ count: 1 }); const after = await getAssetInfo({ id: assetId }, { headers: asBearerAuth(admin.accessToken) }); expect(after).toStrictEqual(expect.objectContaining({ id: assetId, isTrashed: false })); @@ -99,11 +108,12 @@ describe('/trash', () => { const before = await utils.getAssetInfo(admin.accessToken, assetId); expect(before.isTrashed).toBe(true); - const { status } = await request(app) + const { status, body } = await request(app) .post('/trash/restore/assets') .set('Authorization', `Bearer ${admin.accessToken}`) .send({ ids: [assetId] }); - expect(status).toBe(204); + expect(status).toBe(200); + expect(body).toEqual({ count: 1 }); const after = await utils.getAssetInfo(admin.accessToken, assetId); expect(after.isTrashed).toBe(false); diff --git a/mobile/openapi/README.md b/mobile/openapi/README.md index 16f293f81a..697239fa44 100644 --- a/mobile/openapi/README.md +++ b/mobile/openapi/README.md @@ -452,6 +452,7 @@ Class | Method | HTTP request | Description - [ToneMapping](doc//ToneMapping.md) - [TranscodeHWAccel](doc//TranscodeHWAccel.md) - [TranscodePolicy](doc//TranscodePolicy.md) + - [TrashResponseDto](doc//TrashResponseDto.md) - [UpdateAlbumDto](doc//UpdateAlbumDto.md) - [UpdateAlbumUserDto](doc//UpdateAlbumUserDto.md) - [UpdateAssetDto](doc//UpdateAssetDto.md) diff --git a/mobile/openapi/lib/api.dart b/mobile/openapi/lib/api.dart index 915c70f08e..8a1655d35a 100644 --- a/mobile/openapi/lib/api.dart +++ b/mobile/openapi/lib/api.dart @@ -265,6 +265,7 @@ part 'model/time_bucket_size.dart'; part 'model/tone_mapping.dart'; part 'model/transcode_hw_accel.dart'; part 'model/transcode_policy.dart'; +part 'model/trash_response_dto.dart'; part 'model/update_album_dto.dart'; part 'model/update_album_user_dto.dart'; part 'model/update_asset_dto.dart'; diff --git a/mobile/openapi/lib/api/trash_api.dart b/mobile/openapi/lib/api/trash_api.dart index 9c346870ec..8f8c6ffb3a 100644 --- a/mobile/openapi/lib/api/trash_api.dart +++ b/mobile/openapi/lib/api/trash_api.dart @@ -42,11 +42,19 @@ class TrashApi { ); } - Future emptyTrash() async { + Future emptyTrash() async { final response = await emptyTrashWithHttpInfo(); if (response.statusCode >= HttpStatus.badRequest) { throw ApiException(response.statusCode, await _decodeBodyBytes(response)); } + // When a remote server returns no body with a status of 204, we shall not decode it. + // At the time of writing this, `dart:convert` will throw an "Unexpected end of input" + // FormatException when trying to decode an empty string. + if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { + return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'TrashResponseDto',) as TrashResponseDto; + + } + return null; } /// Performs an HTTP 'POST /trash/restore/assets' operation and returns the [Response]. @@ -81,11 +89,19 @@ class TrashApi { /// Parameters: /// /// * [BulkIdsDto] bulkIdsDto (required): - Future restoreAssets(BulkIdsDto bulkIdsDto,) async { + Future restoreAssets(BulkIdsDto bulkIdsDto,) async { final response = await restoreAssetsWithHttpInfo(bulkIdsDto,); if (response.statusCode >= HttpStatus.badRequest) { throw ApiException(response.statusCode, await _decodeBodyBytes(response)); } + // When a remote server returns no body with a status of 204, we shall not decode it. + // At the time of writing this, `dart:convert` will throw an "Unexpected end of input" + // FormatException when trying to decode an empty string. + if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { + return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'TrashResponseDto',) as TrashResponseDto; + + } + return null; } /// Performs an HTTP 'POST /trash/restore' operation and returns the [Response]. @@ -114,10 +130,18 @@ class TrashApi { ); } - Future restoreTrash() async { + Future restoreTrash() async { final response = await restoreTrashWithHttpInfo(); if (response.statusCode >= HttpStatus.badRequest) { throw ApiException(response.statusCode, await _decodeBodyBytes(response)); } + // When a remote server returns no body with a status of 204, we shall not decode it. + // At the time of writing this, `dart:convert` will throw an "Unexpected end of input" + // FormatException when trying to decode an empty string. + if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { + return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'TrashResponseDto',) as TrashResponseDto; + + } + return null; } } diff --git a/mobile/openapi/lib/api_client.dart b/mobile/openapi/lib/api_client.dart index 6a40de730c..4976c8a75f 100644 --- a/mobile/openapi/lib/api_client.dart +++ b/mobile/openapi/lib/api_client.dart @@ -585,6 +585,8 @@ class ApiClient { return TranscodeHWAccelTypeTransformer().decode(value); case 'TranscodePolicy': return TranscodePolicyTypeTransformer().decode(value); + case 'TrashResponseDto': + return TrashResponseDto.fromJson(value); case 'UpdateAlbumDto': return UpdateAlbumDto.fromJson(value); case 'UpdateAlbumUserDto': diff --git a/mobile/openapi/lib/model/trash_response_dto.dart b/mobile/openapi/lib/model/trash_response_dto.dart new file mode 100644 index 0000000000..52a05ff6d4 --- /dev/null +++ b/mobile/openapi/lib/model/trash_response_dto.dart @@ -0,0 +1,98 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.18 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +part of openapi.api; + +class TrashResponseDto { + /// Returns a new [TrashResponseDto] instance. + TrashResponseDto({ + required this.count, + }); + + int count; + + @override + bool operator ==(Object other) => identical(this, other) || other is TrashResponseDto && + other.count == count; + + @override + int get hashCode => + // ignore: unnecessary_parenthesis + (count.hashCode); + + @override + String toString() => 'TrashResponseDto[count=$count]'; + + Map toJson() { + final json = {}; + json[r'count'] = this.count; + return json; + } + + /// Returns a new [TrashResponseDto] instance and imports its values from + /// [value] if it's a [Map], null otherwise. + // ignore: prefer_constructors_over_static_methods + static TrashResponseDto? fromJson(dynamic value) { + if (value is Map) { + final json = value.cast(); + + return TrashResponseDto( + count: mapValueOfType(json, r'count')!, + ); + } + return null; + } + + static List listFromJson(dynamic json, {bool growable = false,}) { + final result = []; + if (json is List && json.isNotEmpty) { + for (final row in json) { + final value = TrashResponseDto.fromJson(row); + if (value != null) { + result.add(value); + } + } + } + return result.toList(growable: growable); + } + + static Map mapFromJson(dynamic json) { + final map = {}; + if (json is Map && json.isNotEmpty) { + json = json.cast(); // ignore: parameter_assignments + for (final entry in json.entries) { + final value = TrashResponseDto.fromJson(entry.value); + if (value != null) { + map[entry.key] = value; + } + } + } + return map; + } + + // maps a json object with a list of TrashResponseDto-objects as value to a dart map + static Map> mapListFromJson(dynamic json, {bool growable = false,}) { + final map = >{}; + if (json is Map && json.isNotEmpty) { + // ignore: parameter_assignments + json = json.cast(); + for (final entry in json.entries) { + map[entry.key] = TrashResponseDto.listFromJson(entry.value, growable: growable,); + } + } + return map; + } + + /// The list of required keys that must be present in a JSON. + static const requiredKeys = { + 'count', + }; +} + diff --git a/open-api/immich-openapi-specs.json b/open-api/immich-openapi-specs.json index fc813ae244..f48fa989da 100644 --- a/open-api/immich-openapi-specs.json +++ b/open-api/immich-openapi-specs.json @@ -6839,7 +6839,14 @@ "operationId": "emptyTrash", "parameters": [], "responses": { - "204": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/TrashResponseDto" + } + } + }, "description": "" } }, @@ -6864,7 +6871,14 @@ "operationId": "restoreTrash", "parameters": [], "responses": { - "204": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/TrashResponseDto" + } + } + }, "description": "" } }, @@ -6899,7 +6913,14 @@ "required": true }, "responses": { - "204": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/TrashResponseDto" + } + } + }, "description": "" } }, @@ -12254,6 +12275,17 @@ ], "type": "string" }, + "TrashResponseDto": { + "properties": { + "count": { + "type": "integer" + } + }, + "required": [ + "count" + ], + "type": "object" + }, "UpdateAlbumDto": { "properties": { "albumName": { diff --git a/open-api/typescript-sdk/src/fetch-client.ts b/open-api/typescript-sdk/src/fetch-client.ts index dec8ad1e3d..c2d73bda1a 100644 --- a/open-api/typescript-sdk/src/fetch-client.ts +++ b/open-api/typescript-sdk/src/fetch-client.ts @@ -1246,6 +1246,9 @@ export type TimeBucketResponseDto = { count: number; timeBucket: string; }; +export type TrashResponseDto = { + count: number; +}; export type UserUpdateMeDto = { email?: string; name?: string; @@ -3073,13 +3076,19 @@ export function getTimeBuckets({ albumId, isArchived, isFavorite, isTrashed, key })); } export function emptyTrash(opts?: Oazapfts.RequestOpts) { - return oazapfts.ok(oazapfts.fetchText("/trash/empty", { + return oazapfts.ok(oazapfts.fetchJson<{ + status: 200; + data: TrashResponseDto; + }>("/trash/empty", { ...opts, method: "POST" })); } export function restoreTrash(opts?: Oazapfts.RequestOpts) { - return oazapfts.ok(oazapfts.fetchText("/trash/restore", { + return oazapfts.ok(oazapfts.fetchJson<{ + status: 200; + data: TrashResponseDto; + }>("/trash/restore", { ...opts, method: "POST" })); @@ -3087,7 +3096,10 @@ export function restoreTrash(opts?: Oazapfts.RequestOpts) { export function restoreAssets({ bulkIdsDto }: { bulkIdsDto: BulkIdsDto; }, opts?: Oazapfts.RequestOpts) { - return oazapfts.ok(oazapfts.fetchText("/trash/restore/assets", oazapfts.json({ + return oazapfts.ok(oazapfts.fetchJson<{ + status: 200; + data: TrashResponseDto; + }>("/trash/restore/assets", oazapfts.json({ ...opts, method: "POST", body: bulkIdsDto diff --git a/server/src/controllers/trash.controller.ts b/server/src/controllers/trash.controller.ts index 20adbb11bb..dfcdfa6ba2 100644 --- a/server/src/controllers/trash.controller.ts +++ b/server/src/controllers/trash.controller.ts @@ -2,6 +2,7 @@ import { Body, Controller, HttpCode, HttpStatus, Post } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { BulkIdsDto } from 'src/dtos/asset-ids.response.dto'; import { AuthDto } from 'src/dtos/auth.dto'; +import { TrashResponseDto } from 'src/dtos/trash.dto'; import { Permission } from 'src/enum'; import { Auth, Authenticated } from 'src/middleware/auth.guard'; import { TrashService } from 'src/services/trash.service'; @@ -12,23 +13,23 @@ export class TrashController { constructor(private service: TrashService) {} @Post('empty') - @HttpCode(HttpStatus.NO_CONTENT) + @HttpCode(HttpStatus.OK) @Authenticated({ permission: Permission.ASSET_DELETE }) - emptyTrash(@Auth() auth: AuthDto): Promise { + emptyTrash(@Auth() auth: AuthDto): Promise { return this.service.empty(auth); } @Post('restore') - @HttpCode(HttpStatus.NO_CONTENT) + @HttpCode(HttpStatus.OK) @Authenticated({ permission: Permission.ASSET_DELETE }) - restoreTrash(@Auth() auth: AuthDto): Promise { + restoreTrash(@Auth() auth: AuthDto): Promise { return this.service.restore(auth); } @Post('restore/assets') - @HttpCode(HttpStatus.NO_CONTENT) + @HttpCode(HttpStatus.OK) @Authenticated({ permission: Permission.ASSET_DELETE }) - restoreAssets(@Auth() auth: AuthDto, @Body() dto: BulkIdsDto): Promise { + restoreAssets(@Auth() auth: AuthDto, @Body() dto: BulkIdsDto): Promise { return this.service.restoreAssets(auth, dto); } } diff --git a/server/src/dtos/trash.dto.ts b/server/src/dtos/trash.dto.ts new file mode 100644 index 0000000000..d8e139bff2 --- /dev/null +++ b/server/src/dtos/trash.dto.ts @@ -0,0 +1,6 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class TrashResponseDto { + @ApiProperty({ type: 'integer' }) + count!: number; +} diff --git a/server/src/entities/asset.entity.ts b/server/src/entities/asset.entity.ts index 9ebf9364d1..0b893134d0 100644 --- a/server/src/entities/asset.entity.ts +++ b/server/src/entities/asset.entity.ts @@ -10,7 +10,7 @@ import { SmartSearchEntity } from 'src/entities/smart-search.entity'; import { StackEntity } from 'src/entities/stack.entity'; import { TagEntity } from 'src/entities/tag.entity'; import { UserEntity } from 'src/entities/user.entity'; -import { AssetType } from 'src/enum'; +import { AssetStatus, AssetType } from 'src/enum'; import { Column, CreateDateColumn, @@ -70,6 +70,9 @@ export class AssetEntity { @Column() type!: AssetType; + @Column({ type: 'enum', enum: AssetStatus, default: AssetStatus.ACTIVE }) + status!: AssetStatus; + @Column() originalPath!: string; diff --git a/server/src/enum.ts b/server/src/enum.ts index d76d97371c..027b3160a7 100644 --- a/server/src/enum.ts +++ b/server/src/enum.ts @@ -182,6 +182,12 @@ export enum UserStatus { DELETED = 'deleted', } +export enum AssetStatus { + ACTIVE = 'active', + TRASHED = 'trashed', + DELETED = 'deleted', +} + export enum SourceType { MACHINE_LEARNING = 'machine-learning', EXIF = 'exif', diff --git a/server/src/interfaces/asset.interface.ts b/server/src/interfaces/asset.interface.ts index de1aa60b70..387fa27185 100644 --- a/server/src/interfaces/asset.interface.ts +++ b/server/src/interfaces/asset.interface.ts @@ -1,7 +1,7 @@ import { AssetJobStatusEntity } from 'src/entities/asset-job-status.entity'; import { AssetEntity } from 'src/entities/asset.entity'; import { ExifEntity } from 'src/entities/exif.entity'; -import { AssetFileType, AssetOrder, AssetType } from 'src/enum'; +import { AssetFileType, AssetOrder, AssetStatus, AssetType } from 'src/enum'; import { AssetSearchOptions, SearchExploreItem } from 'src/interfaces/search.interface'; import { Paginated, PaginationOptions } from 'src/utils/pagination'; import { FindOptionsOrder, FindOptionsRelations, FindOptionsSelect } from 'typeorm'; @@ -56,6 +56,7 @@ export interface AssetBuilderOptions { userIds?: string[]; withStacked?: boolean; exifInfo?: boolean; + status?: AssetStatus; assetType?: AssetType; } @@ -185,8 +186,6 @@ export interface IAssetRepository { updateDuplicates(options: AssetUpdateDuplicateOptions): Promise; update(asset: AssetUpdateOptions): Promise; remove(asset: AssetEntity): Promise; - softDeleteAll(ids: string[]): Promise; - restoreAll(ids: string[]): Promise; findLivePhotoMatch(options: LivePhotoSearchOptions): Promise; getStatistics(ownerId: string, options: AssetStatsOptions): Promise; getTimeBuckets(options: TimeBucketOptions): Promise; diff --git a/server/src/interfaces/event.interface.ts b/server/src/interfaces/event.interface.ts index eced261dbe..bc5ce90f40 100644 --- a/server/src/interfaces/event.interface.ts +++ b/server/src/interfaces/event.interface.ts @@ -27,6 +27,7 @@ type EmitEventMap = { // asset bulk events 'assets.trash': [{ assetIds: string[]; userId: string }]; + 'assets.delete': [{ assetIds: string[]; userId: string }]; 'assets.restore': [{ assetIds: string[]; userId: string }]; // session events diff --git a/server/src/interfaces/job.interface.ts b/server/src/interfaces/job.interface.ts index 79d3f1560b..3e7b0b9d08 100644 --- a/server/src/interfaces/job.interface.ts +++ b/server/src/interfaces/job.interface.ts @@ -93,6 +93,8 @@ export enum JobName { QUEUE_SMART_SEARCH = 'queue-smart-search', SMART_SEARCH = 'smart-search', + QUEUE_TRASH_EMPTY = 'queue-trash-empty', + // duplicate detection QUEUE_DUPLICATE_DETECTION = 'queue-duplicate-detection', DUPLICATE_DETECTION = 'duplicate-detection', @@ -253,6 +255,7 @@ export type JobItem = // Smart Search | { name: JobName.QUEUE_SMART_SEARCH; data: IBaseJob } | { name: JobName.SMART_SEARCH; data: IEntityJob } + | { name: JobName.QUEUE_TRASH_EMPTY; data?: IBaseJob } // Duplicate Detection | { name: JobName.QUEUE_DUPLICATE_DETECTION; data: IBaseJob } diff --git a/server/src/interfaces/search.interface.ts b/server/src/interfaces/search.interface.ts index 323aaf1d00..6578d0a483 100644 --- a/server/src/interfaces/search.interface.ts +++ b/server/src/interfaces/search.interface.ts @@ -1,7 +1,7 @@ import { AssetFaceEntity } from 'src/entities/asset-face.entity'; import { AssetEntity } from 'src/entities/asset.entity'; import { GeodataPlacesEntity } from 'src/entities/geodata-places.entity'; -import { AssetType } from 'src/enum'; +import { AssetStatus, AssetType } from 'src/enum'; import { Paginated } from 'src/utils/pagination'; export const ISearchRepository = 'ISearchRepository'; @@ -61,6 +61,7 @@ export interface SearchStatusOptions { isVisible?: boolean; isNotInAlbum?: boolean; type?: AssetType; + status?: AssetStatus; withArchived?: boolean; withDeleted?: boolean; } diff --git a/server/src/interfaces/trash.interface.ts b/server/src/interfaces/trash.interface.ts new file mode 100644 index 0000000000..96c2322d8a --- /dev/null +++ b/server/src/interfaces/trash.interface.ts @@ -0,0 +1,10 @@ +import { Paginated, PaginationOptions } from 'src/utils/pagination'; + +export const ITrashRepository = 'ITrashRepository'; + +export interface ITrashRepository { + empty(userId: string): Promise; + restore(userId: string): Promise; + restoreAll(assetIds: string[]): Promise; + getDeletedIds(pagination: PaginationOptions): Paginated; +} diff --git a/server/src/migrations/1726593009549-AddAssetStatus.ts b/server/src/migrations/1726593009549-AddAssetStatus.ts new file mode 100644 index 0000000000..5b243b05b5 --- /dev/null +++ b/server/src/migrations/1726593009549-AddAssetStatus.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddAssetStatus1726593009549 implements MigrationInterface { + name = 'AddAssetStatus1726593009549' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TYPE "assets_status_enum" AS ENUM('active', 'trashed', 'deleted')`); + await queryRunner.query(`ALTER TABLE "assets" ADD "status" "assets_status_enum" NOT NULL DEFAULT 'active'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "assets" DROP COLUMN "status"`); + await queryRunner.query(`DROP TYPE "assets_status_enum"`); + } + +} diff --git a/server/src/queries/asset.repository.sql b/server/src/queries/asset.repository.sql index 2ef5caee52..5b57307179 100644 --- a/server/src/queries/asset.repository.sql +++ b/server/src/queries/asset.repository.sql @@ -8,6 +8,7 @@ SELECT "entity"."libraryId" AS "entity_libraryId", "entity"."deviceId" AS "entity_deviceId", "entity"."type" AS "entity_type", + "entity"."status" AS "entity_status", "entity"."originalPath" AS "entity_originalPath", "entity"."thumbhash" AS "entity_thumbhash", "entity"."encodedVideoPath" AS "entity_encodedVideoPath", @@ -96,6 +97,7 @@ SELECT "AssetEntity"."libraryId" AS "AssetEntity_libraryId", "AssetEntity"."deviceId" AS "AssetEntity_deviceId", "AssetEntity"."type" AS "AssetEntity_type", + "AssetEntity"."status" AS "AssetEntity_status", "AssetEntity"."originalPath" AS "AssetEntity_originalPath", "AssetEntity"."thumbhash" AS "AssetEntity_thumbhash", "AssetEntity"."encodedVideoPath" AS "AssetEntity_encodedVideoPath", @@ -130,6 +132,7 @@ SELECT "AssetEntity"."libraryId" AS "AssetEntity_libraryId", "AssetEntity"."deviceId" AS "AssetEntity_deviceId", "AssetEntity"."type" AS "AssetEntity_type", + "AssetEntity"."status" AS "AssetEntity_status", "AssetEntity"."originalPath" AS "AssetEntity_originalPath", "AssetEntity"."thumbhash" AS "AssetEntity_thumbhash", "AssetEntity"."encodedVideoPath" AS "AssetEntity_encodedVideoPath", @@ -218,6 +221,7 @@ SELECT "bd93d5747511a4dad4923546c51365bf1a803774"."libraryId" AS "bd93d5747511a4dad4923546c51365bf1a803774_libraryId", "bd93d5747511a4dad4923546c51365bf1a803774"."deviceId" AS "bd93d5747511a4dad4923546c51365bf1a803774_deviceId", "bd93d5747511a4dad4923546c51365bf1a803774"."type" AS "bd93d5747511a4dad4923546c51365bf1a803774_type", + "bd93d5747511a4dad4923546c51365bf1a803774"."status" AS "bd93d5747511a4dad4923546c51365bf1a803774_status", "bd93d5747511a4dad4923546c51365bf1a803774"."originalPath" AS "bd93d5747511a4dad4923546c51365bf1a803774_originalPath", "bd93d5747511a4dad4923546c51365bf1a803774"."thumbhash" AS "bd93d5747511a4dad4923546c51365bf1a803774_thumbhash", "bd93d5747511a4dad4923546c51365bf1a803774"."encodedVideoPath" AS "bd93d5747511a4dad4923546c51365bf1a803774_encodedVideoPath", @@ -305,6 +309,7 @@ FROM "AssetEntity"."libraryId" AS "AssetEntity_libraryId", "AssetEntity"."deviceId" AS "AssetEntity_deviceId", "AssetEntity"."type" AS "AssetEntity_type", + "AssetEntity"."status" AS "AssetEntity_status", "AssetEntity"."originalPath" AS "AssetEntity_originalPath", "AssetEntity"."thumbhash" AS "AssetEntity_thumbhash", "AssetEntity"."encodedVideoPath" AS "AssetEntity_encodedVideoPath", @@ -402,6 +407,7 @@ SELECT "AssetEntity"."libraryId" AS "AssetEntity_libraryId", "AssetEntity"."deviceId" AS "AssetEntity_deviceId", "AssetEntity"."type" AS "AssetEntity_type", + "AssetEntity"."status" AS "AssetEntity_status", "AssetEntity"."originalPath" AS "AssetEntity_originalPath", "AssetEntity"."thumbhash" AS "AssetEntity_thumbhash", "AssetEntity"."encodedVideoPath" AS "AssetEntity_encodedVideoPath", @@ -455,6 +461,7 @@ SELECT "AssetEntity"."libraryId" AS "AssetEntity_libraryId", "AssetEntity"."deviceId" AS "AssetEntity_deviceId", "AssetEntity"."type" AS "AssetEntity_type", + "AssetEntity"."status" AS "AssetEntity_status", "AssetEntity"."originalPath" AS "AssetEntity_originalPath", "AssetEntity"."thumbhash" AS "AssetEntity_thumbhash", "AssetEntity"."encodedVideoPath" AS "AssetEntity_encodedVideoPath", @@ -527,6 +534,7 @@ SELECT "AssetEntity"."libraryId" AS "AssetEntity_libraryId", "AssetEntity"."deviceId" AS "AssetEntity_deviceId", "AssetEntity"."type" AS "AssetEntity_type", + "AssetEntity"."status" AS "AssetEntity_status", "AssetEntity"."originalPath" AS "AssetEntity_originalPath", "AssetEntity"."thumbhash" AS "AssetEntity_thumbhash", "AssetEntity"."encodedVideoPath" AS "AssetEntity_encodedVideoPath", @@ -581,6 +589,7 @@ SELECT "asset"."libraryId" AS "asset_libraryId", "asset"."deviceId" AS "asset_deviceId", "asset"."type" AS "asset_type", + "asset"."status" AS "asset_status", "asset"."originalPath" AS "asset_originalPath", "asset"."thumbhash" AS "asset_thumbhash", "asset"."encodedVideoPath" AS "asset_encodedVideoPath", @@ -640,6 +649,7 @@ SELECT "stackedAssets"."libraryId" AS "stackedAssets_libraryId", "stackedAssets"."deviceId" AS "stackedAssets_deviceId", "stackedAssets"."type" AS "stackedAssets_type", + "stackedAssets"."status" AS "stackedAssets_status", "stackedAssets"."originalPath" AS "stackedAssets_originalPath", "stackedAssets"."thumbhash" AS "stackedAssets_thumbhash", "stackedAssets"."encodedVideoPath" AS "stackedAssets_encodedVideoPath", @@ -719,6 +729,7 @@ SELECT "asset"."libraryId" AS "asset_libraryId", "asset"."deviceId" AS "asset_deviceId", "asset"."type" AS "asset_type", + "asset"."status" AS "asset_status", "asset"."originalPath" AS "asset_originalPath", "asset"."thumbhash" AS "asset_thumbhash", "asset"."encodedVideoPath" AS "asset_encodedVideoPath", @@ -778,6 +789,7 @@ SELECT "stackedAssets"."libraryId" AS "stackedAssets_libraryId", "stackedAssets"."deviceId" AS "stackedAssets_deviceId", "stackedAssets"."type" AS "stackedAssets_type", + "stackedAssets"."status" AS "stackedAssets_status", "stackedAssets"."originalPath" AS "stackedAssets_originalPath", "stackedAssets"."thumbhash" AS "stackedAssets_thumbhash", "stackedAssets"."encodedVideoPath" AS "stackedAssets_encodedVideoPath", @@ -833,6 +845,7 @@ SELECT "asset"."libraryId" AS "asset_libraryId", "asset"."deviceId" AS "asset_deviceId", "asset"."type" AS "asset_type", + "asset"."status" AS "asset_status", "asset"."originalPath" AS "asset_originalPath", "asset"."thumbhash" AS "asset_thumbhash", "asset"."encodedVideoPath" AS "asset_encodedVideoPath", @@ -892,6 +905,7 @@ SELECT "stackedAssets"."libraryId" AS "stackedAssets_libraryId", "stackedAssets"."deviceId" AS "stackedAssets_deviceId", "stackedAssets"."type" AS "stackedAssets_type", + "stackedAssets"."status" AS "stackedAssets_status", "stackedAssets"."originalPath" AS "stackedAssets_originalPath", "stackedAssets"."thumbhash" AS "stackedAssets_thumbhash", "stackedAssets"."encodedVideoPath" AS "stackedAssets_encodedVideoPath", @@ -997,6 +1011,7 @@ SELECT "asset"."libraryId" AS "asset_libraryId", "asset"."deviceId" AS "asset_deviceId", "asset"."type" AS "asset_type", + "asset"."status" AS "asset_status", "asset"."originalPath" AS "asset_originalPath", "asset"."thumbhash" AS "asset_thumbhash", "asset"."encodedVideoPath" AS "asset_encodedVideoPath", @@ -1072,6 +1087,7 @@ SELECT "asset"."libraryId" AS "asset_libraryId", "asset"."deviceId" AS "asset_deviceId", "asset"."type" AS "asset_type", + "asset"."status" AS "asset_status", "asset"."originalPath" AS "asset_originalPath", "asset"."thumbhash" AS "asset_thumbhash", "asset"."encodedVideoPath" AS "asset_encodedVideoPath", diff --git a/server/src/queries/person.repository.sql b/server/src/queries/person.repository.sql index 95374b136d..fa8b0910b4 100644 --- a/server/src/queries/person.repository.sql +++ b/server/src/queries/person.repository.sql @@ -159,6 +159,7 @@ FROM "AssetFaceEntity__AssetFaceEntity_asset"."libraryId" AS "AssetFaceEntity__AssetFaceEntity_asset_libraryId", "AssetFaceEntity__AssetFaceEntity_asset"."deviceId" AS "AssetFaceEntity__AssetFaceEntity_asset_deviceId", "AssetFaceEntity__AssetFaceEntity_asset"."type" AS "AssetFaceEntity__AssetFaceEntity_asset_type", + "AssetFaceEntity__AssetFaceEntity_asset"."status" AS "AssetFaceEntity__AssetFaceEntity_asset_status", "AssetFaceEntity__AssetFaceEntity_asset"."originalPath" AS "AssetFaceEntity__AssetFaceEntity_asset_originalPath", "AssetFaceEntity__AssetFaceEntity_asset"."thumbhash" AS "AssetFaceEntity__AssetFaceEntity_asset_thumbhash", "AssetFaceEntity__AssetFaceEntity_asset"."encodedVideoPath" AS "AssetFaceEntity__AssetFaceEntity_asset_encodedVideoPath", @@ -260,6 +261,7 @@ FROM "AssetEntity"."libraryId" AS "AssetEntity_libraryId", "AssetEntity"."deviceId" AS "AssetEntity_deviceId", "AssetEntity"."type" AS "AssetEntity_type", + "AssetEntity"."status" AS "AssetEntity_status", "AssetEntity"."originalPath" AS "AssetEntity_originalPath", "AssetEntity"."thumbhash" AS "AssetEntity_thumbhash", "AssetEntity"."encodedVideoPath" AS "AssetEntity_encodedVideoPath", @@ -391,6 +393,7 @@ SELECT "AssetFaceEntity__AssetFaceEntity_asset"."libraryId" AS "AssetFaceEntity__AssetFaceEntity_asset_libraryId", "AssetFaceEntity__AssetFaceEntity_asset"."deviceId" AS "AssetFaceEntity__AssetFaceEntity_asset_deviceId", "AssetFaceEntity__AssetFaceEntity_asset"."type" AS "AssetFaceEntity__AssetFaceEntity_asset_type", + "AssetFaceEntity__AssetFaceEntity_asset"."status" AS "AssetFaceEntity__AssetFaceEntity_asset_status", "AssetFaceEntity__AssetFaceEntity_asset"."originalPath" AS "AssetFaceEntity__AssetFaceEntity_asset_originalPath", "AssetFaceEntity__AssetFaceEntity_asset"."thumbhash" AS "AssetFaceEntity__AssetFaceEntity_asset_thumbhash", "AssetFaceEntity__AssetFaceEntity_asset"."encodedVideoPath" AS "AssetFaceEntity__AssetFaceEntity_asset_encodedVideoPath", diff --git a/server/src/queries/search.repository.sql b/server/src/queries/search.repository.sql index 6658a47835..58b2999012 100644 --- a/server/src/queries/search.repository.sql +++ b/server/src/queries/search.repository.sql @@ -13,6 +13,7 @@ FROM "asset"."libraryId" AS "asset_libraryId", "asset"."deviceId" AS "asset_deviceId", "asset"."type" AS "asset_type", + "asset"."status" AS "asset_status", "asset"."originalPath" AS "asset_originalPath", "asset"."thumbhash" AS "asset_thumbhash", "asset"."encodedVideoPath" AS "asset_encodedVideoPath", @@ -43,6 +44,7 @@ FROM "stackedAssets"."libraryId" AS "stackedAssets_libraryId", "stackedAssets"."deviceId" AS "stackedAssets_deviceId", "stackedAssets"."type" AS "stackedAssets_type", + "stackedAssets"."status" AS "stackedAssets_status", "stackedAssets"."originalPath" AS "stackedAssets_originalPath", "stackedAssets"."thumbhash" AS "stackedAssets_thumbhash", "stackedAssets"."encodedVideoPath" AS "stackedAssets_encodedVideoPath", @@ -106,6 +108,7 @@ SELECT "asset"."libraryId" AS "asset_libraryId", "asset"."deviceId" AS "asset_deviceId", "asset"."type" AS "asset_type", + "asset"."status" AS "asset_status", "asset"."originalPath" AS "asset_originalPath", "asset"."thumbhash" AS "asset_thumbhash", "asset"."encodedVideoPath" AS "asset_encodedVideoPath", @@ -136,6 +139,7 @@ SELECT "stackedAssets"."libraryId" AS "stackedAssets_libraryId", "stackedAssets"."deviceId" AS "stackedAssets_deviceId", "stackedAssets"."type" AS "stackedAssets_type", + "stackedAssets"."status" AS "stackedAssets_status", "stackedAssets"."originalPath" AS "stackedAssets_originalPath", "stackedAssets"."thumbhash" AS "stackedAssets_thumbhash", "stackedAssets"."encodedVideoPath" AS "stackedAssets_encodedVideoPath", @@ -345,6 +349,7 @@ SELECT "asset"."libraryId" AS "asset_libraryId", "asset"."deviceId" AS "asset_deviceId", "asset"."type" AS "asset_type", + "asset"."status" AS "asset_status", "asset"."originalPath" AS "asset_originalPath", "asset"."thumbhash" AS "asset_thumbhash", "asset"."encodedVideoPath" AS "asset_encodedVideoPath", diff --git a/server/src/queries/shared.link.repository.sql b/server/src/queries/shared.link.repository.sql index f3b3c3140d..a19b698f76 100644 --- a/server/src/queries/shared.link.repository.sql +++ b/server/src/queries/shared.link.repository.sql @@ -27,6 +27,7 @@ FROM "SharedLinkEntity__SharedLinkEntity_assets"."libraryId" AS "SharedLinkEntity__SharedLinkEntity_assets_libraryId", "SharedLinkEntity__SharedLinkEntity_assets"."deviceId" AS "SharedLinkEntity__SharedLinkEntity_assets_deviceId", "SharedLinkEntity__SharedLinkEntity_assets"."type" AS "SharedLinkEntity__SharedLinkEntity_assets_type", + "SharedLinkEntity__SharedLinkEntity_assets"."status" AS "SharedLinkEntity__SharedLinkEntity_assets_status", "SharedLinkEntity__SharedLinkEntity_assets"."originalPath" AS "SharedLinkEntity__SharedLinkEntity_assets_originalPath", "SharedLinkEntity__SharedLinkEntity_assets"."thumbhash" AS "SharedLinkEntity__SharedLinkEntity_assets_thumbhash", "SharedLinkEntity__SharedLinkEntity_assets"."encodedVideoPath" AS "SharedLinkEntity__SharedLinkEntity_assets_encodedVideoPath", @@ -93,6 +94,7 @@ FROM "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."libraryId" AS "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6_libraryId", "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."deviceId" AS "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6_deviceId", "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."type" AS "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6_type", + "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."status" AS "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6_status", "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."originalPath" AS "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6_originalPath", "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."thumbhash" AS "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6_thumbhash", "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."encodedVideoPath" AS "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6_encodedVideoPath", @@ -214,6 +216,7 @@ SELECT "SharedLinkEntity__SharedLinkEntity_assets"."libraryId" AS "SharedLinkEntity__SharedLinkEntity_assets_libraryId", "SharedLinkEntity__SharedLinkEntity_assets"."deviceId" AS "SharedLinkEntity__SharedLinkEntity_assets_deviceId", "SharedLinkEntity__SharedLinkEntity_assets"."type" AS "SharedLinkEntity__SharedLinkEntity_assets_type", + "SharedLinkEntity__SharedLinkEntity_assets"."status" AS "SharedLinkEntity__SharedLinkEntity_assets_status", "SharedLinkEntity__SharedLinkEntity_assets"."originalPath" AS "SharedLinkEntity__SharedLinkEntity_assets_originalPath", "SharedLinkEntity__SharedLinkEntity_assets"."thumbhash" AS "SharedLinkEntity__SharedLinkEntity_assets_thumbhash", "SharedLinkEntity__SharedLinkEntity_assets"."encodedVideoPath" AS "SharedLinkEntity__SharedLinkEntity_assets_encodedVideoPath", diff --git a/server/src/queries/view.repository.sql b/server/src/queries/view.repository.sql index 5542f782c7..e5b88ffef9 100644 --- a/server/src/queries/view.repository.sql +++ b/server/src/queries/view.repository.sql @@ -8,6 +8,7 @@ SELECT "asset"."libraryId" AS "asset_libraryId", "asset"."deviceId" AS "asset_deviceId", "asset"."type" AS "asset_type", + "asset"."status" AS "asset_status", "asset"."originalPath" AS "asset_originalPath", "asset"."thumbhash" AS "asset_thumbhash", "asset"."encodedVideoPath" AS "asset_encodedVideoPath", diff --git a/server/src/repositories/asset.repository.ts b/server/src/repositories/asset.repository.ts index d23389356d..4ec5523df1 100644 --- a/server/src/repositories/asset.repository.ts +++ b/server/src/repositories/asset.repository.ts @@ -6,7 +6,7 @@ import { AssetJobStatusEntity } from 'src/entities/asset-job-status.entity'; import { AssetEntity } from 'src/entities/asset.entity'; import { ExifEntity } from 'src/entities/exif.entity'; import { SmartInfoEntity } from 'src/entities/smart-info.entity'; -import { AssetFileType, AssetOrder, AssetType } from 'src/enum'; +import { AssetFileType, AssetOrder, AssetStatus, AssetType } from 'src/enum'; import { AssetBuilderOptions, AssetCreate, @@ -295,16 +295,6 @@ export class AssetRepository implements IAssetRepository { .execute(); } - @Chunked() - async softDeleteAll(ids: string[]): Promise { - await this.repository.softDelete({ id: In(ids) }); - } - - @Chunked() - async restoreAll(ids: string[]): Promise { - await this.repository.restore({ id: In(ids) }); - } - async update(asset: AssetUpdateOptions): Promise { await this.repository.update(asset.id, asset); } @@ -597,7 +587,10 @@ export class AssetRepository implements IAssetRepository { } if (isTrashed !== undefined) { - builder.withDeleted().andWhere(`asset.deletedAt is not null`); + builder + .withDeleted() + .andWhere(`asset.deletedAt is not null`) + .andWhere('asset.status = :status', { status: AssetStatus.TRASHED }); } const items = await builder.getRawMany(); @@ -755,6 +748,10 @@ export class AssetRepository implements IAssetRepository { if (options.isTrashed !== undefined) { builder.andWhere(`asset.deletedAt ${options.isTrashed ? 'IS NOT NULL' : 'IS NULL'}`).withDeleted(); + + if (options.isTrashed) { + builder.andWhere('asset.status = :status', { status: AssetStatus.TRASHED }); + } } if (options.isDuplicate !== undefined) { diff --git a/server/src/repositories/index.ts b/server/src/repositories/index.ts index 6f19914d40..7082fc031f 100644 --- a/server/src/repositories/index.ts +++ b/server/src/repositories/index.ts @@ -29,6 +29,7 @@ import { IStackRepository } from 'src/interfaces/stack.interface'; import { IStorageRepository } from 'src/interfaces/storage.interface'; import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface'; import { ITagRepository } from 'src/interfaces/tag.interface'; +import { ITrashRepository } from 'src/interfaces/trash.interface'; import { IUserRepository } from 'src/interfaces/user.interface'; import { IViewRepository } from 'src/interfaces/view.interface'; import { AccessRepository } from 'src/repositories/access.repository'; @@ -62,6 +63,7 @@ import { StackRepository } from 'src/repositories/stack.repository'; import { StorageRepository } from 'src/repositories/storage.repository'; import { SystemMetadataRepository } from 'src/repositories/system-metadata.repository'; import { TagRepository } from 'src/repositories/tag.repository'; +import { TrashRepository } from 'src/repositories/trash.repository'; import { UserRepository } from 'src/repositories/user.repository'; import { ViewRepository } from 'src/repositories/view-repository'; @@ -97,6 +99,7 @@ export const repositories = [ { provide: IStorageRepository, useClass: StorageRepository }, { provide: ISystemMetadataRepository, useClass: SystemMetadataRepository }, { provide: ITagRepository, useClass: TagRepository }, + { provide: ITrashRepository, useClass: TrashRepository }, { provide: IUserRepository, useClass: UserRepository }, { provide: IViewRepository, useClass: ViewRepository }, ]; diff --git a/server/src/repositories/job.repository.ts b/server/src/repositories/job.repository.ts index f82a752acf..2b4c1f6dc1 100644 --- a/server/src/repositories/job.repository.ts +++ b/server/src/repositories/job.repository.ts @@ -95,6 +95,9 @@ export const JOBS_TO_QUEUE: Record = { // Version check [JobName.VERSION_CHECK]: QueueName.BACKGROUND_TASK, + + // Trash + [JobName.QUEUE_TRASH_EMPTY]: QueueName.BACKGROUND_TASK, }; @Instrumentation() diff --git a/server/src/repositories/trash.repository.ts b/server/src/repositories/trash.repository.ts new file mode 100644 index 0000000000..9e0f6728f1 --- /dev/null +++ b/server/src/repositories/trash.repository.ts @@ -0,0 +1,49 @@ +import { InjectRepository } from '@nestjs/typeorm'; +import { AssetEntity } from 'src/entities/asset.entity'; +import { AssetStatus } from 'src/enum'; +import { ITrashRepository } from 'src/interfaces/trash.interface'; +import { Paginated, paginatedBuilder, PaginationOptions } from 'src/utils/pagination'; +import { In, IsNull, Not, Repository } from 'typeorm'; + +export class TrashRepository implements ITrashRepository { + constructor(@InjectRepository(AssetEntity) private assetRepository: Repository) {} + + async getDeletedIds(pagination: PaginationOptions): Paginated { + const { hasNextPage, items } = await paginatedBuilder( + this.assetRepository + .createQueryBuilder('asset') + .select('asset.id') + .where({ status: AssetStatus.DELETED }) + .withDeleted(), + pagination, + ); + + return { + hasNextPage, + items: items.map((asset) => asset.id), + }; + } + + async restore(userId: string): Promise { + const result = await this.assetRepository.update( + { ownerId: userId, deletedAt: Not(IsNull()) }, + { status: AssetStatus.ACTIVE, deletedAt: null }, + ); + + return result.affected || 0; + } + + async empty(userId: string): Promise { + const result = await this.assetRepository.update( + { ownerId: userId, deletedAt: Not(IsNull()), status: AssetStatus.TRASHED }, + { status: AssetStatus.DELETED }, + ); + + return result.affected || 0; + } + + async restoreAll(ids: string[]): Promise { + const result = await this.assetRepository.update({ id: In(ids) }, { status: AssetStatus.ACTIVE, deletedAt: null }); + return result.affected ?? 0; + } +} diff --git a/server/src/services/asset-media.service.spec.ts b/server/src/services/asset-media.service.spec.ts index 9d6f0ff9cf..d7f0c6da0f 100644 --- a/server/src/services/asset-media.service.spec.ts +++ b/server/src/services/asset-media.service.spec.ts @@ -4,7 +4,7 @@ import { AssetMediaStatus, AssetRejectReason, AssetUploadAction } from 'src/dtos import { AssetMediaCreateDto, AssetMediaReplaceDto, UploadFieldName } from 'src/dtos/asset-media.dto'; import { AssetFileEntity } from 'src/entities/asset-files.entity'; import { ASSET_CHECKSUM_CONSTRAINT, AssetEntity } from 'src/entities/asset.entity'; -import { AssetType } from 'src/enum'; +import { AssetStatus, AssetType } from 'src/enum'; import { IAssetRepository } from 'src/interfaces/asset.interface'; import { IEventRepository } from 'src/interfaces/event.interface'; import { IJobRepository, JobName } from 'src/interfaces/job.interface'; @@ -478,7 +478,10 @@ describe(AssetMediaService.name, () => { }), ); - expect(assetMock.softDeleteAll).toHaveBeenCalledWith([copiedAsset.id]); + expect(assetMock.updateAll).toHaveBeenCalledWith([copiedAsset.id], { + deletedAt: expect.any(Date), + status: AssetStatus.TRASHED, + }); expect(userMock.updateUsage).toHaveBeenCalledWith(authStub.user1.user.id, updatedFile.size); expect(storageMock.utimes).toHaveBeenCalledWith( updatedFile.originalPath, @@ -506,7 +509,10 @@ describe(AssetMediaService.name, () => { id: 'copied-asset', }); - expect(assetMock.softDeleteAll).toHaveBeenCalledWith(['copied-asset']); + expect(assetMock.updateAll).toHaveBeenCalledWith([copiedAsset.id], { + deletedAt: expect.any(Date), + status: AssetStatus.TRASHED, + }); expect(userMock.updateUsage).toHaveBeenCalledWith(authStub.user1.user.id, updatedFile.size); expect(storageMock.utimes).toHaveBeenCalledWith( updatedFile.originalPath, @@ -532,7 +538,10 @@ describe(AssetMediaService.name, () => { id: 'copied-asset', }); - expect(assetMock.softDeleteAll).toHaveBeenCalledWith(['copied-asset']); + expect(assetMock.updateAll).toHaveBeenCalledWith([copiedAsset.id], { + deletedAt: expect.any(Date), + status: AssetStatus.TRASHED, + }); expect(userMock.updateUsage).toHaveBeenCalledWith(authStub.user1.user.id, updatedFile.size); expect(storageMock.utimes).toHaveBeenCalledWith( updatedFile.originalPath, @@ -561,7 +570,7 @@ describe(AssetMediaService.name, () => { }); expect(assetMock.create).not.toHaveBeenCalled(); - expect(assetMock.softDeleteAll).not.toHaveBeenCalled(); + expect(assetMock.updateAll).not.toHaveBeenCalled(); expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.DELETE_FILES, data: { files: [updatedFile.originalPath, undefined] }, diff --git a/server/src/services/asset-media.service.ts b/server/src/services/asset-media.service.ts index df3b183442..5321c335a7 100644 --- a/server/src/services/asset-media.service.ts +++ b/server/src/services/asset-media.service.ts @@ -27,7 +27,7 @@ import { } from 'src/dtos/asset-media.dto'; import { AuthDto } from 'src/dtos/auth.dto'; import { ASSET_CHECKSUM_CONSTRAINT, AssetEntity } from 'src/entities/asset.entity'; -import { AssetType, Permission } from 'src/enum'; +import { AssetStatus, AssetType, Permission } from 'src/enum'; import { IAccessRepository } from 'src/interfaces/access.interface'; import { IAssetRepository } from 'src/interfaces/asset.interface'; import { IEventRepository } from 'src/interfaces/event.interface'; @@ -193,7 +193,7 @@ export class AssetMediaService { // but the local variable holds the original file data paths. const copiedPhoto = await this.createCopy(asset); // and immediate trash it - await this.assetRepository.softDeleteAll([copiedPhoto.id]); + await this.assetRepository.updateAll([copiedPhoto.id], { deletedAt: new Date(), status: AssetStatus.TRASHED }); await this.eventRepository.emit('asset.trash', { assetId: copiedPhoto.id, userId: auth.user.id }); await this.userRepository.updateUsage(auth.user.id, file.size); diff --git a/server/src/services/asset.service.spec.ts b/server/src/services/asset.service.spec.ts index 3ac7aa1c71..2e2d676939 100755 --- a/server/src/services/asset.service.spec.ts +++ b/server/src/services/asset.service.spec.ts @@ -2,7 +2,7 @@ import { BadRequestException } from '@nestjs/common'; import { mapAsset } from 'src/dtos/asset-response.dto'; import { AssetJobName, AssetStatsResponseDto } from 'src/dtos/asset.dto'; import { AssetEntity } from 'src/entities/asset.entity'; -import { AssetType } from 'src/enum'; +import { AssetStatus, AssetType } from 'src/enum'; import { AssetStats, IAssetRepository } from 'src/interfaces/asset.interface'; import { IEventRepository } from 'src/interfaces/event.interface'; import { IJobRepository, JobName } from 'src/interfaces/job.interface'; @@ -269,10 +269,10 @@ describe(AssetService.name, () => { await sut.deleteAll(authStub.user1, { ids: ['asset1', 'asset2'], force: true }); - expect(jobMock.queueAll).toHaveBeenCalledWith([ - { name: JobName.ASSET_DELETION, data: { id: 'asset1', deleteOnDisk: true } }, - { name: JobName.ASSET_DELETION, data: { id: 'asset2', deleteOnDisk: true } }, - ]); + expect(eventMock.emit).toHaveBeenCalledWith('assets.delete', { + assetIds: ['asset1', 'asset2'], + userId: 'user-id', + }); }); it('should soft delete a batch of assets', async () => { @@ -280,7 +280,10 @@ describe(AssetService.name, () => { await sut.deleteAll(authStub.user1, { ids: ['asset1', 'asset2'], force: false }); - expect(assetMock.softDeleteAll).toHaveBeenCalledWith(['asset1', 'asset2']); + expect(assetMock.updateAll).toHaveBeenCalledWith(['asset1', 'asset2'], { + deletedAt: expect.any(Date), + status: AssetStatus.TRASHED, + }); expect(jobMock.queue.mock.calls).toEqual([]); }); }); diff --git a/server/src/services/asset.service.ts b/server/src/services/asset.service.ts index 98d3dd1459..b3f824f226 100644 --- a/server/src/services/asset.service.ts +++ b/server/src/services/asset.service.ts @@ -20,7 +20,7 @@ import { import { AuthDto } from 'src/dtos/auth.dto'; import { MemoryLaneDto } from 'src/dtos/search.dto'; import { AssetEntity } from 'src/entities/asset.entity'; -import { Permission } from 'src/enum'; +import { AssetStatus, Permission } from 'src/enum'; import { IAccessRepository } from 'src/interfaces/access.interface'; import { IAssetRepository } from 'src/interfaces/asset.interface'; import { IEventRepository } from 'src/interfaces/event.interface'; @@ -302,18 +302,11 @@ export class AssetService { const { ids, force } = dto; await requireAccess(this.access, { auth, permission: Permission.ASSET_DELETE, ids }); - - if (force) { - await this.jobRepository.queueAll( - ids.map((id) => ({ - name: JobName.ASSET_DELETION, - data: { id, deleteOnDisk: true }, - })), - ); - } else { - await this.assetRepository.softDeleteAll(ids); - await this.eventRepository.emit('assets.trash', { assetIds: ids, userId: auth.user.id }); - } + await this.assetRepository.updateAll(ids, { + deletedAt: new Date(), + status: force ? AssetStatus.DELETED : AssetStatus.TRASHED, + }); + await this.eventRepository.emit(force ? 'assets.delete' : 'assets.trash', { assetIds: ids, userId: auth.user.id }); } async run(auth: AuthDto, dto: AssetJobsDto) { diff --git a/server/src/services/microservices.service.ts b/server/src/services/microservices.service.ts index df4b072d56..25bfc0fdd2 100644 --- a/server/src/services/microservices.service.ts +++ b/server/src/services/microservices.service.ts @@ -16,6 +16,7 @@ import { SmartInfoService } from 'src/services/smart-info.service'; import { StorageTemplateService } from 'src/services/storage-template.service'; import { StorageService } from 'src/services/storage.service'; import { TagService } from 'src/services/tag.service'; +import { TrashService } from 'src/services/trash.service'; import { UserService } from 'src/services/user.service'; import { VersionService } from 'src/services/version.service'; import { otelShutdown } from 'src/utils/instrumentation'; @@ -36,6 +37,7 @@ export class MicroservicesService { private storageTemplateService: StorageTemplateService, private storageService: StorageService, private tagService: TagService, + private trashService: TrashService, private userService: UserService, private duplicateService: DuplicateService, private versionService: VersionService, @@ -97,6 +99,7 @@ export class MicroservicesService { [JobName.NOTIFY_SIGNUP]: (data) => this.notificationService.handleUserSignup(data), [JobName.TAG_CLEANUP]: () => this.tagService.handleTagCleanup(), [JobName.VERSION_CHECK]: () => this.versionService.handleVersionCheck(), + [JobName.QUEUE_TRASH_EMPTY]: () => this.trashService.handleQueueEmptyTrash(), }); } diff --git a/server/src/services/trash.service.spec.ts b/server/src/services/trash.service.spec.ts index 5c0609956a..87821f028a 100644 --- a/server/src/services/trash.service.spec.ts +++ b/server/src/services/trash.service.spec.ts @@ -1,22 +1,24 @@ import { BadRequestException } from '@nestjs/common'; -import { IAssetRepository } from 'src/interfaces/asset.interface'; import { IEventRepository } from 'src/interfaces/event.interface'; -import { IJobRepository, JobName } from 'src/interfaces/job.interface'; +import { IJobRepository, JobName, JobStatus } from 'src/interfaces/job.interface'; +import { ILoggerRepository } from 'src/interfaces/logger.interface'; +import { ITrashRepository } from 'src/interfaces/trash.interface'; import { TrashService } from 'src/services/trash.service'; -import { assetStub } from 'test/fixtures/asset.stub'; import { authStub } from 'test/fixtures/auth.stub'; import { IAccessRepositoryMock, newAccessRepositoryMock } from 'test/repositories/access.repository.mock'; -import { newAssetRepositoryMock } from 'test/repositories/asset.repository.mock'; import { newEventRepositoryMock } from 'test/repositories/event.repository.mock'; import { newJobRepositoryMock } from 'test/repositories/job.repository.mock'; +import { newLoggerRepositoryMock } from 'test/repositories/logger.repository.mock'; +import { newTrashRepositoryMock } from 'test/repositories/trash.repository.mock'; import { Mocked } from 'vitest'; describe(TrashService.name, () => { let sut: TrashService; let accessMock: IAccessRepositoryMock; - let assetMock: Mocked; - let jobMock: Mocked; let eventMock: Mocked; + let jobMock: Mocked; + let trashMock: Mocked; + let loggerMock: Mocked; it('should work', () => { expect(sut).toBeDefined(); @@ -24,11 +26,12 @@ describe(TrashService.name, () => { beforeEach(() => { accessMock = newAccessRepositoryMock(); - assetMock = newAssetRepositoryMock(); eventMock = newEventRepositoryMock(); jobMock = newJobRepositoryMock(); + trashMock = newTrashRepositoryMock(); + loggerMock = newLoggerRepositoryMock(); - sut = new TrashService(accessMock, assetMock, jobMock, eventMock); + sut = new TrashService(accessMock, eventMock, jobMock, trashMock, loggerMock); }); describe('restoreAssets', () => { @@ -40,44 +43,70 @@ describe(TrashService.name, () => { ).rejects.toBeInstanceOf(BadRequestException); }); + it('should handle an empty list', async () => { + await expect(sut.restoreAssets(authStub.user1, { ids: [] })).resolves.toEqual({ count: 0 }); + expect(accessMock.asset.checkOwnerAccess).not.toHaveBeenCalled(); + }); + it('should restore a batch of assets', async () => { accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset1', 'asset2'])); await sut.restoreAssets(authStub.user1, { ids: ['asset1', 'asset2'] }); - expect(assetMock.restoreAll).toHaveBeenCalledWith(['asset1', 'asset2']); + expect(trashMock.restoreAll).toHaveBeenCalledWith(['asset1', 'asset2']); expect(jobMock.queue.mock.calls).toEqual([]); }); }); describe('restore', () => { it('should handle an empty trash', async () => { - assetMock.getByUserId.mockResolvedValue({ items: [], hasNextPage: false }); - await expect(sut.restore(authStub.user1)).resolves.toBeUndefined(); - expect(assetMock.restoreAll).not.toHaveBeenCalled(); - expect(eventMock.clientSend).not.toHaveBeenCalled(); + trashMock.getDeletedIds.mockResolvedValue({ items: [], hasNextPage: false }); + trashMock.restore.mockResolvedValue(0); + await expect(sut.restore(authStub.user1)).resolves.toEqual({ count: 0 }); + expect(trashMock.restore).toHaveBeenCalledWith('user-id'); }); - it('should restore and notify', async () => { - assetMock.getByUserId.mockResolvedValue({ items: [assetStub.image], hasNextPage: false }); - await expect(sut.restore(authStub.user1)).resolves.toBeUndefined(); - expect(assetMock.restoreAll).toHaveBeenCalledWith([assetStub.image.id]); - expect(eventMock.emit).toHaveBeenCalledWith('assets.restore', { assetIds: ['asset-id'], userId: 'user-id' }); + it('should restore', async () => { + trashMock.getDeletedIds.mockResolvedValue({ items: ['asset-id'], hasNextPage: false }); + trashMock.restore.mockResolvedValue(1); + await expect(sut.restore(authStub.user1)).resolves.toEqual({ count: 1 }); + expect(trashMock.restore).toHaveBeenCalledWith('user-id'); }); }); describe('empty', () => { it('should handle an empty trash', async () => { - assetMock.getByUserId.mockResolvedValue({ items: [], hasNextPage: false }); - await expect(sut.empty(authStub.user1)).resolves.toBeUndefined(); - expect(jobMock.queueAll).toHaveBeenCalledWith([]); + trashMock.getDeletedIds.mockResolvedValue({ items: [], hasNextPage: false }); + trashMock.empty.mockResolvedValue(0); + await expect(sut.empty(authStub.user1)).resolves.toEqual({ count: 0 }); + expect(jobMock.queue).not.toHaveBeenCalled(); }); it('should empty the trash', async () => { - assetMock.getByUserId.mockResolvedValue({ items: [assetStub.image], hasNextPage: false }); - await expect(sut.empty(authStub.user1)).resolves.toBeUndefined(); + trashMock.getDeletedIds.mockResolvedValue({ items: ['asset-id'], hasNextPage: false }); + trashMock.empty.mockResolvedValue(1); + await expect(sut.empty(authStub.user1)).resolves.toEqual({ count: 1 }); + expect(trashMock.empty).toHaveBeenCalledWith('user-id'); + expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.QUEUE_TRASH_EMPTY, data: {} }); + }); + }); + + describe('onAssetsDelete', () => { + it('should queue the empty trash job', async () => { + await expect(sut.onAssetsDelete()).resolves.toBeUndefined(); + expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.QUEUE_TRASH_EMPTY, data: {} }); + }); + }); + + describe('handleQueueEmptyTrash', () => { + it('should queue asset delete jobs', async () => { + trashMock.getDeletedIds.mockResolvedValue({ items: ['asset-1'], hasNextPage: false }); + await expect(sut.handleQueueEmptyTrash()).resolves.toEqual(JobStatus.SUCCESS); expect(jobMock.queueAll).toHaveBeenCalledWith([ - { name: JobName.ASSET_DELETION, data: { id: assetStub.image.id, deleteOnDisk: true } }, + { + name: JobName.ASSET_DELETION, + data: { id: 'asset-1', deleteOnDisk: true }, + }, ]); }); }); diff --git a/server/src/services/trash.service.ts b/server/src/services/trash.service.ts index 712b9e50f2..88340f7d7c 100644 --- a/server/src/services/trash.service.ts +++ b/server/src/services/trash.service.ts @@ -1,69 +1,86 @@ import { Inject } from '@nestjs/common'; -import { DateTime } from 'luxon'; +import { OnEmit } from 'src/decorators'; import { BulkIdsDto } from 'src/dtos/asset-ids.response.dto'; import { AuthDto } from 'src/dtos/auth.dto'; +import { TrashResponseDto } from 'src/dtos/trash.dto'; import { Permission } from 'src/enum'; import { IAccessRepository } from 'src/interfaces/access.interface'; -import { IAssetRepository } from 'src/interfaces/asset.interface'; import { IEventRepository } from 'src/interfaces/event.interface'; -import { IJobRepository, JOBS_ASSET_PAGINATION_SIZE, JobName } from 'src/interfaces/job.interface'; +import { IJobRepository, JOBS_ASSET_PAGINATION_SIZE, JobName, JobStatus } from 'src/interfaces/job.interface'; +import { ILoggerRepository } from 'src/interfaces/logger.interface'; +import { ITrashRepository } from 'src/interfaces/trash.interface'; import { requireAccess } from 'src/utils/access'; import { usePagination } from 'src/utils/pagination'; export class TrashService { constructor( @Inject(IAccessRepository) private access: IAccessRepository, - @Inject(IAssetRepository) private assetRepository: IAssetRepository, - @Inject(IJobRepository) private jobRepository: IJobRepository, @Inject(IEventRepository) private eventRepository: IEventRepository, - ) {} + @Inject(IJobRepository) private jobRepository: IJobRepository, + @Inject(ITrashRepository) private trashRepository: ITrashRepository, + @Inject(ILoggerRepository) private logger: ILoggerRepository, + ) { + this.logger.setContext(TrashService.name); + } - async restoreAssets(auth: AuthDto, dto: BulkIdsDto): Promise { + async restoreAssets(auth: AuthDto, dto: BulkIdsDto): Promise { const { ids } = dto; - await requireAccess(this.access, { auth, permission: Permission.ASSET_DELETE, ids }); - await this.restoreAndSend(auth, ids); - } - - async restore(auth: AuthDto): Promise { - const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) => - this.assetRepository.getByUserId(pagination, auth.user.id, { - trashedBefore: DateTime.now().toJSDate(), - }), - ); - - for await (const assets of assetPagination) { - const ids = assets.map((a) => a.id); - await this.restoreAndSend(auth, ids); + if (ids.length === 0) { + return { count: 0 }; } + + await requireAccess(this.access, { auth, permission: Permission.ASSET_DELETE, ids }); + await this.trashRepository.restoreAll(ids); + await this.eventRepository.emit('assets.restore', { assetIds: ids, userId: auth.user.id }); + + this.logger.log(`Restored ${ids.length} assets from trash`); + + return { count: ids.length }; } - async empty(auth: AuthDto): Promise { + async restore(auth: AuthDto): Promise { + const count = await this.trashRepository.restore(auth.user.id); + if (count > 0) { + this.logger.log(`Restored ${count} assets from trash`); + } + return { count }; + } + + async empty(auth: AuthDto): Promise { + const count = await this.trashRepository.empty(auth.user.id); + if (count > 0) { + await this.jobRepository.queue({ name: JobName.QUEUE_TRASH_EMPTY, data: {} }); + } + return { count }; + } + + @OnEmit({ event: 'assets.delete' }) + async onAssetsDelete() { + await this.jobRepository.queue({ name: JobName.QUEUE_TRASH_EMPTY, data: {} }); + } + + async handleQueueEmptyTrash() { + let count = 0; const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) => - this.assetRepository.getByUserId(pagination, auth.user.id, { - trashedBefore: DateTime.now().toJSDate(), - withArchived: true, - }), + this.trashRepository.getDeletedIds(pagination), ); - for await (const assets of assetPagination) { + for await (const assetIds of assetPagination) { + this.logger.debug(`Queueing ${assetIds.length} assets for deletion from the trash`); + count += assetIds.length; await this.jobRepository.queueAll( - assets.map((asset) => ({ + assetIds.map((assetId) => ({ name: JobName.ASSET_DELETION, data: { - id: asset.id, + id: assetId, deleteOnDisk: true, }, })), ); } - } - private async restoreAndSend(auth: AuthDto, ids: string[]) { - if (ids.length === 0) { - return; - } + this.logger.log(`Queued ${count} assets for deletion from the trash`); - await this.assetRepository.restoreAll(ids); - await this.eventRepository.emit('assets.restore', { assetIds: ids, userId: auth.user.id }); + return JobStatus.SUCCESS; } } diff --git a/server/test/fixtures/asset.stub.ts b/server/test/fixtures/asset.stub.ts index 5ee42224ba..a9b5167909 100644 --- a/server/test/fixtures/asset.stub.ts +++ b/server/test/fixtures/asset.stub.ts @@ -2,7 +2,7 @@ import { AssetFileEntity } from 'src/entities/asset-files.entity'; import { AssetEntity } from 'src/entities/asset.entity'; import { ExifEntity } from 'src/entities/exif.entity'; import { StackEntity } from 'src/entities/stack.entity'; -import { AssetFileType, AssetType } from 'src/enum'; +import { AssetFileType, AssetStatus, AssetType } from 'src/enum'; import { authStub } from 'test/fixtures/auth.stub'; import { fileStub } from 'test/fixtures/file.stub'; import { libraryStub } from 'test/fixtures/library.stub'; @@ -42,6 +42,7 @@ export const stackStub = (stackId: string, assets: AssetEntity[]): StackEntity = export const assetStub = { noResizePath: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, originalFileName: 'IMG_123.jpg', deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -76,6 +77,7 @@ export const assetStub = { noWebpPath: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -83,7 +85,6 @@ export const assetStub = { ownerId: 'user-id', deviceId: 'device-id', originalPath: 'upload/library/IMG_456.jpg', - files: [previewFile], checksum: Buffer.from('file hash', 'utf8'), type: AssetType.IMAGE, @@ -114,6 +115,7 @@ export const assetStub = { noThumbhash: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -148,6 +150,7 @@ export const assetStub = { primaryImage: Object.freeze({ id: 'primary-asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -192,6 +195,7 @@ export const assetStub = { image: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -231,6 +235,7 @@ export const assetStub = { trashed: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -270,6 +275,7 @@ export const assetStub = { archived: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -309,6 +315,7 @@ export const assetStub = { external: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -348,6 +355,7 @@ export const assetStub = { offline: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -385,6 +393,7 @@ export const assetStub = { externalOffline: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -424,6 +433,7 @@ export const assetStub = { image1: Object.freeze({ id: 'asset-id-1', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -461,6 +471,7 @@ export const assetStub = { imageFrom2015: Object.freeze({ id: 'asset-id-1', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2015-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2015-02-23T05:06:29.716Z'), @@ -498,6 +509,7 @@ export const assetStub = { video: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, originalFileName: 'asset-id.ext', deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -536,6 +548,7 @@ export const assetStub = { }), livePhotoMotionAsset: Object.freeze({ + status: AssetStatus.ACTIVE, id: fileStub.livePhotoMotion.uuid, originalPath: fileStub.livePhotoMotion.originalPath, ownerId: authStub.user1.user.id, @@ -551,6 +564,7 @@ export const assetStub = { liveMotionWithThumb: Object.freeze({ id: fileStub.livePhotoMotion.uuid, + status: AssetStatus.ACTIVE, originalPath: fileStub.livePhotoMotion.originalPath, ownerId: authStub.user1.user.id, type: AssetType.VIDEO, @@ -581,6 +595,7 @@ export const assetStub = { livePhotoStillAsset: Object.freeze({ id: 'live-photo-still-asset', + status: AssetStatus.ACTIVE, originalPath: fileStub.livePhotoStill.originalPath, ownerId: authStub.user1.user.id, type: AssetType.IMAGE, @@ -596,6 +611,7 @@ export const assetStub = { livePhotoStillAssetWithTheSameLivePhotoMotionAsset: Object.freeze({ id: 'live-photo-still-asset-1', + status: AssetStatus.ACTIVE, originalPath: fileStub.livePhotoStill.originalPath, ownerId: authStub.user1.user.id, type: AssetType.IMAGE, @@ -611,6 +627,7 @@ export const assetStub = { livePhotoWithOriginalFileName: Object.freeze({ id: 'live-photo-still-asset', + status: AssetStatus.ACTIVE, originalPath: fileStub.livePhotoStill.originalPath, originalFileName: fileStub.livePhotoStill.originalName, ownerId: authStub.user1.user.id, @@ -627,6 +644,7 @@ export const assetStub = { withLocation: Object.freeze({ id: 'asset-with-favorite-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-22T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-22T05:06:29.716Z'), @@ -668,6 +686,7 @@ export const assetStub = { }), sidecar: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -701,6 +720,7 @@ export const assetStub = { }), sidecarWithoutExt: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -735,6 +755,7 @@ export const assetStub = { readOnly: Object.freeze({ id: 'read-only-asset', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -769,6 +790,7 @@ export const assetStub = { hasEncodedVideo: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, originalFileName: 'asset-id.ext', deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -805,6 +827,7 @@ export const assetStub = { }), missingFileExtension: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -843,6 +866,7 @@ export const assetStub = { }), hasFileExtension: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -881,6 +905,7 @@ export const assetStub = { }), imageDng: Object.freeze({ id: 'asset-id', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -919,6 +944,7 @@ export const assetStub = { }), hasEmbedding: Object.freeze({ id: 'asset-id-embedding', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), @@ -959,6 +985,7 @@ export const assetStub = { }), hasDupe: Object.freeze({ id: 'asset-id-dupe', + status: AssetStatus.ACTIVE, deviceAssetId: 'device-asset-id', fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'), fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'), diff --git a/server/test/fixtures/shared-link.stub.ts b/server/test/fixtures/shared-link.stub.ts index 54898d8693..f237e1dea9 100644 --- a/server/test/fixtures/shared-link.stub.ts +++ b/server/test/fixtures/shared-link.stub.ts @@ -5,7 +5,7 @@ import { SharedLinkResponseDto } from 'src/dtos/shared-link.dto'; import { mapUser } from 'src/dtos/user.dto'; import { SharedLinkEntity } from 'src/entities/shared-link.entity'; import { UserEntity } from 'src/entities/user.entity'; -import { AssetOrder, AssetType, SharedLinkType } from 'src/enum'; +import { AssetOrder, AssetStatus, AssetType, SharedLinkType } from 'src/enum'; import { assetStub } from 'test/fixtures/asset.stub'; import { authStub } from 'test/fixtures/auth.stub'; import { userStub } from 'test/fixtures/user.stub'; @@ -188,6 +188,7 @@ export const sharedLinkStub = { assets: [ { id: 'id_1', + status: AssetStatus.ACTIVE, owner: undefined as unknown as UserEntity, ownerId: 'user_id_1', deviceAssetId: 'device_asset_id_1', diff --git a/server/test/repositories/asset.repository.mock.ts b/server/test/repositories/asset.repository.mock.ts index 80b71fce9f..9ac568af30 100644 --- a/server/test/repositories/asset.repository.mock.ts +++ b/server/test/repositories/asset.repository.mock.ts @@ -34,8 +34,6 @@ export const newAssetRepositoryMock = (): Mocked => { getStatistics: vitest.fn(), getTimeBucket: vitest.fn(), getTimeBuckets: vitest.fn(), - restoreAll: vitest.fn(), - softDeleteAll: vitest.fn(), getAssetIdByCity: vitest.fn(), getAssetIdByTag: vitest.fn(), getAllForUserFullSync: vitest.fn(), diff --git a/server/test/repositories/trash.repository.mock.ts b/server/test/repositories/trash.repository.mock.ts new file mode 100644 index 0000000000..472b315b01 --- /dev/null +++ b/server/test/repositories/trash.repository.mock.ts @@ -0,0 +1,11 @@ +import { ITrashRepository } from 'src/interfaces/trash.interface'; +import { Mocked, vitest } from 'vitest'; + +export const newTrashRepositoryMock = (): Mocked => { + return { + empty: vitest.fn(), + restore: vitest.fn(), + restoreAll: vitest.fn(), + getDeletedIds: vitest.fn(), + }; +}; diff --git a/web/src/routes/(user)/trash/[[photos=photos]]/[[assetId=id]]/+page.svelte b/web/src/routes/(user)/trash/[[photos=photos]]/[[assetId=id]]/+page.svelte index 27ad5bb3f0..862d9382a4 100644 --- a/web/src/routes/(user)/trash/[[photos=photos]]/[[assetId=id]]/+page.svelte +++ b/web/src/routes/(user)/trash/[[photos=photos]]/[[assetId=id]]/+page.svelte @@ -33,7 +33,8 @@ handlePromiseError(goto(AppRoute.PHOTOS)); } - const assetStore = new AssetStore({ isTrashed: true }); + const options = { isTrashed: true }; + const assetStore = new AssetStore(options); const assetInteractionStore = createAssetInteractionStore(); const { isMultiSelectState, selectedAssets } = assetInteractionStore; @@ -47,16 +48,15 @@ } try { - await emptyTrash(); - - const deletedAssetIds = assetStore.assets.map((a) => a.id); - const numberOfAssets = deletedAssetIds.length; - assetStore.removeAssets(deletedAssetIds); + const { count } = await emptyTrash(); notificationController.show({ - message: $t('assets_permanently_deleted_count', { values: { count: numberOfAssets } }), + message: $t('assets_permanently_deleted_count', { values: { count } }), type: NotificationType.Info, }); + + // reset asset grid (TODO fix in asset store that it should reset when it is empty) + await assetStore.updateOptions(options); } catch (error) { handleError(error, $t('errors.unable_to_empty_trash')); } @@ -71,16 +71,14 @@ return; } try { - await restoreTrash(); - - const restoredAssetIds = assetStore.assets.map((a) => a.id); - const numberOfAssets = restoredAssetIds.length; - assetStore.removeAssets(restoredAssetIds); - + const { count } = await restoreTrash(); notificationController.show({ - message: $t('assets_restored_count', { values: { count: numberOfAssets } }), + message: $t('assets_restored_count', { values: { count } }), type: NotificationType.Info, }); + + // reset asset grid (TODO fix in asset store that it should reset when it is empty) + await assetStore.updateOptions(options); } catch (error) { handleError(error, $t('errors.unable_to_restore_trash')); }