diff --git a/package.json b/package.json index 7f65c507..6b5b5aba 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "prettier": "^2.0.5", "ts-loader": "^8.0.0", "ts-node": "^9.0.0", - "typedoc": "^0.19.2", + "typedoc": "^0.19.2", "typescript": "^4.0.3" }, "bugs": { diff --git a/src/share.ts b/src/share.ts index 7c41b396..ab778c9b 100644 --- a/src/share.ts +++ b/src/share.ts @@ -2,6 +2,8 @@ import Client, { ClientError } from "./client"; import File from "./file"; import FileSystemElement from "./fileSystemElement"; import Folder from "./folder"; +import UserGroup from "./userGroup"; +import User from "./user"; export enum SharePermission { all = 31, @@ -12,7 +14,7 @@ export enum SharePermission { share = 16, } -enum ShareType { +export enum ShareType { user = 0, group = 1, publicLink = 3, @@ -21,9 +23,10 @@ enum ShareType { export interface ICreateShare { "fileSystemElement": FileSystemElement; - // @todo "shareWith"?: User | UserGroup | EMail; "publicUpload"?: boolean; "password"?: string; + "shareType"?: ShareType; + "shareWith"?: User | UserGroup; } export enum ShareItemType { @@ -39,11 +42,18 @@ export default class Share { } public static createShareRequestBody(createShare: ICreateShare): string { - const shareType: ShareType = ShareType.publicLink; + // for backwards compatibility, default sharetype is public link + const shareType: ShareType = createShare.shareType == null ? ShareType.publicLink : createShare.shareType; + if (shareType === ShareType.user || shareType === ShareType.group) { + if (createShare.shareWith == null) { + throw new ClientError("Error, when using ShareType user or usergroup, you need to specify shareWith", "ERR_SHARETYPE_USER_GROUP_WITHOUT_SHAREWITH"); + } + } const shareRequest: { path: string, shareType: number, + shareWith?: string, // @todo permissions: number | number[] password?: string, } = { @@ -52,6 +62,10 @@ export default class Share { shareType, }; + if (createShare.shareWith) { + shareRequest.shareWith = createShare.shareWith.id; + } + if (createShare.password) { shareRequest.password = createShare.password; } @@ -65,10 +79,10 @@ export default class Share { id: string; itemType: ShareItemType, note: string, - token: string, - url: string, + token: string | null, + url: string | null, publicUpload: boolean, - // share_type: number, + shareType: number, // "uid_owner": string, // "displayname_owner": string, // "permissions": SharePermission, @@ -81,7 +95,7 @@ export default class Share { // "displayname_file_owner": string, // "path": string, // "mimetype"?: string, - // "share_with"?: string, + "shareWith": User | UserGroup | null, // "share_with_displayname"?: string, // "mail_send": boolean, // "hide_download": boolean, @@ -94,9 +108,10 @@ export default class Share { id, itemType: ShareItemType.file, note: "", - token: "", - url: "", + token: null, + url: null, publicUpload: false, + shareType: ShareType.publicLink, }; } @@ -111,6 +126,7 @@ export default class Share { /** * set a new password + * * @param password */ public async setPassword(password: string): Promise { @@ -136,16 +152,6 @@ export default class Share { throw new ClientError(`Error invalid share data received "ocs.data" missing`, "ERR_INVALID_SHARE_RESPONSE"); } - if (!rawShareData.ocs.data[0].url) { - throw new ClientError(`Error invalid share data received "url" missing`, "ERR_INVALID_SHARE_RESPONSE"); - } - this.memento.url = rawShareData.ocs.data[0].url; - - if (!rawShareData.ocs.data[0].token) { - throw new ClientError(`Error invalid share data received "token" missing`, "ERR_INVALID_SHARE_RESPONSE"); - } - this.memento.token = rawShareData.ocs.data[0].token; - if (!rawShareData.ocs.data[0].item_type) { throw new ClientError(`Error invalid share data received "item_type" missing`, "ERR_INVALID_SHARE_RESPONSE"); } @@ -163,6 +169,42 @@ export default class Share { this.memento.note = rawShareData.ocs.data[0].note; } + if (rawShareData.ocs.data[0].share_type) { + switch (rawShareData.ocs.data[0].share_type) { + case ShareType.user.valueOf(): + this.memento.shareType = ShareType.user; + if (rawShareData.ocs.data[0].share_with) { + this.memento.shareWith = new User(this.client, rawShareData.ocs.data[0].share_with) + } else { + throw new ClientError(`Error user share without target user`, "ERR_USER_SHARE_NO_USER"); + } + break; + case ShareType.group.valueOf(): + this.memento.shareType = ShareType.group; + if (rawShareData.ocs.data[0].share_with) { + this.memento.shareWith = new UserGroup(this.client, rawShareData.ocs.data[0].share_with) + } else { + throw new ClientError(`Error usergroup share without target usergroup`, "ERR_USERGROUP_SHARE_NO_USERGROUP"); + } + break; + case ShareType.publicLink.valueOf(): + this.memento.shareType = ShareType.publicLink; + if (!rawShareData.ocs.data[0].url) { + throw new ClientError(`Error invalid share data received "url" missing`, "ERR_INVALID_SHARE_RESPONSE"); + } + this.memento.url = rawShareData.ocs.data[0].url; + if (!rawShareData.ocs.data[0].token) { + throw new ClientError(`Error invalid share data received "token" missing`, "ERR_INVALID_SHARE_RESPONSE"); + } + this.memento.token = rawShareData.ocs.data[0].token; + + break; + default: + throw new ClientError(`Error unsupported share type`, "ERR_UNSUPPORTED_SHARE_TYPE"); + } + + } + // console.log(JSON.stringify(rawShareData, null, 4)); // console.log(JSON.stringify(this, null, 4)); } @@ -171,7 +213,7 @@ export default class Share { * token * The token is readonly */ - public get token(): string { + public get token(): string | null { return this.memento.token; } @@ -179,7 +221,7 @@ export default class Share { * share url * The share url is readonly */ - public get url(): string { + public get url(): string | null { return this.memento.url; } @@ -222,4 +264,19 @@ export default class Share { return this.memento.itemType; } + /** + * share type + * The type of the share + */ + public get shareType(): ShareType { + return this.memento.shareType; + } + + /** + * share with + * The entity the share is shared with + */ + public get shareWith(): User | UserGroup | null { + return this.memento.shareWith; + } }