Skip to content

High performance self-hosted photo and video management solution.

fix: remove partner assets from existing memories (#28950)

shenlong6b7b0fe3a6b1
diff --git a/server/src/schema/migrations/1787148183730-DeleteMismatchedMemoryAssets.ts b/server/src/schema/migrations/1787148183730-DeleteMismatchedMemoryAssets.ts
new file mode 100644
index 0000000000..16efe3871a
--- /dev/null
+++ b/server/src/schema/migrations/1787148183730-DeleteMismatchedMemoryAssets.ts
@@ -0,0 +1,16 @@
+import { Kysely, sql } from 'kysely';
+
+export async function up(db: Kysely<any>): Promise<void> {
+ // Delete cross-owner memory assets
+ await sql`
+ DELETE FROM memory_asset
+ USING memory, asset
+ WHERE memory_asset."memoriesId" = memory.id
+ AND memory_asset."assetId" = asset.id
+ AND memory."ownerId" != asset."ownerId"
+ `.execute(db);
+}
+
+export async function down(): Promise<void> {
+ // Not implemented: the deleted rows were cross-owner entries
+}
diff --git a/server/src/services/album.service.ts b/server/src/services/album.service.ts
index a94049bbd2..8cf3106dd8 100644
--- a/server/src/services/album.service.ts
+++ b/server/src/services/album.service.ts
@@ -176,7 +176,7 @@ export class AlbumService extends BaseService {
const results = await addAssets(
auth,
{ access: this.accessRepository, bulk: this.albumRepository },
- { parentId: id, assetIds: dto.ids },
+ { parentId: id, assetIds: dto.ids, permission: Permission.AssetShare },
);
const { id: firstNewAssetId } = results.find(({ success }) => success) || {};
diff --git a/server/src/services/memory.service.ts b/server/src/services/memory.service.ts
index 1175a11af4..9ffbba8c1a 100644
--- a/server/src/services/memory.service.ts
+++ b/server/src/services/memory.service.ts
@@ -94,7 +94,7 @@ export class MemoryService extends BaseService {
const assetIds = dto.assetIds || [];
const allowedAssetIds = await this.checkAccess({
auth,
- permission: Permission.AssetShare,
+ permission: Permission.AssetUpdate,
ids: assetIds,
});
const memory = await this.memoryRepository.create(
@@ -135,7 +135,11 @@ export class MemoryService extends BaseService {
await this.requireAccess({ auth, permission: Permission.MemoryRead, ids: [id] });
const repos = { access: this.accessRepository, bulk: this.memoryRepository };
- const results = await addAssets(auth, repos, { parentId: id, assetIds: dto.ids });
+ const results = await addAssets(auth, repos, {
+ parentId: id,
+ assetIds: dto.ids,
+ permission: Permission.AssetUpdate,
+ });
const hasSuccess = results.some(({ success }) => success);
if (hasSuccess) {
diff --git a/server/src/services/tag.service.ts b/server/src/services/tag.service.ts
index f1fadd5c76..c2285a3a20 100644
--- a/server/src/services/tag.service.ts
+++ b/server/src/services/tag.service.ts
@@ -116,7 +116,7 @@ export class TagService extends BaseService {
const results = await addAssets(
auth,
{ access: this.accessRepository, bulk: this.tagRepository },
- { parentId: id, assetIds: dto.ids },
+ { parentId: id, assetIds: dto.ids, permission: Permission.AssetUpdate },
);
for (const { id: assetId, success } of results) {
diff --git a/server/src/utils/asset.util.ts b/server/src/utils/asset.util.ts
index 5420e60361..cca61dab11 100644
--- a/server/src/utils/asset.util.ts
+++ b/server/src/utils/asset.util.ts
@@ -33,14 +33,14 @@ export const getAssetFiles = (files: AssetFile[]) => ({
export const addAssets = async (
auth: AuthDto,
repositories: { access: AccessRepository; bulk: IBulkAsset },
- dto: { parentId: string; assetIds: string[] },
+ dto: { parentId: string; assetIds: string[]; permission: Permission },
) => {
const { access, bulk } = repositories;
const existingAssetIds = await bulk.getAssetIds(dto.parentId, dto.assetIds);
const notPresentAssetIds = dto.assetIds.filter((id) => !existingAssetIds.has(id));
const allowedAssetIds = await checkAccess(access, {
auth,
- permission: Permission.AssetShare,
+ permission: dto.permission,
ids: notPresentAssetIds,
});
diff --git a/server/test/medium/specs/services/memory.service.spec.ts b/server/test/medium/specs/services/memory.service.spec.ts
index f4a6e341bf..03670f5661 100644
--- a/server/test/medium/specs/services/memory.service.spec.ts
+++ b/server/test/medium/specs/services/memory.service.spec.ts
@@ -249,6 +249,43 @@ describe(MemoryService.name, () => {
}),
);
});
+
+ it('should not link a partner asset', async () => {
+ const { sut, ctx } = setup();
+ const { user: owner } = await ctx.newUser();
+ const { user: partner } = await ctx.newUser();
+ await ctx.newPartner({ sharedById: partner.id, sharedWithId: owner.id, inTimeline: true });
+ const { asset } = await ctx.newAsset({ ownerId: partner.id });
+ const auth = factory.auth({ user: owner });
+ const dto = {
+ type: MemoryType.OnThisDay,
+ data: { year: 2021 },
+ memoryAt: new Date(2021),
+ assetIds: [asset.id],
+ };
+
+ await expect(sut.create(auth, dto)).resolves.toEqual(expect.objectContaining({ assets: [] }));
+ });
+ });
+
+ describe('addAssets', () => {
+ it('should not link a partner asset', async () => {
+ const { sut, ctx } = setup();
+ const { user: owner } = await ctx.newUser();
+ const { user: partner } = await ctx.newUser();
+ await ctx.newPartner({ sharedById: partner.id, sharedWithId: owner.id, inTimeline: true });
+ const { asset } = await ctx.newAsset({ ownerId: partner.id });
+ const auth = factory.auth({ user: owner });
+ const memory = await sut.create(auth, {
+ type: MemoryType.OnThisDay,
+ data: { year: 2021 },
+ memoryAt: new Date(2021),
+ });
+
+ await expect(sut.addAssets(auth, memory.id, { ids: [asset.id] })).resolves.toEqual([
+ { id: asset.id, success: false, error: BulkIdErrorReason.NO_PERMISSION },
+ ]);
+ });
});
describe('onMemoryCreate', () => {
diff --git a/server/test/medium/specs/services/tag.service.spec.ts b/server/test/medium/specs/services/tag.service.spec.ts
index 52d002eed2..3ade80f4f1 100644
--- a/server/test/medium/specs/services/tag.service.spec.ts
+++ b/server/test/medium/specs/services/tag.service.spec.ts
@@ -1,4 +1,5 @@
import { Kysely } from 'kysely';
+import { BulkIdErrorReason } from 'src/dtos/asset-ids.response.dto';
import { JobStatus } from 'src/enum';
import { AccessRepository } from 'src/repositories/access.repository';
import { AssetRepository } from 'src/repositories/asset.repository';
@@ -101,6 +102,21 @@ describe(TagService.name, () => {
);
await expect(ctx.get(TagRepository).getAssetIds(tag.id, [asset.id])).resolves.toContain(asset.id);
});
+
+ it('should not tag a partner asset', async () => {
+ const { sut, ctx } = setup();
+ ctx.getMock(EventRepository).emit.mockResolvedValue();
+ const { user: owner } = await ctx.newUser();
+ const { user: partner } = await ctx.newUser();
+ await ctx.newPartner({ sharedById: partner.id, sharedWithId: owner.id, inTimeline: true });
+ const { asset } = await ctx.newAsset({ ownerId: partner.id });
+ const [tag] = await upsertTags(ctx.get(TagRepository), { userId: owner.id, tags: ['tag-1'] });
+ const authDto = factory.auth({ user: owner });
+
+ await expect(sut.addAssets(authDto, tag.id, { ids: [asset.id] })).resolves.toEqual([
+ { id: asset.id, success: false, error: BulkIdErrorReason.NO_PERMISSION },
+ ]);
+ });
});
describe('removeAssets', () => {