diff --git a/src/Lark.ts b/src/Lark.ts index 4014fdb..635479a 100644 --- a/src/Lark.ts +++ b/src/Lark.ts @@ -9,6 +9,8 @@ import { DocumentAIModel, DocumentModel, DriveFileModel, + PublishedFile, + PublicFileType, TableFormView, UserIdType, WikiNode, @@ -291,6 +293,30 @@ export class LarkApp implements LarkAppOption { return this.documentStore.getOneContent(doc_token, 'markdown'); } + async publishFile( + URI: string, + enablePassword = false, + editable = false + ): Promise { + await this.getAccessToken(); + + const [[pathType, token]] = DriveFileModel.parseURI(URI), + type: PublicFileType = + pathType === 'wiki' + ? pathType + : getLarkDocumentType(pathType as LarkDocumentPathType); + + const permissionPublic = await this.driveFileStore.updatePublicPermission(type, token, { + external_access_entity: 'open', + link_share_entity: editable ? 'anyone_editable' : 'anyone_readable' + }), + password = enablePassword + ? await this.driveFileStore.createPublicPassword(type, token) + : undefined; + + return { permissionPublic, password }; + } + async getBiTableSchema(appId: string): Promise { const { client } = this; diff --git a/src/module/Drive/index.ts b/src/module/Drive/index.ts index 7e6d2cb..3d10b08 100644 --- a/src/module/Drive/index.ts +++ b/src/module/Drive/index.ts @@ -4,10 +4,22 @@ import { buildURLData, splitArray } from 'web-utility'; import { LarkData, LarkDocumentPathTypeMap, LarkDocumentType, UploadTargetType } from '../../type'; import { UserIdType } from '../User/type'; -import { CopiedFile, DriveFile, DriveFileType, TransferOwner, TransferOption } from './type'; +import { + CopiedFile, + DriveFile, + DriveFileType, + PermissionPublic, + PublicFileType, + PublicPermissionPatch, + TransferOption, + TransferOwner +} from './type'; export * from './type'; +const MemberPermissionBaseURI = 'drive/v1/permissions'; +const PublicPermissionBaseURI = 'drive/v2/permissions'; + export abstract class DriveFileModel extends BaseListModel { baseURI = 'drive/v1'; abstract client: RESTClient; @@ -106,6 +118,35 @@ export abstract class DriveFileModel extends BaseListModel { return body!.data!.file; } + /** + * @see {@link https://open.feishu.cn/document/server-docs/docs/permission/permission-public/patch-2} + */ + @toggle('uploading') + async updatePublicPermission( + type: PublicFileType, + token: string, + permission: PublicPermissionPatch + ) { + const { body } = await this.client.patch>( + `${PublicPermissionBaseURI}/${token}/public?${buildURLData({ type })}`, + permission + ); + + return body!.data!.permission_public; + } + + /** + * @see {@link https://open.feishu.cn/document/server-docs/docs/permission/permission-public/permission-public-password/create} + */ + @toggle('uploading') + async createPublicPassword(type: PublicFileType, token: string) { + const { body } = await this.client.post>( + `${MemberPermissionBaseURI}/${token}/public/password?${buildURLData({ type })}` + ); + + return body!.data!.password; + } + /** * @see {@link https://open.feishu.cn/document/server-docs/docs/permission/permission-member/transfer_owner} */ @@ -117,7 +158,7 @@ export abstract class DriveFileModel extends BaseListModel { option = {} as TransferOption ) { await this.client.post( - `${this.baseURI}/permissions/${token}/members/transfer_owner?${buildURLData({ ...option, type })}`, + `${MemberPermissionBaseURI}/${token}/members/transfer_owner?${buildURLData({ ...option, type })}`, newOwner ); } diff --git a/src/module/Drive/type.ts b/src/module/Drive/type.ts index 5014076..131e4ff 100644 --- a/src/module/Drive/type.ts +++ b/src/module/Drive/type.ts @@ -14,6 +14,38 @@ export type DriveFile = Record< export type CopiedFile = Record<'type' | `${'parent_' | ''}token` | 'name' | 'url', string>; export type DriveFileType = LarkDocumentType | 'minutes' | 'folder' | 'wiki'; +export type PublicFileType = Exclude; + +type PublicEditableLevel = 'view' | 'edit'; +type PublicLinkAccessLevel = 'readable' | 'editable'; +type PublicLinkShareScope = 'tenant' | 'partner_tenant' | 'anyone'; + +export type PublicPermissionLevel = `anyone_can_${PublicEditableLevel}` | 'only_full_access'; +export type PublicCommentEntity = `anyone_can_${PublicEditableLevel}`; +export type PublicShareEntity = 'anyone' | 'same_tenant'; +export type PublicCollaboratorEntity = + | `collaborator_can_${PublicEditableLevel}` + | 'collaborator_full_access'; +export type PublicExternalAccessEntity = 'open' | 'closed' | 'allow_share_partner_tenant'; +export type PublicLinkShareEntity = `${PublicLinkShareScope}_${PublicLinkAccessLevel}` | 'closed'; + +export interface PermissionPublic { + external_access_entity: PublicExternalAccessEntity; + security_entity: PublicPermissionLevel; + comment_entity: PublicCommentEntity; + share_entity: PublicShareEntity; + manage_collaborator_entity: PublicCollaboratorEntity; + link_share_entity: PublicLinkShareEntity; + copy_entity: PublicPermissionLevel; + lock_switch: boolean; +} + +export type PublicPermissionPatch = Partial>; + +export interface PublishedFile { + permissionPublic: Partial; + password?: string; +} export interface TransferOwner { member_type: 'email' | 'userid' | 'openid';