Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Lark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
DocumentAIModel,
DocumentModel,
DriveFileModel,
PublicFileType,
PublishedFile,
TableFormView,
UserIdType,
WikiNode,
Expand Down Expand Up @@ -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<BiTableSchema> {
const { client } = this;

Expand Down
38 changes: 37 additions & 1 deletion src/module/Drive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -106,6 +115,33 @@ export abstract class DriveFileModel extends BaseListModel<DriveFile> {
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<LarkData<{ permission_public: PublicPermission }>>(
`${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<LarkData<{ password: string }>>(
`${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}
*/
Expand Down
32 changes: 32 additions & 0 deletions src/module/Drive/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DriveFileType, 'folder'>;

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<Omit<PublicPermission, 'lock_switch'>>;

export interface PublishedFile {
permission: PublicPermission;
password?: string;
}

export interface TransferOwner {
member_type: 'email' | 'userid' | 'openid';
Expand Down
Loading