Debian Codegraph @

extensions/DiscussionTools/modules/ThreadItem.js

raw symbols SCIP
1 /* global moment */
2
3 const utils = require( './utils.js' );
4
5 /**
6 * A thread item, either a heading or a comment
7 *
8 * @class ThreadItem
9 * @constructor
10 * @param {string} type `heading` or `comment`
11 * @param {number} level Indentation level
12 * @param {Object} range Object describing the extent of the comment, including the
13 * signature and timestamp. It has the same properties as a Range object: `startContainer`,
14 * `startOffset`, `endContainer`, `endOffset` (we don't use a real Range because they change
15 * magically when the DOM structure changes).
16 */
17 function ThreadItem( type, level, range ) {
18 this.type = type;
19 this.level = level;
20 this.range = range;
21
22 /**
23 * @member {string} Name for this comment, intended to be used to
24 * find this comment in other revisions of the same page
25 */
26 this.name = null;
27 /**
28 * @member {string} Unique ID (within the page) for this comment
29 */
30 this.id = null;
31 /**
32 * @member {ThreadItem[]} Replies to this thread item
33 */
34 this.replies = [];
35
36 /**
37 * @member {string[]} Warnings
38 */
39 this.warnings = [];
40
41 this.rootNode = null;
42
43 this.authors = null;
44 this.commentCount = null;
45 this.oldestReply = null;
46 this.latestReply = null;
47 }
48
49 OO.initClass( ThreadItem );
50
51 /**
52 * Create a new ThreadItem from a JSON serialization
53 *
54 * @param {string|Object} json JSON serialization or hash object
55 * @param {HTMLElement} rootNode
56 * @return {ThreadItem}
57 * @throws {Error} Unknown ThreadItem type
58 */
59 ThreadItem.static.newFromJSON = function ( json, rootNode ) {
60 // The page can be served from the HTTP cache (Varnish), and the JSON may be generated
61 // by an older version of our PHP code. Code below must be able to handle that.
62 // See ThreadItem::jsonSerialize() in PHP.
63
64 const hash = typeof json === 'string' ? JSON.parse( json ) : json;
65
66 let item;
67 switch ( hash.type ) {
68 case 'comment': {
69 // Late require to avoid circular dependency
70 const CommentItem = require( './CommentItem.js' );
71 item = new CommentItem(
72 hash.level,
73 hash.range,
74 hash.signatureRanges,
75 hash.timestampRanges,
76 moment.utc( hash.timestamp, [
77 // See CommentItem#getTimestampString for notes about the two formats.
78 'YYYYMMDDHHmmss',
79 moment.ISO_8601
80 ], true ),
81 hash.author,
82 hash.displayName
83 );
84 break;
85 }
86 case 'heading': {
87 const HeadingItem = require( './HeadingItem.js' );
88 // Cached HTML may still have the placeholder heading constant in it.
89 // This code can be removed a few weeks after being deployed.
90 if ( hash.headingLevel === 99 ) {
91 hash.headingLevel = null;
92 }
93 item = new HeadingItem(
94 hash.range,
95 hash.headingLevel
96 );
97 break;
98 }
99 default:
100 throw new Error( 'Unknown ThreadItem type ' + hash.name );
101 }
102 item.name = hash.name;
103 item.id = hash.id;
104
105 item.rootNode = rootNode;
106
107 const idEscaped = $.escapeSelector( item.id );
108 const startMarker = document.getElementById( item.id );
109 const endMarker = document.querySelector( '[data-mw-comment-end="' + idEscaped + '"]' );
110
111 item.range = {
112 // Start range after startMarker, because it produces funny results from getBoundingClientRect
113 startContainer: startMarker.parentNode,
114 startOffset: utils.childIndexOf( startMarker ) + 1,
115 // End range inside endMarker, because modifier crashes if endContainer is a <p>/<dd>/<li> node
116 endContainer: endMarker,
117 endOffset: 0
118 };
119
120 if ( item.type === 'comment' ) {
121 const sigMarker = document.querySelector( '[data-mw-comment-sig="' + idEscaped + '"]' );
122 if ( sigMarker ) {
123 item.signatureRanges = [ {
124 startContainer: sigMarker.parentNode,
125 startOffset: utils.childIndexOf( sigMarker ) + 1,
126 endContainer: item.range.endContainer,
127 endOffset: item.range.endOffset
128 } ];
129 }
130 }
131
132 return item;
133 };
134
135 /**
136 * Calculate summary metadata for a thread.
137 */
138 ThreadItem.prototype.calculateThreadSummary = function () {
139 if ( this.authors ) {
140 return;
141 }
142 const authors = {};
143 let commentCount = 0;
144 let oldestReply = null;
145 let latestReply = null;
146 function threadScan( comment ) {
147 if ( comment.type === 'comment' ) {
148 authors[ comment.author ] = authors[ comment.author ] || {
149 username: comment.author,
150 displayNames: []
151 };
152 if (
153 comment.displayName &&
154 authors[ comment.author ].displayNames.indexOf( comment.displayName ) === -1
155 ) {
156 authors[ comment.author ].displayNames.push( comment.displayName );
157 }
158
159 if (
160 !oldestReply ||
161 ( comment.timestamp < oldestReply.timestamp )
162 ) {
163 oldestReply = comment;
164 }
165 if (
166 !latestReply ||
167 ( latestReply.timestamp < comment.timestamp )
168 ) {
169 latestReply = comment;
170 }
171 commentCount++;
172 }
173 comment.replies.forEach( threadScan );
174 }
175 this.replies.forEach( threadScan );
176
177 this.authors = Object.keys( authors ).sort().map( ( author ) => authors[ author ] );
178 this.commentCount = commentCount;
179 this.oldestReply = oldestReply;
180 this.latestReply = latestReply;
181 };
182
183 /**
184 * Get the list of authors in the comment tree below this thread item.
185 *
186 * Usually called on a HeadingItem to find all authors in a thread.
187 *
188 * @return {Object[]} Authors, with `username` and `displayNames` (list of display names) properties.
189 */
190 ThreadItem.prototype.getAuthorsBelow = function () {
191 this.calculateThreadSummary();
192 return this.authors;
193 };
194
195 /**
196 * Get the number of comment items in the tree below this thread item.
197 *
198 * @return {number}
199 */
200 ThreadItem.prototype.getCommentCount = function () {
201 this.calculateThreadSummary();
202 return this.commentCount;
203 };
204
205 /**
206 * Get the latest reply in the tree below this thread item, null if there are no replies
207 *
208 * @return {CommentItem|null}
209 */
210 ThreadItem.prototype.getLatestReply = function () {
211 this.calculateThreadSummary();
212 return this.latestReply;
213 };
214
215 /**
216 * Get the oldest reply in the tree below this thread item, null if there are no replies
217 *
218 * @return {CommentItem|null}
219 */
220 ThreadItem.prototype.getOldestReply = function () {
221 this.calculateThreadSummary();
222 return this.oldestReply;
223 };
224
225 /**
226 * Get the list of thread items in the comment tree below this thread item.
227 *
228 * @return {ThreadItem[]} Thread items
229 */
230 ThreadItem.prototype.getThreadItemsBelow = function () {
231 const threadItems = [];
232 function getReplies( comment ) {
233 threadItems.push( comment );
234 comment.replies.forEach( getReplies );
235 }
236
237 this.replies.forEach( getReplies );
238
239 return threadItems;
240 };
241
242 /**
243 * Get the range of the entire thread item
244 *
245 * @return {Range}
246 */
247 ThreadItem.prototype.getRange = function () {
248 const doc = this.range.startContainer.ownerDocument;
249 const nativeRange = doc.createRange();
250 nativeRange.setStart( this.range.startContainer, this.range.startOffset );
251 nativeRange.setEnd( this.range.endContainer, this.range.endOffset );
252 return nativeRange;
253 };
254
255 // Deprecated alias
256 ThreadItem.prototype.getNativeRange = ThreadItem.prototype.getRange;
257
258 // TODO: Implement getHTML/getText if required
259
260 module.exports = ThreadItem;