You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commentcollection.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* eslint-disable */
  2. /*
  3. * Copyright (c) 2016
  4. *
  5. * This file is licensed under the Affero General Public License version 3
  6. * or later.
  7. *
  8. * See the COPYING-README file.
  9. *
  10. */
  11. (function(OC, OCA) {
  12. /**
  13. * @class OCA.Comments.CommentCollection
  14. * @classdesc
  15. *
  16. * Collection of comments assigned to a file
  17. *
  18. */
  19. var CommentCollection = OC.Backbone.Collection.extend(
  20. /** @lends OCA.Comments.CommentCollection.prototype */ {
  21. sync: OC.Backbone.davSync,
  22. model: OCA.Comments.CommentModel,
  23. /**
  24. * Object type
  25. *
  26. * @type string
  27. */
  28. _objectType: 'files',
  29. /**
  30. * Object id
  31. *
  32. * @type string
  33. */
  34. _objectId: null,
  35. /**
  36. * True if there are no more page results left to fetch
  37. *
  38. * @type bool
  39. */
  40. _endReached: false,
  41. /**
  42. * Number of comments to fetch per page
  43. *
  44. * @type int
  45. */
  46. _limit: 20,
  47. /**
  48. * Initializes the collection
  49. *
  50. * @param {string} [options.objectType] object type
  51. * @param {string} [options.objectId] object id
  52. */
  53. initialize: function(models, options) {
  54. options = options || {}
  55. if (options.objectType) {
  56. this._objectType = options.objectType
  57. }
  58. if (options.objectId) {
  59. this._objectId = options.objectId
  60. }
  61. },
  62. url: function() {
  63. return OC.linkToRemote('dav') + '/comments/'
  64. + encodeURIComponent(this._objectType) + '/'
  65. + encodeURIComponent(this._objectId) + '/'
  66. },
  67. setObjectId: function(objectId) {
  68. this._objectId = objectId
  69. },
  70. hasMoreResults: function() {
  71. return !this._endReached
  72. },
  73. reset: function() {
  74. this._endReached = false
  75. this._summaryModel = null
  76. return OC.Backbone.Collection.prototype.reset.apply(this, arguments)
  77. },
  78. /**
  79. * Fetch the next set of results
  80. */
  81. fetchNext: function(options) {
  82. var self = this
  83. if (!this.hasMoreResults()) {
  84. return null
  85. }
  86. var body = '<?xml version="1.0" encoding="utf-8" ?>\n'
  87. + '<oc:filter-comments xmlns:D="DAV:" xmlns:oc="http://owncloud.org/ns">\n'
  88. // load one more so we know there is more
  89. + ' <oc:limit>' + (this._limit + 1) + '</oc:limit>\n'
  90. + ' <oc:offset>' + this.length + '</oc:offset>\n'
  91. + '</oc:filter-comments>\n'
  92. options = options || {}
  93. var success = options.success
  94. options = _.extend({
  95. remove: false,
  96. parse: true,
  97. data: body,
  98. davProperties: CommentCollection.prototype.model.prototype.davProperties,
  99. success: function(resp) {
  100. if (resp.length <= self._limit) {
  101. // no new entries, end reached
  102. self._endReached = true
  103. } else {
  104. // remove last entry, for next page load
  105. resp = _.initial(resp)
  106. }
  107. if (!self.set(resp, options)) {
  108. return false
  109. }
  110. if (success) {
  111. success.apply(null, arguments)
  112. }
  113. self.trigger('sync', 'REPORT', self, options)
  114. }
  115. }, options)
  116. return this.sync('REPORT', this, options)
  117. },
  118. /**
  119. * Returns the matching summary model
  120. *
  121. * @returns {OCA.Comments.CommentSummaryModel} summary model
  122. */
  123. getSummaryModel: function() {
  124. if (!this._summaryModel) {
  125. this._summaryModel = new OCA.Comments.CommentSummaryModel({
  126. id: this._objectId,
  127. objectType: this._objectType
  128. })
  129. }
  130. return this._summaryModel
  131. },
  132. /**
  133. * Updates the read marker for this comment thread
  134. *
  135. * @param {Date} [date] optional date, defaults to now
  136. * @param {Object} [options] backbone options
  137. */
  138. updateReadMarker: function(date, options) {
  139. options = options || {}
  140. return this.getSummaryModel().save({
  141. readMarker: (date || new Date()).toUTCString()
  142. }, options)
  143. }
  144. })
  145. OCA.Comments.CommentCollection = CommentCollection
  146. })(OC, OCA)