2020-10-21 15:52:04 +00:00
|
|
|
/* global moment */
|
2020-06-25 12:23:17 +00:00
|
|
|
|
2020-07-20 14:48:41 +00:00
|
|
|
var utils = require( './utils.js' );
|
|
|
|
|
2020-06-25 12:23:17 +00:00
|
|
|
/**
|
|
|
|
* A thread item, either a heading or a comment
|
|
|
|
*
|
|
|
|
* @class ThreadItem
|
|
|
|
* @constructor
|
|
|
|
* @param {string} type `heading` or `comment`
|
2020-10-01 19:36:11 +00:00
|
|
|
* @param {number} level Indentation level
|
2020-06-25 12:23:17 +00:00
|
|
|
* @param {Object} range Object describing the extent of the comment, including the
|
|
|
|
* signature and timestamp. It has the same properties as a Range object: `startContainer`,
|
|
|
|
* `startOffset`, `endContainer`, `endOffset` (we don't use a real Range because they change
|
|
|
|
* magically when the DOM structure changes).
|
|
|
|
*/
|
2020-05-22 16:26:05 +00:00
|
|
|
function ThreadItem( type, level, range ) {
|
|
|
|
this.type = type;
|
|
|
|
this.level = level;
|
|
|
|
this.range = range;
|
|
|
|
|
2020-06-25 12:23:17 +00:00
|
|
|
/**
|
2021-02-12 19:16:13 +00:00
|
|
|
* @member {string} Name for this comment, intended to be used to
|
2020-06-25 12:23:17 +00:00
|
|
|
* find this comment in other revisions of the same page
|
|
|
|
*/
|
2021-02-12 19:16:13 +00:00
|
|
|
this.name = null;
|
|
|
|
/**
|
|
|
|
* @member {string} Unique ID (within the page) for this comment
|
|
|
|
*/
|
2020-05-22 16:26:05 +00:00
|
|
|
this.id = null;
|
2020-06-25 12:23:17 +00:00
|
|
|
/**
|
2020-12-12 12:52:17 +00:00
|
|
|
* @member {ThreadItem[]} Replies to this thread item
|
2020-06-25 12:23:17 +00:00
|
|
|
*/
|
2020-05-22 16:26:05 +00:00
|
|
|
this.replies = [];
|
2020-07-29 23:57:51 +00:00
|
|
|
|
2020-11-02 18:35:38 +00:00
|
|
|
/**
|
|
|
|
* @member {string[]} Warnings
|
|
|
|
*/
|
|
|
|
this.warnings = [];
|
|
|
|
|
2020-07-29 23:57:51 +00:00
|
|
|
this.rootNode = null;
|
2022-07-05 23:21:34 +00:00
|
|
|
|
|
|
|
this.authors = null;
|
|
|
|
this.commentCount = null;
|
|
|
|
this.oldestReply = null;
|
|
|
|
this.latestReply = null;
|
2020-05-22 16:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OO.initClass( ThreadItem );
|
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
/**
|
|
|
|
* Create a new ThreadItem from a JSON serialization
|
|
|
|
*
|
|
|
|
* @param {string|Object} json JSON serialization or hash object
|
2022-06-15 00:30:54 +00:00
|
|
|
* @param {HTMLElement} rootNode
|
2020-09-16 12:07:27 +00:00
|
|
|
* @return {ThreadItem}
|
|
|
|
* @throws {Error} Unknown ThreadItem type
|
|
|
|
*/
|
2022-06-15 00:30:54 +00:00
|
|
|
ThreadItem.static.newFromJSON = function ( json, rootNode ) {
|
2020-10-21 15:52:04 +00:00
|
|
|
// The page can be served from the HTTP cache (Varnish), and the JSON may be generated
|
|
|
|
// by an older version of our PHP code. Code below must be able to handle that.
|
|
|
|
// See ThreadItem::jsonSerialize() in PHP.
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var hash = typeof json === 'string' ? JSON.parse( json ) : json;
|
2020-10-27 12:18:50 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var item;
|
2020-09-16 12:07:27 +00:00
|
|
|
switch ( hash.type ) {
|
|
|
|
case 'comment':
|
|
|
|
// Late require to avoid circular dependency
|
2021-04-08 13:46:09 +00:00
|
|
|
var CommentItem = require( './CommentItem.js' );
|
2020-09-16 12:07:27 +00:00
|
|
|
item = new CommentItem(
|
|
|
|
hash.level,
|
|
|
|
hash.range,
|
|
|
|
hash.signatureRanges,
|
2022-10-04 12:50:57 +00:00
|
|
|
hash.timestampRanges,
|
2022-06-20 20:12:06 +00:00
|
|
|
moment.utc( hash.timestamp, [
|
|
|
|
// See CommentItem#getTimestampString for notes about the two formats.
|
|
|
|
'YYYYMMDDHHmmss',
|
|
|
|
moment.ISO_8601
|
|
|
|
], true ),
|
2022-02-04 18:16:24 +00:00
|
|
|
hash.author,
|
|
|
|
hash.displayName
|
2020-09-16 12:07:27 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'heading':
|
2021-04-08 13:46:09 +00:00
|
|
|
var HeadingItem = require( './HeadingItem.js' );
|
2022-06-06 17:35:43 +00:00
|
|
|
// Cached HTML may still have the placeholder heading constant in it.
|
|
|
|
// This code can be removed a few weeks after being deployed.
|
|
|
|
if ( hash.headingLevel === 99 ) {
|
|
|
|
hash.headingLevel = null;
|
|
|
|
}
|
2020-09-16 12:07:27 +00:00
|
|
|
item = new HeadingItem(
|
|
|
|
hash.range,
|
2022-06-06 17:35:43 +00:00
|
|
|
hash.headingLevel
|
2020-09-16 12:07:27 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error( 'Unknown ThreadItem type ' + hash.name );
|
|
|
|
}
|
2021-02-12 19:16:13 +00:00
|
|
|
item.name = hash.name;
|
2020-10-01 19:36:11 +00:00
|
|
|
item.id = hash.id;
|
|
|
|
|
2022-06-15 00:30:54 +00:00
|
|
|
item.rootNode = rootNode;
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var idEscaped = $.escapeSelector( item.id );
|
2021-05-27 14:29:22 +00:00
|
|
|
var startMarker = document.getElementById( item.id );
|
|
|
|
var endMarker = document.querySelector( '[data-mw-comment-end="' + idEscaped + '"]' );
|
|
|
|
|
2020-10-27 12:18:50 +00:00
|
|
|
item.range = {
|
2021-05-27 19:28:28 +00:00
|
|
|
// Start range after startMarker, because it produces funny results from getBoundingClientRect
|
2021-05-27 14:29:22 +00:00
|
|
|
startContainer: startMarker.parentNode,
|
|
|
|
startOffset: utils.childIndexOf( startMarker ) + 1,
|
2021-05-27 19:28:28 +00:00
|
|
|
// End range inside endMarker, because modifier crashes if endContainer is a <p>/<dd>/<li> node
|
|
|
|
endContainer: endMarker,
|
|
|
|
endOffset: 0
|
2020-10-27 12:18:50 +00:00
|
|
|
};
|
2020-09-16 12:07:27 +00:00
|
|
|
|
|
|
|
return item;
|
|
|
|
};
|
|
|
|
|
2022-07-05 17:36:56 +00:00
|
|
|
/**
|
2022-07-05 23:21:34 +00:00
|
|
|
* Calculate summary metadata for a thread.
|
2022-07-05 17:36:56 +00:00
|
|
|
*/
|
2022-07-05 23:21:34 +00:00
|
|
|
ThreadItem.prototype.calculateThreadSummary = function () {
|
|
|
|
if ( this.authors ) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-05 17:36:56 +00:00
|
|
|
var authors = {};
|
|
|
|
var commentCount = 0;
|
|
|
|
var oldestReply = null;
|
|
|
|
var latestReply = null;
|
|
|
|
function threadScan( comment ) {
|
|
|
|
if ( comment.type === 'comment' ) {
|
2022-09-06 13:16:10 +00:00
|
|
|
authors[ comment.author ] = authors[ comment.author ] || {
|
|
|
|
username: comment.author,
|
|
|
|
displayNames: []
|
|
|
|
};
|
|
|
|
if (
|
|
|
|
comment.displayName &&
|
|
|
|
authors[ comment.author ].displayNames.indexOf( comment.displayName ) === -1
|
|
|
|
) {
|
|
|
|
authors[ comment.author ].displayNames.push( comment.displayName );
|
|
|
|
}
|
|
|
|
|
2022-07-05 17:36:56 +00:00
|
|
|
if (
|
|
|
|
!oldestReply ||
|
|
|
|
( comment.timestamp < oldestReply.timestamp )
|
|
|
|
) {
|
|
|
|
oldestReply = comment;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!latestReply ||
|
|
|
|
( latestReply.timestamp < comment.timestamp )
|
|
|
|
) {
|
|
|
|
latestReply = comment;
|
|
|
|
}
|
|
|
|
commentCount++;
|
|
|
|
}
|
|
|
|
comment.replies.forEach( threadScan );
|
|
|
|
}
|
|
|
|
this.replies.forEach( threadScan );
|
|
|
|
|
2022-09-06 13:16:10 +00:00
|
|
|
this.authors = Object.keys( authors ).sort().map( function ( author ) {
|
|
|
|
return authors[ author ];
|
|
|
|
} );
|
2022-07-05 23:21:34 +00:00
|
|
|
this.commentCount = commentCount;
|
|
|
|
this.oldestReply = oldestReply;
|
|
|
|
this.latestReply = latestReply;
|
2022-07-05 17:36:56 +00:00
|
|
|
};
|
|
|
|
|
2020-07-20 14:13:59 +00:00
|
|
|
/**
|
|
|
|
* Get the list of authors in the comment tree below this thread item.
|
|
|
|
*
|
|
|
|
* Usually called on a HeadingItem to find all authors in a thread.
|
|
|
|
*
|
2022-09-06 13:16:10 +00:00
|
|
|
* @return {Object[]} Authors, with `username` and `displayNames` (list of display names) properties.
|
2020-07-20 14:13:59 +00:00
|
|
|
*/
|
|
|
|
ThreadItem.prototype.getAuthorsBelow = function () {
|
2022-07-05 23:21:34 +00:00
|
|
|
this.calculateThreadSummary();
|
|
|
|
return this.authors;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of comment items in the tree below this thread item.
|
|
|
|
*
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
ThreadItem.prototype.getCommentCount = function () {
|
|
|
|
this.calculateThreadSummary();
|
|
|
|
return this.commentCount;
|
|
|
|
};
|
2020-07-20 14:13:59 +00:00
|
|
|
|
2022-07-05 23:21:34 +00:00
|
|
|
/**
|
|
|
|
* Get the latest reply in the tree below this thread item, null if there are no replies
|
|
|
|
*
|
|
|
|
* @return {CommentItem|null}
|
|
|
|
*/
|
|
|
|
ThreadItem.prototype.getLatestReply = function () {
|
|
|
|
this.calculateThreadSummary();
|
|
|
|
return this.latestReply;
|
|
|
|
};
|
2020-07-20 14:13:59 +00:00
|
|
|
|
2022-07-05 23:21:34 +00:00
|
|
|
/**
|
|
|
|
* Get the oldest reply in the tree below this thread item, null if there are no replies
|
|
|
|
*
|
|
|
|
* @return {CommentItem|null}
|
|
|
|
*/
|
|
|
|
ThreadItem.prototype.getOldestReply = function () {
|
|
|
|
this.calculateThreadSummary();
|
|
|
|
return this.oldestReply;
|
2020-07-20 14:13:59 +00:00
|
|
|
};
|
|
|
|
|
2022-02-21 00:22:39 +00:00
|
|
|
/**
|
|
|
|
* Get the list of thread items in the comment tree below this thread item.
|
|
|
|
*
|
|
|
|
* @return {ThreadItem[]} Thread items
|
|
|
|
*/
|
|
|
|
ThreadItem.prototype.getThreadItemsBelow = function () {
|
|
|
|
var threadItems = [];
|
|
|
|
function getReplies( comment ) {
|
|
|
|
threadItems.push( comment );
|
|
|
|
comment.replies.forEach( getReplies );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.replies.forEach( getReplies );
|
|
|
|
|
|
|
|
return threadItems;
|
|
|
|
};
|
|
|
|
|
2020-07-22 18:34:08 +00:00
|
|
|
/**
|
2023-12-15 16:22:52 +00:00
|
|
|
* Get the range of the entire thread item
|
2020-07-22 18:34:08 +00:00
|
|
|
*
|
|
|
|
* @return {Range}
|
|
|
|
*/
|
2023-12-15 16:22:52 +00:00
|
|
|
ThreadItem.prototype.getRange = function () {
|
2021-04-08 13:46:09 +00:00
|
|
|
var doc = this.range.startContainer.ownerDocument;
|
|
|
|
var nativeRange = doc.createRange();
|
2020-07-22 18:34:08 +00:00
|
|
|
nativeRange.setStart( this.range.startContainer, this.range.startOffset );
|
2020-08-11 04:22:57 +00:00
|
|
|
nativeRange.setEnd( this.range.endContainer, this.range.endOffset );
|
2020-07-22 18:34:08 +00:00
|
|
|
return nativeRange;
|
|
|
|
};
|
|
|
|
|
2023-12-15 16:22:52 +00:00
|
|
|
// Deprecated alias
|
|
|
|
ThreadItem.prototype.getNativeRange = ThreadItem.prototype.getRange;
|
|
|
|
|
2020-07-22 18:25:34 +00:00
|
|
|
// TODO: Implement getHTML/getText if required
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
module.exports = ThreadItem;
|