Files
YouTube.js/lib/parser/contents/classes/CommentThread.js
LuanRT 1d62e469a9 refactor: rewrite Comments Section logic (#88)
* feat: add core comments section classes

* chore: update type declarations

* chore: fix linter warnings

* style: fix linter

* chore: update tests

* chore(tests): fix typo

* chore(tests): fix typo x2

* fix(tests): `getReplies()` method is only present in `CommentThread` and not `Comment`

* chore(tests): fix comment id path

* chore(tests): remove outdated code

* chore(tests): fix results path

* chore: enforce code style

* chore: update type declarations

* docs: add examples and documentation

* chore(docs): fix paths

* chore(docs): fix more paths

* chore(docs): fix `Comments.js` path

* chore(docs): fix typo

* chore(docs): mention example file

* chore(examples): fix imports

* chore(examples): fix typo
2022-07-02 19:55:33 -03:00

74 lines
2.0 KiB
JavaScript

'use strict';
const Parser = require('..');
const { InnertubeError } = require('../../../utils/Utils');
class CommentThread {
type = 'CommentThread';
#replies;
#actions;
#continuation;
constructor(data) {
/** @type {import('./Comment')} */
this.comment = Parser.parse(data.comment);
this.#replies = Parser.parse(data.replies, 'comments');
/** @type {boolean} */
this.is_moderated_elq_comment = data.isModeratedElqComment;
}
/**
* Retrieves replies to this comment thread.
* @returns {Promise.<CommentThread>}
*/
async getReplies() {
if (!this.#replies)
throw new InnertubeError('This comment has no replies.', { comment_id: this.comment.comment_id });
const continuation = this.#replies.contents.get({ type: 'ContinuationItem' });
const response = await continuation.endpoint.callTest(this.#actions);
this.replies = response.on_response_received_endpoints_memo.get('Comment').map((comment) => {
comment.setActions(this.#actions);
return comment;
});
this.#continuation = response.on_response_received_endpoints_memo.get('ContinuationItem')?.[0];
return this;
}
/**
* Retrieves next batch of replies.
* @returns {Promise.<CommentThread>}
*/
async getContinuation() {
if (!this.replies)
throw new InnertubeError('Continuation not available.');
if (!this.#continuation)
throw new InnertubeError('Continuation not found.');
const response = await this.#continuation.button.endpoint.callTest(this.#actions);
this.replies = response.on_response_received_endpoints_memo.get('Comment').map((comment) => {
comment.setActions(this.#actions);
return comment;
});
this.#continuation = response.on_response_received_endpoints_memo.get('ContinuationItem')?.[0];
return this;
}
/**
* @param {import('../../../core/Actions')} actions
* @private
*/
setActions(actions) {
this.#actions = actions;
}
}
module.exports = CommentThread;