Skip to content

fix(server): clear activity when asset is removed from album #19019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions e2e/src/api/specs/activity.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ReactionType,
createActivity as create,
createAlbum,
removeAssetFromAlbum,
} from '@immich/sdk';
import { createUserDto, uuidDto } from 'src/fixtures';
import { errorDto } from 'src/responses';
Expand Down Expand Up @@ -342,5 +343,36 @@ describe('/activities', () => {

expect(status).toBe(204);
});

it('should return empty list when asset is removed', async () => {
const album3 = await createAlbum(
{
createAlbumDto: {
albumName: 'Album 3',
assetIds: [asset.id],
},
},
{ headers: asBearerAuth(admin.accessToken) },
);

await createActivity({ albumId: album3.id, assetId: asset.id, type: ReactionType.Like });

await removeAssetFromAlbum(
{
id: album3.id,
bulkIdsDto: {
ids: [asset.id],
},
},
{ headers: asBearerAuth(admin.accessToken) },
);

const { status, body } = await request(app)
.get('/activities')
.query({ albumId: album.id })
.set('Authorization', `Bearer ${admin.accessToken}`);
expect(status).toEqual(200);
expect(body).toEqual([]);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Kysely, sql } from 'kysely';

export async function up(db: Kysely<any>): Promise<void> {
await sql`DELETE FROM activity AS a
WHERE a."assetId" IS NOT NULL
AND NOT EXISTS (
SELECT 1
FROM albums_assets_assets AS aaa
WHERE a."albumId" = aaa."albumsId"
AND a."assetId" = aaa."assetsId"
);`.execute(db);
await sql`ALTER TABLE "activity" ADD CONSTRAINT "fk_activity_album_asset_composite" FOREIGN KEY ("albumId", "assetId") REFERENCES "albums_assets_assets" ("albumsId", "assetsId") ON UPDATE NO ACTION ON DELETE CASCADE;`.execute(db);
await sql`CREATE INDEX "IDX_86102d85cfa7f196073aebff68" ON "activity" ("albumId", "assetId")`.execute(db);
}

export async function down(db: Kysely<any>): Promise<void> {
await sql`DROP INDEX "IDX_86102d85cfa7f196073aebff68";`.execute(db);
await sql`ALTER TABLE "activity" DROP CONSTRAINT "fk_activity_album_asset_composite";`.execute(db);
}
10 changes: 10 additions & 0 deletions server/src/schema/tables/activity.table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UpdatedAtTrigger, UpdateIdColumn } from 'src/decorators';
import { AlbumAssetTable } from 'src/schema/tables/album-asset.table';
import { AlbumTable } from 'src/schema/tables/album.table';
import { AssetTable } from 'src/schema/tables/asset.table';
import { UserTable } from 'src/schema/tables/user.table';
Expand All @@ -7,6 +8,7 @@ import {
Column,
CreateDateColumn,
ForeignKeyColumn,
ForeignKeyConstraint,
Index,
PrimaryGeneratedColumn,
Table,
Expand All @@ -25,6 +27,14 @@ import {
name: 'CHK_2ab1e70f113f450eb40c1e3ec8',
expression: `("comment" IS NULL AND "isLiked" = true) OR ("comment" IS NOT NULL AND "isLiked" = false)`,
})
@ForeignKeyConstraint({
name: 'fk_activity_album_asset_composite',
columns: ['albumId', 'assetId'],
referenceTable: () => AlbumAssetTable,
referenceColumns: ['albumsId', 'assetsId'],
onUpdate: 'NO ACTION',
onDelete: 'CASCADE',
})
export class ActivityTable {
@PrimaryGeneratedColumn()
id!: string;
Expand Down