Skip to content

Commit

Permalink
Hotfix: Reshare shared file (#62)
Browse files Browse the repository at this point in the history
* Temp work for fixing resharing

* Fix: Lookup Metadastore for sharing

* Remove test focus

Co-authored-by: jsonsivar <[email protected]>
  • Loading branch information
perfectmak and jsonsivar authored Feb 26, 2021
1 parent d73aaa7 commit 328679d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
25 changes: 25 additions & 0 deletions integration_tests/sharing_interactions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -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: '[email protected]',
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 () => {
Expand Down
27 changes: 13 additions & 14 deletions packages/storage/src/sharing/sharing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
26 changes: 23 additions & 3 deletions packages/storage/src/userStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -1237,13 +1238,32 @@ export class UserStorage {
fullPaths: FullPath[],
): Promise<{ key: string; fullPath: FullPath; }[]> {
const bucketCache = new Map<string, BucketMetadataWithThreads>();
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),
},
};
Expand Down

0 comments on commit 328679d

Please sign in to comment.