Share.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /**
  2. * @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. export default class Share {
  23. #share;
  24. /**
  25. * Create the share object
  26. *
  27. * @param {Object} ocsData ocs request response
  28. */
  29. constructor(ocsData) {
  30. if (ocsData.ocs && ocsData.ocs.data && ocsData.ocs.data[0]) {
  31. ocsData = ocsData.ocs.data[0]
  32. }
  33. // convert int into boolean
  34. ocsData.hide_download = !!ocsData.hide_download
  35. ocsData.mail_send = !!ocsData.mail_send
  36. // store state
  37. this.#share = ocsData
  38. }
  39. /**
  40. * Get the share state
  41. * ! used for reactivity purpose
  42. * Do not remove. It allow vuejs to
  43. * inject its watchers into the #share
  44. * state and make the whole class reactive
  45. *
  46. * @returns {Object} the share raw state
  47. * @readonly
  48. * @memberof Sidebar
  49. */
  50. get state() {
  51. return this.#share
  52. }
  53. /**
  54. * get the share id
  55. *
  56. * @returns {int}
  57. * @readonly
  58. * @memberof Share
  59. */
  60. get id() {
  61. return this.#share.id
  62. }
  63. /**
  64. * Get the share type
  65. *
  66. * @returns {int}
  67. * @readonly
  68. * @memberof Share
  69. */
  70. get type() {
  71. return this.#share.share_type
  72. }
  73. /**
  74. * Get the share permissions
  75. * See OC.PERMISSION_* variables
  76. *
  77. * @returns {int}
  78. * @readonly
  79. * @memberof Share
  80. */
  81. get permissions() {
  82. return this.#share.permissions
  83. }
  84. /**
  85. * Set the share permissions
  86. * See OC.PERMISSION_* variables
  87. *
  88. * @param {int} permissions valid permission, See OC.PERMISSION_* variables
  89. * @memberof Share
  90. */
  91. set permissions(permissions) {
  92. this.#share.permissions = permissions
  93. }
  94. // SHARE OWNER --------------------------------------------------
  95. /**
  96. * Get the share owner uid
  97. *
  98. * @returns {string}
  99. * @readonly
  100. * @memberof Share
  101. */
  102. get owner() {
  103. return this.#share.uid_owner
  104. }
  105. /**
  106. * Get the share owner's display name
  107. *
  108. * @returns {string}
  109. * @readonly
  110. * @memberof Share
  111. */
  112. get ownerDisplayName() {
  113. return this.#share.displayname_owner
  114. }
  115. // SHARED WITH --------------------------------------------------
  116. /**
  117. * Get the share with entity uid
  118. *
  119. * @returns {string}
  120. * @readonly
  121. * @memberof Share
  122. */
  123. get shareWith() {
  124. return this.#share.share_with
  125. }
  126. /**
  127. * Get the share with entity display name
  128. * fallback to its uid if none
  129. *
  130. * @returns {string}
  131. * @readonly
  132. * @memberof Share
  133. */
  134. get shareWithDisplayName() {
  135. return this.#share.share_with_displayname
  136. || this.#share.share_with
  137. }
  138. /**
  139. * Get the share with avatar if any
  140. *
  141. * @returns {string}
  142. * @readonly
  143. * @memberof Share
  144. */
  145. get shareWithAvatar() {
  146. return this.#share.share_with_avatar
  147. }
  148. // SHARED FILE OR FOLDER OWNER ----------------------------------
  149. /**
  150. * Get the shared item owner uid
  151. *
  152. * @returns {string}
  153. * @readonly
  154. * @memberof Share
  155. */
  156. get uidFileOwner() {
  157. return this.#share.uid_file_owner
  158. }
  159. /**
  160. * Get the shared item display name
  161. * fallback to its uid if none
  162. *
  163. * @returns {string}
  164. * @readonly
  165. * @memberof Share
  166. */
  167. get displaynameFileOwner() {
  168. return this.#share.displayname_file_owner
  169. || this.#share.uid_file_owner
  170. }
  171. // TIME DATA ----------------------------------------------------
  172. /**
  173. * Get the share creation timestamp
  174. *
  175. * @returns {int}
  176. * @readonly
  177. * @memberof Share
  178. */
  179. get createdTime() {
  180. return this.#share.stime
  181. }
  182. /**
  183. * Get the expiration date as a string format
  184. *
  185. * @returns {string}
  186. * @readonly
  187. * @memberof Share
  188. */
  189. get expireDate() {
  190. return this.#share.expiration
  191. }
  192. /**
  193. * Set the expiration date as a string format
  194. * e.g. YYYY-MM-DD
  195. *
  196. * @param {string} date the share expiration date
  197. * @memberof Share
  198. */
  199. set expireDate(date) {
  200. this.#share.expiration = date
  201. }
  202. // EXTRA DATA ---------------------------------------------------
  203. /**
  204. * Get the public share token
  205. *
  206. * @returns {string} the token
  207. * @readonly
  208. * @memberof Share
  209. */
  210. get token() {
  211. return this.#share.token
  212. }
  213. /**
  214. * Get the share note if any
  215. *
  216. * @returns {string}
  217. * @readonly
  218. * @memberof Share
  219. */
  220. get note() {
  221. return this.#share.note
  222. }
  223. /**
  224. * Set the share note if any
  225. *
  226. * @param {string} note the note
  227. * @memberof Share
  228. */
  229. set note(note) {
  230. this.#share.note = note.trim()
  231. }
  232. /**
  233. * Have a mail been sent
  234. *
  235. * @returns {boolean}
  236. * @readonly
  237. * @memberof Share
  238. */
  239. get mailSend() {
  240. return this.#share.mail_send === true
  241. }
  242. /**
  243. * Hide the download button on public page
  244. *
  245. * @returns {boolean}
  246. * @readonly
  247. * @memberof Share
  248. */
  249. get hideDownload() {
  250. return this.#share.hide_download === true
  251. }
  252. /**
  253. * Hide the download button on public page
  254. *
  255. * @param {boolean} state hide the button ?
  256. * @memberof Share
  257. */
  258. set hideDownload(state) {
  259. this.#share.hide_download = state === true
  260. }
  261. /**
  262. * Password protection of the share
  263. *
  264. * @returns {string}
  265. * @readonly
  266. * @memberof Share
  267. */
  268. get password() {
  269. return this.#share.password
  270. }
  271. /**
  272. * Password protection of the share
  273. *
  274. * @param {string} password the share password
  275. * @memberof Share
  276. */
  277. set password(password) {
  278. this.#share.password = password.trim()
  279. }
  280. // SHARED ITEM DATA ---------------------------------------------
  281. /**
  282. * Get the shared item absolute full path
  283. *
  284. * @returns {string}
  285. * @readonly
  286. * @memberof Share
  287. */
  288. get path() {
  289. return this.#share.path
  290. }
  291. /**
  292. * Return the item type: file or folder
  293. *
  294. * @returns {string} 'folder' or 'file'
  295. * @readonly
  296. * @memberof Share
  297. */
  298. get itemType() {
  299. return this.#share.item_type
  300. }
  301. /**
  302. * Get the shared item mimetype
  303. *
  304. * @returns {string}
  305. * @readonly
  306. * @memberof Share
  307. */
  308. get mimetype() {
  309. return this.#share.mimetype
  310. }
  311. /**
  312. * Get the shared item id
  313. *
  314. * @returns {int}
  315. * @readonly
  316. * @memberof Share
  317. */
  318. get fileSource() {
  319. return this.#share.file_source
  320. }
  321. /**
  322. * Get the target path on the receiving end
  323. * e.g the file /xxx/aaa will be shared in
  324. * the receiving root as /aaa, the fileTarget is /aaa
  325. *
  326. * @returns {string}
  327. * @readonly
  328. * @memberof Share
  329. */
  330. get fileTarget() {
  331. return this.#share.file_target
  332. }
  333. /**
  334. * Get the parent folder id if any
  335. *
  336. * @returns {int}
  337. * @readonly
  338. * @memberof Share
  339. */
  340. get fileParent() {
  341. return this.#share.file_parent
  342. }
  343. // PERMISSIONS Shortcuts
  344. /**
  345. * Does this share have CREATE permissions
  346. *
  347. * @returns {boolean}
  348. * @readonly
  349. * @memberof Share
  350. */
  351. get hasCreatePermission() {
  352. return !!((this.permissions & OC.PERMISSION_CREATE))
  353. }
  354. /**
  355. * Does this share have DELETE permissions
  356. *
  357. * @returns {boolean}
  358. * @readonly
  359. * @memberof Share
  360. */
  361. get hasDeletePermission() {
  362. return !!((this.permissions & OC.PERMISSION_DELETE))
  363. }
  364. /**
  365. * Does this share have UPDATE permissions
  366. *
  367. * @returns {boolean}
  368. * @readonly
  369. * @memberof Share
  370. */
  371. get hasUpdatePermission() {
  372. return !!((this.permissions & OC.PERMISSION_UPDATE))
  373. }
  374. /**
  375. * Does this share have SHARE permissions
  376. *
  377. * @returns {boolean}
  378. * @readonly
  379. * @memberof Share
  380. */
  381. get hasSharePermission() {
  382. return !!((this.permissions & OC.PERMISSION_SHARE))
  383. }
  384. // TODO: SORT THOSE PROPERTIES
  385. get label() {
  386. return this.#share.label
  387. }
  388. get parent() {
  389. return this.#share.parent
  390. }
  391. get storageId() {
  392. return this.#share.storage_id
  393. }
  394. get storage() {
  395. return this.#share.storage
  396. }
  397. get itemSource() {
  398. return this.#share.item_source
  399. }
  400. }