Sharing files and folders
The secrecyClient.cloud.shareNode method shares files or folders in the cloud with specific users or groups of your application, granting them certain access Rights.
It takes an array of accesses as its first parameter, and an optional progress callback as its second one. Each access describes the node and the recipient public key — not the user id — together with the granted rights. Use secrecyClient.app.userPublicKey to resolve a user id into their public key in your application, or secrecyClient.group.publicKeys to resolve group ids. See keys & identities.
// Rights 'read' | 'write' | 'delete'
import { type Rights } from '@secrecy/lib';
const shareFileOrFolder = async ({
nodeId,
rights,
secrecyUserId,
}: {
nodeId: string;
rights: Rights;
secrecyUserId: string;
}): Promise<boolean> => {
// First we need to check if the secrecyClient is available
if (!secrecyClient) {
return false;
}
try {
// sharing is done by public key, so the user public key is resolved first
const pubKey = await secrecyClient.app.userPublicKey(secrecyUserId);
const result = await secrecyClient.cloud.shareNode([
{
nodeId,
pubKey,
rights,
// rights the recipient is allowed to grant or revoke in turn
addAccess: null,
delAccess: null,
sharingAddAccess: null,
sharingDelAccess: null,
},
]);
// isFinished is false when some accesses could not be granted
return result.isFinished;
} catch (error) {
console.error(error);
return false;
}
};Each access grants one right level through rights, and four optional delegation rights: addAccess and delAccess (what the recipient may grant or revoke on the node) and sharingAddAccess and sharingDelAccess (what the recipient may grant or revoke on the sharing itself). Pass null to grant none of them.
To share several nodes or to share with several users at once, an access may also take nodeIds and/or pubKeys arrays instead of the singular fields.
The method resolves with { isFinished: true } when every access has been granted. When it resolves with isFinished: false, the details property lists the missing node accesses, the missing data accesses and the accesses whose rights differ from the expected ones.