diff --git a/src/Lark.ts b/src/Lark.ts index 4014fdb..d36b47a 100644 --- a/src/Lark.ts +++ b/src/Lark.ts @@ -9,6 +9,8 @@ import { DocumentAIModel, DocumentModel, DriveFileModel, + PublicFileType, + PublishedFile, TableFormView, UserIdType, WikiNode, @@ -291,6 +293,31 @@ export class LarkApp implements LarkAppOption { return this.documentStore.getOneContent(doc_token, 'markdown'); } + /** + * @see {@link DriveFileModel#updatePublicPermission} + * @see {@link DriveFileModel#createPublicPassword} + */ + async publishFile(URI: string, enablePassword = false, editable = false) { + await this.getAccessToken(); + + const [[pathType, token]] = DriveFileModel.parseURI(URI); + const type: PublicFileType = + pathType === 'wiki' ? pathType : getLarkDocumentType(pathType as LarkDocumentPathType); + + const permission = await this.driveFileStore.updatePublicPermission(type, token, { + external_access_entity: 'open', + link_share_entity: editable ? 'anyone_editable' : 'anyone_readable', + copy_entity: 'anyone_can_view', + security_entity: 'anyone_can_view', + comment_entity: 'anyone_can_view' + }), + password = enablePassword + ? await this.driveFileStore.createPublicPassword(type, token) + : undefined; + + return { permission, password } as PublishedFile; + } + async getBiTableSchema(appId: string): Promise { const { client } = this; diff --git a/src/module/Drive/index.ts b/src/module/Drive/index.ts index 7e6d2cb..ca949ac 100644 --- a/src/module/Drive/index.ts +++ b/src/module/Drive/index.ts @@ -4,7 +4,16 @@ 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, + PublicFileType, + PublicPermission, + PublicPermissionPatch, + TransferOption, + TransferOwner +} from './type'; export * from './type'; @@ -106,6 +115,33 @@ 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>( + `${this.baseURI}/../v2/permissions/${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>( + `${this.baseURI}/permissions/${token}/public/password?${buildURLData({ type })}` + ); + return body!.data!.password; + } + /** * @see {@link https://open.feishu.cn/document/server-docs/docs/permission/permission-member/transfer_owner} */ diff --git a/src/module/Drive/type.ts b/src/module/Drive/type.ts index 5014076..d979ea4 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 = `${'' | 'partner_'}tenant` | 'anyone'; + +export type PublicCommentEntity = `anyone_can_${PublicEditableLevel}`; +export type PublicPermissionLevel = PublicCommentEntity | 'only_full_access'; +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 PublicPermission { + 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 { + permission: PublicPermission; + password?: string; +} export interface TransferOwner { member_type: 'email' | 'userid' | 'openid';