Skip to content

Commit

Permalink
Merge pull request #150 from internxt/feature/PB-534-show-list-folder…
Browse files Browse the repository at this point in the history
…-you-have-shared

[PB-534]: feature/Show the list of folders you have shared with other users
  • Loading branch information
CandelR authored Aug 10, 2023
2 parents b78ec73 + 0f60edc commit b54e9f5
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@internxt/sdk",
"version": "1.4.39",
"version": "1.4.40",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down
207 changes: 207 additions & 0 deletions src/drive/share/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ import {
GenerateShareLinkPayload,
GetSharedDirectoryPayload,
GetShareLinkFolderSizePayload,
GrantSharePrivilegesToUserResponse,
ListAllSharedFoldersResponse,
ListPrivateSharedFoldersResponse,
ListShareLinksResponse,
PrivateSharedFolder,
PrivateSharingRolesResponse,
SharedFolderUser,
ShareDomainsResponse,
ShareLink,
SharePrivateFolderWithUserPayload,
UpdateUserRolePayload,
UpdateUserRoleResponse,
UpdateShareLinkPayload,
} from './types';
import { ApiSecurity, ApiUrl, AppDetails } from '../../shared';
Expand Down Expand Up @@ -137,6 +146,7 @@ export class Share {
public getShareDomains(): Promise<ShareDomainsResponse> {
return this.client.get('/storage/share/domains', this.headers());
}

/**
* Get size of folder in share links
* @param payload
Expand All @@ -145,6 +155,203 @@ export class Share {
return this.client.get(`/storage/share/${payload.itemId}/folder/${payload.folderId}/size`, this.basicHeaders());
}

/**
* Fetches all folders shared by a user.
*
* @param {number} page - The page number for pagination.
* @param {number} perPage - The number of items per page for pagination.
* @param {string} [orderBy] - The optional order criteria (e.g., 'views:ASC', 'createdAt:DESC').
* @returns {Promise<ListPrivateSharedFoldersResponse>} A promise containing the list of shared folders.
*/
public getSentSharedFolders(
page = 0,
perPage = 50,
orderBy?: 'views:ASC' | 'views:DESC' | 'createdAt:ASC' | 'createdAt:DESC',
): Promise<ListPrivateSharedFoldersResponse> {
const orderByQueryParam = orderBy ? `&orderBy=${orderBy}` : '';

return this.client.get(
`private-sharing/sent/folders?page=${page}&perPage=${perPage}${orderByQueryParam}`,
this.headers(),
);
}

/**
* Fetches folders shared with a user.
*
* @param {number} page - The page number for pagination.
* @param {number} perPage - The number of items per page for pagination.
* @param {string} [orderBy] - The optional order criteria (e.g., 'views:ASC', 'createdAt:DESC').
* @returns {Promise<ListPrivateSharedFoldersResponse>} A promise containing the list of shared folders.
*/
public getReceivedSharedFolders(
page = 0,
perPage = 50,
orderBy?: 'views:ASC' | 'views:DESC' | 'createdAt:ASC' | 'createdAt:DESC',
): Promise<ListPrivateSharedFoldersResponse> {
const orderByQueryParam = orderBy ? `&orderBy=${orderBy}` : '';

return this.client.get(
`private-sharing/receive/folders?page=${page}&perPage=${perPage}${orderByQueryParam}`,
this.headers(),
);
}

/**
* Fetches all shared folders.
*
* @param {number} page - The page number for pagination.
* @param {number} perPage - The number of items per page for pagination.
* @param {string} [orderBy] - The optional order criteria (e.g., 'views:ASC', 'createdAt:DESC').
* @returns {Promise<ListAllSharedFoldersResponse>} A promise containing the list of shared folders.
*/
public getAllSharedFolders(
page = 0,
perPage = 50,
orderBy?: 'views:ASC' | 'views:DESC' | 'createdAt:ASC' | 'createdAt:DESC',
): Promise<ListAllSharedFoldersResponse> {
const orderByQueryParam = orderBy ? `&orderBy=${orderBy}` : '';

return this.client.get(
`private-sharing/folders?page=${page}&perPage=${perPage}${orderByQueryParam}`,
this.headers(),
);
}

/**
* Get all users with access to a shared folder.
*
* @param {string} folderUUID - The UUID of the folder.
* @param {number} page - The page number for pagination.
* @param {number} perPage - The number of items per page for pagination.
* @param {string} [orderBy] - The optional order criteria (e.g., 'views:ASC', 'createdAt:DESC').
* @returns {Promise<{ users: SharedFolderUser[] }>} A promise containing the list of users with access to the folder.
*/
public getSharedFolderUsers(
folderUUID: string,
page = 0,
perPage = 50,
orderBy?: 'views:ASC' | 'views:DESC' | 'createdAt:ASC' | 'createdAt:DESC',
): Promise<{ users: SharedFolderUser[] }> {
const orderByQueryParam = orderBy ? `&orderBy=${orderBy}` : '';

return this.client.get(
`private-sharing/shared-with/by-folder-id/${folderUUID}?page=${page}&perPage=${perPage}${orderByQueryParam}`,
this.headers(),
);
}

/**
* Get private folder data.
*
* @param {string} folderUUID - The UUID of the folder.
* @returns {Promise<{ data: PrivateSharedFolder }>} A promise containing the private folder data.
*/
public getPrivateSharedFolder(folderUUID: string): Promise<{ data: PrivateSharedFolder }> {
return this.client.get(`private-sharing/by-folder-id/${folderUUID}`, this.headers());
}

/**
* Grant privileges of folder to a user.
*
* @param {string} userUuid - The UUID of the user.
* @param {string} privateFolderId - The UUID of the shared folder.
* @param {string} roleId - The id of the role.
* @returns {Promise<GrantSharePrivilegesToUserResponse>} A promise with message field
*/
public grantSharePrivilegesToUser(
userUuid: string,
privateFolderId: string,
roleId: string,
): Promise<GrantSharePrivilegesToUserResponse> {
return this.client.post(
'private-sharing/grant-privileges',
{
userUuid,
privateFolderId,
roleId,
},
this.headers(),
);
}

/**
* Update the role of a user on a folder.
*
* @param {UpdateUserRolePayload} options - The options for updating the user's role on the folder.
* @param {string} options.folderUUID - The unique identifier of the folder.
* @param {string} options.roleId - The identifier of the role to assign to the user.
* @param {string} options.userUUID - The email address of the user.
* @returns {Promise<UpdateRoleFolderResponse>} A promise that resolves when the user's role is updated.
*/
public updateUserRole({ folderUUID, roleId, userUUID }: UpdateUserRolePayload): Promise<UpdateUserRoleResponse> {
return this.client.put(
`private-sharing/role/${roleId}`,
{
folderId: folderUUID,
userId: userUUID,
},
this.headers(),
);
}

/**
* Share a private folder with a user.
*
* @param {SharePrivateFolderWithUserPayload} options - The options for sharing the private folder with a user.
* @param {string} options.emailToShare - The email address of the user to share the folder with.
* @param {string} options.privateFolderId - The unique identifier of the private folder.
* @param {string} options.roleId - The identifier of the role to assign to the user for the shared folder.
* @param {string} options.encryptionKey - The encryption key for the shared folder.
* @returns {Promise<void>} A promise that resolves when the folder is shared with the user.
*/
public sharePrivateFolderWithUser({
emailToShare,
privateFolderId,
roleId,
encryptionKey,
}: SharePrivateFolderWithUserPayload): Promise<void> {
return this.client.post(
'private-sharing/create',
{
email: emailToShare,
folderId: privateFolderId,
roleId,
encryptionKey,
},
this.headers(),
);
}

/**
* Fetches roles for private sharing items.
*
* @returns {Promise<PrivateSharingRolesResponse>} A promise containing the list of private sharing roles.
*/
public getPrivateSharingRoles(): Promise<PrivateSharingRolesResponse> {
return this.client.get('private-sharing/roles', this.headers());
}

/**
* Stop sharing folder
*
* @param {string} folderUUID - The unique identifier of the folder.
* @returns {Promise<{ stopped: boolean }>} A promise that resolves with an object indicating whether the sharing was stopped.
*/
public stopSharingFolder(folderUUID: string): Promise<{ stoped: boolean }> {
return this.client.delete(`/private-sharing/stop/folder-id/${folderUUID}`, this.headers());
}

/**
* Remove user from the shared users list of a folder.
* @param {string} folderUUID - The UUID of the shared folder.
* @param {string} userUUID - The UUID of the user to be removed.
* @returns {Promise<{ removed: boolean }>} A promise indicating whether the user was removed.
*/
public removeUserFromSharedFolder(folderUUID: string, userUUID: string): Promise<{ removed: boolean }> {
return this.client.delete(`/private-sharing/remove/folder-id/${folderUUID}/user-id/${userUUID}`, this.headers());
}

/**
* Returns the needed headers for the module requests
* @private
Expand Down
81 changes: 81 additions & 0 deletions src/drive/share/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FolderChild } from '../storage/types';

export interface GenerateShareLinkPayload {
itemId: string;
type: string;
Expand Down Expand Up @@ -86,3 +88,82 @@ export type ListShareLinksResponse = {
items: ListShareLinksItem[];
pagination: { page: number; perPage: number; countAll: number; orderBy?: string };
};

export type ListPrivateSharedFoldersResponse = {
folders: FolderChild[];
};

export type ListAllSharedFoldersResponse = {
sharedByMe: FolderChild[];
sharedWithMe: FolderChild[];
};

export type SharePrivateFolderWithUserPayload = {
emailToShare: string;
privateFolderId: FolderChild['uuid'];
roleId: PrivateSharingRole['id'];
encryptionKey: string;
};

export type PrivateSharingRole = { id: string; role: string; createdAt: Date; updatedAt: Date };

export type PrivateSharingRolesResponse = { roles: PrivateSharingRole[] };

export type GrantSharePrivilegesToUserResponse = { message: string };
export type UpdateUserRoleResponse = { message: string };

export type UpdateUserRolePayload = {
folderUUID: string;
roleId: string;
userUUID: string;
};

export type SharedFolderUser = {
avatar: string | null;
email: string;
grantedFrom: string;
grantedFromPlainName: string;
id: number;
lastname: string;
name: string;
roleId: string;
roleName: string;
uuid: string;
};

export type getSharedFolderUsersResponse = { users: SharedFolderUser[] };

export type PrivateSharedFolder = {
id: string;
folderId: string;
ownerId: string;
sharedWith: string;
encryptionKey: string;
createdAt: string;
updatedAt: string;
folder: {
id: number;
uuid: string;
parentId: number;
parentUuid: string | null;
name: string;
bucket: string | null;
userId: number;
encryptVersion: string;
plainName: string | null;
deleted: boolean;
removed: boolean;
deletedAt: string | null;
createdAt: string;
updatedAt: string;
removedAt: string | null;
};
owner: {
uuid: string;
email: string;
name: string;
lastname: string;
avatar: string | null;
};
fileSize: number;
};
1 change: 1 addition & 0 deletions src/drive/storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface FolderChild {
updatedAt: string;
userId: number;
user_id: number;
uuid: string;
}

export interface FetchFolderContentResponse {
Expand Down
15 changes: 14 additions & 1 deletion src/drive/users/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ApiSecurity, ApiUrl, AppDetails } from '../../shared';
import { headersWithToken } from '../../shared/headers';
import { ChangePasswordPayload, FriendInvite, InitializeUserResponse, UpdateProfilePayload } from './types';
import {
ChangePasswordPayload,
FriendInvite,
InitializeUserResponse,
UpdateProfilePayload,
UserPublicKeyResponse,
} from './types';
import { UserSettings } from '../../shared/types/userSettings';
import { HttpClient } from '../../shared/http/client';

Expand Down Expand Up @@ -131,6 +137,13 @@ export class Users {
return this.client.post<void>('/user/verifyEmail', payload, this.headers());
}

/**
* Get public key of given email
*/
public getPublicKeyByEmail({ email }: { email: string }): Promise<UserPublicKeyResponse> {
return this.client.get<UserPublicKeyResponse>(`/users/public-key/${email}`, this.headers());
}

private headers() {
return headersWithToken(this.appDetails.clientName, this.appDetails.clientVersion, this.apiSecurity.token);
}
Expand Down
2 changes: 2 additions & 0 deletions src/drive/users/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export interface ChangePasswordPayload {
export type UpdateProfilePayload = Partial<Pick<UserSettings, 'name' | 'lastname'>>;

export type FriendInvite = { guestEmail: string; host: number; accepted: boolean; id: number };

export type UserPublicKeyResponse = { publicKey: string };
1 change: 1 addition & 0 deletions test/drive/storage/mothers/folderContentResponse.mother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function randomFolderContentResponse(folderCount: number, fileCount: numb
updatedAt: '',
userId: 0,
user_id: 0,
uuid: '',
};
const file: DriveFileData = {
bucket: '',
Expand Down

0 comments on commit b54e9f5

Please sign in to comment.