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.6KB

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