From e8f4b82449c904d9795e027c72277072e3324413 Mon Sep 17 00:00:00 2001 From: Ole Langbehn Date: Thu, 9 Sep 2021 15:55:00 +0200 Subject: [PATCH 1/2] Implementation for share types user and group --- package.json | 2 +- src/share.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 6 deletions(-) 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..d2b0fbca 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; } @@ -68,7 +82,7 @@ export default class Share { token: string, url: string, 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, // "share_with_displayname"?: string, // "mail_send": boolean, // "hide_download": boolean, @@ -97,6 +111,7 @@ export default class Share { token: "", url: "", 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 { @@ -163,6 +179,33 @@ 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; + 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)); } @@ -222,4 +265,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 | undefined { + return this.memento.shareWith; + } } From 6b7c6dd9b53a7e2b0d2d79daf0096d84192a8f3a Mon Sep 17 00:00:00 2001 From: Ole Langbehn Date: Thu, 9 Sep 2021 18:09:44 +0200 Subject: [PATCH 2/2] update after testing/debugging * url and token are only set when share type is publicLink * replaced optional memento members with |null ts types --- src/share.ts | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/share.ts b/src/share.ts index d2b0fbca..ab778c9b 100644 --- a/src/share.ts +++ b/src/share.ts @@ -79,8 +79,8 @@ export default class Share { id: string; itemType: ShareItemType, note: string, - token: string, - url: string, + token: string | null, + url: string | null, publicUpload: boolean, shareType: number, // "uid_owner": string, @@ -95,7 +95,7 @@ export default class Share { // "displayname_file_owner": string, // "path": string, // "mimetype"?: string, - "shareWith"?: User | UserGroup, + "shareWith": User | UserGroup | null, // "share_with_displayname"?: string, // "mail_send": boolean, // "hide_download": boolean, @@ -108,8 +108,8 @@ export default class Share { id, itemType: ShareItemType.file, note: "", - token: "", - url: "", + token: null, + url: null, publicUpload: false, shareType: ShareType.publicLink, }; @@ -152,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"); } @@ -199,6 +189,15 @@ export default class Share { 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"); @@ -214,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; } @@ -222,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; } @@ -277,7 +276,7 @@ export default class Share { * share with * The entity the share is shared with */ - public get shareWith(): User | UserGroup | undefined { + public get shareWith(): User | UserGroup | null { return this.memento.shareWith; } }