Skip to content
Open
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
26 changes: 26 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,
PublishedFile,
PublicFileType,
TableFormView,
UserIdType,
WikiNode,
Expand Down Expand Up @@ -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<PublishedFile> {
Comment on lines +296 to +300
await this.getAccessToken();

const [[pathType, token]] = DriveFileModel.parseURI(URI),
type: PublicFileType =
pathType === 'wiki'
? pathType
: getLarkDocumentType(pathType as LarkDocumentPathType);

Comment on lines +303 to +308
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<BiTableSchema> {
const { client } = this;

Expand Down
45 changes: 43 additions & 2 deletions src/module/Drive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DriveFile> {
baseURI = 'drive/v1';
abstract client: RESTClient;
Expand Down Expand Up @@ -106,6 +118,35 @@ 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: PermissionPublic }>>(
`${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<LarkData<{ password: string }>>(
`${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}
*/
Expand All @@ -117,7 +158,7 @@ export abstract class DriveFileModel extends BaseListModel<DriveFile> {
option = {} as TransferOption
) {
await this.client.post<LarkData>(
`${this.baseURI}/permissions/${token}/members/transfer_owner?${buildURLData({ ...option, type })}`,
`${MemberPermissionBaseURI}/${token}/members/transfer_owner?${buildURLData({ ...option, type })}`,
newOwner
);
}
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 = '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<Omit<PermissionPublic, 'lock_switch'>>;

export interface PublishedFile {
permissionPublic: Partial<PermissionPublic>;
password?: string;
}
Comment on lines +45 to +48

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