import { Identity, SpaceUser } from '@spacehq/users'; import { Buckets, Client, UserAuth } from '@textile/hub'; import { UserMetadataStore } from './metadata/metadataStore'; import { AcceptInvitationResponse, AddItemsRequest, AddItemsResponse, CreateFolderRequest, GetFilesSharedByMeResponse, GetFilesSharedWithMeResponse, GetNotificationsResponse, GetRecentlySharedWithResponse, ListDirectoryRequest, ListDirectoryResponse, MakeFilePublicRequest, MovePathsResponse, NotificationSubscribeResponse, OpenFileRequest, OpenFileResponse, OpenUuidFileRequest, OpenUuidFileResponse, ShareViaPublicKeyRequest, ShareViaPublicKeyResponse, TxlSubscribeResponse } from './types'; export interface UserStorageConfig { textileHubAddress?: string; /** * Optional initializer of a bucket from textiles users auth * can be use to override/provide custom initialization logic * * The default value will be Textiles Buckets.withUserAuth * @param auth - Textile UserAuth object to initialize bucket */ bucketsInit?: (auth: UserAuth) => Buckets; threadsInit?: (auth: UserAuth) => Client; metadataStoreInit?: (identity: Identity) => Promise; /** * If set to true, would enable logging and some other debugging features. * Should only be set to true in development * */ debugMode?: boolean; } /** * UserStorage performs storage actions on behalf of the user provided. * * @example * ```typescript * const spaceStorage = new UserStorage(spaceUser); * * // create an empty folder * await spaceStorage.createFolder({ * bucket: 'personal', * path: '/cool' * }); * ``` */ export declare class UserStorage { private readonly user; private readonly config; private userMetadataStore?; private listener?; private mailbox?; private logger; constructor(user: SpaceUser, config?: UserStorageConfig); /** * Creates the listener post constructor * * @remarks * - This should be called after the constructor if txlSubscribe will be users */ initListener(): Promise; /** * Setup mailbox * * @remarks * - This should be called after the constructor if sharing functionalities are to be used */ initMailbox(): Promise; private initMailboxForUser; /** * Sync the notifications from the temp key with the current space user of this storage. * * This is useful for new users who have some pending invitation assigned to the notification. * * @param key - temp key gotten from ShareViaPublicKey */ syncFromTempKey(key: string): Promise; private authenticateTempUser; private syncTempUsersMessageWithUser; /** * Creates an empty folder at the requested path and bucket. * * @remarks * - It throws if an error occurred while creating the folder */ createFolder(request: CreateFolderRequest): Promise; private static addMembersToPathItems; private static parsePathItems; /** * txlSubscribe is used to listen for Textile events. * * It listens to all buckets for the user and produces an event when something changes in the bucket. * * TODO: try to make the event more granular so we can pick up specific files/folders * * @example * ```typescript * const spaceStorage = new UserStorage(spaceUser); * await spaceStorage.initListener(); * * const response = await spaceStorage.txlSubscribe(); * * response.on('data', (data: TxlSubscriveEvent) => { * const { bucketName } = data as AddItemsStatus; * // bucketName would be the name of the bucket * }); * ``` */ txlSubscribe(): Promise; /** * NotificationSubscribe is used to listen for Mailbox events. * * It listens to the users mailbox events, enriches the message with Space specific data. * * * @example * ```typescript * const spaceStorage = new UserStorage(spaceUser); * await spaceStorage.initListener(); * * const response = await spaceStorage.notificationSubscribe(); * * response.on('data', (data: NotificationSubscribeEvent) => { * const { notification: { relatedObject } } = data as Notification; * console.log('file invitation: ', relatedObject); * }); * ``` */ notificationSubscribe(): Promise; /** * Returns all bucket entries at the specified path. * * @example * ```typescript * const spaceStorage = new UserStorage(spaceUser); * const response = await spaceStorage.listDirectory({ bucket: 'personal', path: ''}); * * console.log(response.items); // print items in repository * ``` */ listDirectory(request: ListDirectoryRequest): Promise; /** * openFile returns a stream (AsyncIterableIterator) of the file at the path in the bucket. * * @example * ```typescript * const spaceStorage = new UserStorage(spaceUser); * * const response = await spaceStorage.openFile({ bucket: 'personal', path: '/file.txt' }); * // response.stream is an async iterable * for await (const chunk of response.stream) { * // aggregate the chunks based on your logic * } * * // response also contains a convenience function consumeStream * const fileBytes = await response.consumeStream(); * ``` */ openFile(request: OpenFileRequest): Promise; /** * Open a file with uuid. Will only return a result if the current user * has access to the file. * * See the sharing guide for a use case of how this method will be useful. * * @example * ```typescript * const spaceStorage = new UserStorage(spaceUser); * * const response = await spaceStorage.openFileByUuid({ * uuid: 'file-uu-id', * }); * const filename = response.entry.name; * * // response.stream is an async iterable * for await (const chunk of response.stream) { * // aggregate the chunks based on your logic * } * * // response also contains a convenience function consumeStream * const fileBytes = await response.consumeStream(); *``` * */ openFileByUuid(request: OpenUuidFileRequest): Promise; /** * Allow or revoke public access to a file. * * @example * ```typescript * const spaceStorage = new UserStorage(spaceUser); * * await spaceStorage.setFilePublicAccess({ * bucket: 'personal', * path: '/file.txt', * allowAccess: true, // <- set to false to revoke public access * }); * ``` */ setFilePublicAccess(request: MakeFilePublicRequest): Promise; /** * addItems is used to upload files to buckets. * * It uses an ReadableStream of Uint8Array data to read each files content to be uploaded. * * Uploads will sequential and asynchronous with updates being delivered through the * event emitter returned by the function. * * @example * ```typescript * const spaceStorage = new UserStorage(spaceUser); * * const response = await spaceStorage.addItems({ * bucket: 'personal', * files: [ * { * path: 'file.txt', * content: '', * mimeType: 'plain/text', * }, * { * path: 'space.png', * content: '', * mimeType: 'image/png', * } * ], * }); * * response.on('data', (data: AddItemsEventData) => { * const status = data as AddItemsStatus; * // update event on how each file is uploaded * }); * * response.on('error', (err: AddItemsEventData) => { * const status = data as AddItemsStatus; * // error event if a file upload fails * // status.error contains the error * }); * * response.once('done', (data: AddItemsEventData) => { * const summary = data as AddItemsResultSummary; * // returns a summary of all files and their upload status * }); * ``` */ addItems(request: AddItemsRequest): Promise; private uploadMultipleFiles; handleFileInvitation(invitationId: string, accept: boolean): Promise; private upsertInvitation; /** * Return the list of files the current storage user has shared with other users in the past * * @param offset - optional offset value for pagination. Can be gotten from the nextOffset field of a response * */ getFilesSharedByMe(offset?: string): Promise; /** * Returns a list of public keys of clients to which files where shared with * * @param offset - optional offset value for pagination. Can be gotten from the nextOffset field of a response */ getRecentlySharedWith(offset?: string): Promise; private static parseMsg; /** * Returns notifications for the user. If the notification is detected to be a supported * type, it is enhanced with additional information stored in the relatedObject field. * * @param seek - optional offset value for pagination. Can be gotten from the nextOffset field of a response * @param limit - optional limit value to return a specificed amount of results * */ getNotifications(seek?: string, limit?: number): Promise; private getExistingSharedFilesStatuses; /** * Moves files in a given bucket from source to destination. Multiple moves can be * requested by matching up the indices of the sourcePath and destPath arrays * * @param bucketName - name of bucket * @param sourcePaths - array of strings corresponding to the source paths * @param destPaths - array of strings corresponding to the target paths * */ movePaths(bucketName: string, sourcePaths: string[], destPaths: string[]): Promise; private moveMultiplePaths; /** * Return the list of shared files accepted by user * * @param offset - optional offset value for pagination. Can be gotten from the nextOffset field of a response * */ getFilesSharedWithMe(offset?: string): Promise; private buildSharedFileFromMetadata; private getFileMetadataMap; private getOrCreateBucket; /** * setNotificationsLastSeenAt sets the field so that . * * @example * ```typescript * const result = await spaceStorage.setNotificationsLastSeenAt(Date.now()); * ``` */ setNotificationsLastSeenAt(timestamp: number): Promise; /** * shareViaPublicKey shares specified files to users who owns the specified public keys. * * @example * ```typescript * const result = await spaceStorage.shareViaPublicKey({ * publicKeys: [{ * id: 'user@email.com', // or any identifier for the user * pk: 'user-pk-hex-or-multibase', // optional, omit if user doesn't exist yet, it would generate temp access key * }], * paths: [{ * bucket: 'personal', * path: '/file/path/here' * }], * }); * * ``` */ shareViaPublicKey(request: ShareViaPublicKeyRequest): Promise; private addPathToRecentlyShared; private static addUsersToRecentlyShared; private normalizeShareKeys; private normalizeFullPaths; private getUserBucketsClient; private getUserThreadsClient; private getUserAuth; private initBucket; private initThreads; private getMetadataStore; private getDefaultUserMetadataStore; }