versioncollection.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Copyright (c) 2015
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. * @author Robin Appelman <robin@icewind.nl>
  6. * @author Vincent Petry <vincent@nextcloud.com>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. (function() {
  25. /**
  26. * @memberof OCA.Versions
  27. */
  28. const VersionCollection = OC.Backbone.Collection.extend({
  29. model: OCA.Versions.VersionModel,
  30. sync: OC.Backbone.davSync,
  31. /**
  32. * @member OCA.Files.FileInfoModel
  33. */
  34. _fileInfo: null,
  35. _currentUser: null,
  36. _client: null,
  37. setFileInfo(fileInfo) {
  38. this._fileInfo = fileInfo
  39. },
  40. getFileInfo() {
  41. return this._fileInfo
  42. },
  43. setCurrentUser(user) {
  44. this._currentUser = user
  45. },
  46. getCurrentUser() {
  47. return this._currentUser || OC.getCurrentUser().uid
  48. },
  49. setClient(client) {
  50. this._client = client
  51. },
  52. getClient() {
  53. return this._client || new OC.Files.Client({
  54. host: OC.getHost(),
  55. root: OC.linkToRemoteBase('dav') + '/versions/' + this.getCurrentUser(),
  56. useHTTPS: OC.getProtocol() === 'https',
  57. })
  58. },
  59. url() {
  60. return OC.linkToRemoteBase('dav') + '/versions/' + this.getCurrentUser() + '/versions/' + this._fileInfo.get('id')
  61. },
  62. parse(result) {
  63. const fullPath = this._fileInfo.getFullPath()
  64. const fileId = this._fileInfo.get('id')
  65. const name = this._fileInfo.get('name')
  66. const user = this.getCurrentUser()
  67. const client = this.getClient()
  68. return _.map(result, function(version) {
  69. version.fullPath = fullPath
  70. version.fileId = fileId
  71. version.name = name
  72. version.timestamp = parseInt(moment(new Date(version.timestamp)).format('X'), 10)
  73. version.id = OC.basename(version.href)
  74. version.size = parseInt(version.size, 10)
  75. version.user = user
  76. version.client = client
  77. return version
  78. })
  79. },
  80. })
  81. OCA.Versions = OCA.Versions || {}
  82. OCA.Versions.VersionCollection = VersionCollection
  83. })()