diff --git a/integration_tests/sharing_interactions.spec.ts b/integration_tests/sharing_interactions.spec.ts index 3f749b1..d27695e 100644 --- a/integration_tests/sharing_interactions.spec.ts +++ b/integration_tests/sharing_interactions.spec.ts @@ -47,8 +47,10 @@ describe('Users sharing data', () => { it('users can share, accept and view shared files', async () => { const { user: user1 } = await authenticateAnonymousUser(); const { user: user2 } = await authenticateAnonymousUser(); + const { user: user3 } = await authenticateAnonymousUser(); const user1Pk = Buffer.from(user1.identity.public.pubKey).toString('hex'); const user2Pk = Buffer.from(user2.identity.public.pubKey).toString('hex'); + const user3Pk = Buffer.from(user3.identity.public.pubKey).toString('hex'); const txtContent = 'Some manual text should be in the file'; @@ -115,6 +117,7 @@ describe('Users sharing data', () => { expect(received.notifications[0].relatedObject?.inviterPublicKey).to.equal(user1Pk); expect(received.notifications[0].relatedObject?.itemPaths[0].bucket).to.equal('personal'); expect(received.notifications[0].relatedObject?.itemPaths[0].path).to.equal('/top.txt'); + console.log('dbid: ', received.notifications[0].relatedObject?.itemPaths[0].dbId); expect(received.notifications[0].relatedObject?.itemPaths[0].dbId).not.to.be.null; expect(received.notifications[0].relatedObject?.itemPaths[0].uuid).to.equal(ld.items[0].uuid); expect(received.notifications[0].relatedObject?.itemPaths[0].bucketKey).not.to.be.null; @@ -123,6 +126,28 @@ describe('Users sharing data', () => { // accept the notification await acceptNotification(storage2, received.notifications[0], user1); + + // reshare file + const storage3 = new UserStorage(user3, TestStorageConfig); + await storage3.initMailbox(); + + console.log('Resharing second time'); + const share2Result = await storage2.shareViaPublicKey({ + publicKeys: [{ + id: 'new-space-user-2@fleek.co', + pk: user3Pk, + }], + paths: [{ + bucket: 'personal', + path: '/top.txt', + dbId: received.notifications[0].relatedObject?.itemPaths[0].dbId, + }], + }); + + console.log('second share result: ', share2Result); + const secondReceivedNotifs = await storage3.getNotifications(); + // accept the notification + await acceptNotification(storage3, secondReceivedNotifs.notifications[0], user2); }).timeout(TestsDefaultTimeout); it('users can receive sharing notifications subscription events', async () => { diff --git a/packages/storage/src/sharing/sharing.ts b/packages/storage/src/sharing/sharing.ts index a1bac9e..3d16354 100644 --- a/packages/storage/src/sharing/sharing.ts +++ b/packages/storage/src/sharing/sharing.ts @@ -18,29 +18,28 @@ export const createFileInvitations = async ( const enhancedPaths:FullPath[] = []; const bucketsAndEnhancedPaths = await Promise.all(paths.map(async (path) => { - const b = await store.findBucket(path.bucket); - - if (!b) { - throw new Error('Unable to find bucket metadata'); - } - - const f = await store.findFileMetadata(path.bucket, path.dbId || b.dbId, path.path); - return [b, { + // const b = await store.findBucket(path.bucket); + // + // if (!b) { + // throw new Error('Unable to find bucket metadata'); + // } + + const f = await store.findFileMetadata(path.bucket, path.dbId || '', path.path); + const encryptionKey = f?.bucketKey; + return [encryptionKey, { ...path, uuid: f?.uuid, - dbId: b?.dbId, - bucketKey: b?.bucketKey, + dbId: f?.dbId, + bucketKey: f?.bucketKey, }]; })); - const keysP = bucketsAndEnhancedPaths.map((o) => (o[0] as BucketMetadata).encryptionKey); - const keys = await Promise.all(keysP); - + const keys = bucketsAndEnhancedPaths.map((o) => o[0] as string); const keysCleaned: Uint8Array[] = keys.map((k) => { if (!k) { throw new Error('Required encryption key not found'); } - return k; + return new TextEncoder().encode(k); }); pubkeys.forEach((pubkey) => { diff --git a/packages/storage/src/userStorage.ts b/packages/storage/src/userStorage.ts index 04c68dd..d344705 100644 --- a/packages/storage/src/userStorage.ts +++ b/packages/storage/src/userStorage.ts @@ -1126,6 +1126,7 @@ export class UserStorage { const roles = new Map(); const role = PathAccessRole.PATH_ACCESS_ROLE_ADMIN; roles.set(userKey.pk, role); + client.withThread(path.fullPath.dbId); await client.pushPathAccessRoles(path.key, path.fullPath.path, roles); } } @@ -1237,13 +1238,32 @@ export class UserStorage { fullPaths: FullPath[], ): Promise<{ key: string; fullPath: FullPath; }[]> { const bucketCache = new Map(); + const store = await this.getMetadataStore(); return Promise.all(fullPaths.map(async (fullPath) => { - const bucket = bucketCache.get(fullPath.bucket) || await this.getOrCreateBucket(client, fullPath.bucket); - bucketCache.set(fullPath.bucket, bucket); + let rootKey: string; + let { dbId } = fullPath; + if (dbId) { + const metadata = await store.findFileMetadata(fullPath.bucket, dbId, fullPath.path); + if (!metadata) { + throw new Error('Full Paths Bucket root not found'); + } + + rootKey = metadata.bucketKey || ''; + } else { + const bucket = await this.getOrCreateBucket(client, fullPath.bucket); + if (!bucket.root) { + throw new Error('Bucket root not found'); + } + + rootKey = bucket.root.key; + dbId = bucket.dbId; + } + return { - key: bucket.root?.key || '', + key: rootKey || '', fullPath: { ...fullPath, + dbId, path: sanitizePath(fullPath.path), }, };