Skip to content

Commit

Permalink
refactor(parser)!: Remove old comment node
Browse files Browse the repository at this point in the history
  • Loading branch information
LuanRT committed Nov 22, 2024
1 parent 2c7f0ca commit 2f087d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 205 deletions.
179 changes: 0 additions & 179 deletions src/parser/classes/comments/Comment.ts

This file was deleted.

43 changes: 18 additions & 25 deletions src/parser/classes/comments/CommentThread.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Parser } from '../../index.js';
import Button from '../Button.js';
import ContinuationItem from '../ContinuationItem.js';
import Comment from './Comment.js';
import CommentView from './CommentView.js';
import CommentReplies from './CommentReplies.js';

import { InnertubeError } from '../../../utils/Utils.js';
import { observe, YTNode } from '../../helpers.js';

import type Actions from '../../../core/Actions.js';
import type { ObservedArray } from '../../helpers.js';
import type { ObservedArray, Memo } from '../../helpers.js';
import type { RawNode } from '../../index.js';

export default class CommentThread extends YTNode {
Expand All @@ -18,20 +17,15 @@ export default class CommentThread extends YTNode {
#actions?: Actions;
#continuation?: ContinuationItem;

comment: Comment | CommentView | null;
replies?: ObservedArray<Comment | CommentView>;
comment_replies_data: CommentReplies | null;
is_moderated_elq_comment: boolean;
has_replies: boolean;
public comment: CommentView | null;
public replies?: ObservedArray<CommentView>;
public comment_replies_data: CommentReplies | null;
public is_moderated_elq_comment: boolean;
public has_replies: boolean;

constructor(data: RawNode) {
super();

if (Reflect.has(data, 'commentViewModel')) {
this.comment = Parser.parseItem(data.commentViewModel, CommentView);
} else {
this.comment = Parser.parseItem(data.comment, Comment);
}
this.comment = Parser.parseItem(data.commentViewModel, CommentView);
this.comment_replies_data = Parser.parseItem(data.replies, CommentReplies);
this.is_moderated_elq_comment = data.isModeratedElqComment;
this.has_replies = !!this.comment_replies_data;
Expand All @@ -57,12 +51,8 @@ export default class CommentThread extends YTNode {
if (!response.on_response_received_endpoints_memo)
throw new InnertubeError('Unexpected response.', response);

this.replies = observe(response.on_response_received_endpoints_memo.getType(Comment, CommentView).map((comment) => {
comment.setActions(this.#actions);
return comment;
}));

this.#continuation = response?.on_response_received_endpoints_memo.getType(ContinuationItem).first();
this.replies = this.#getPatchedReplies(response.on_response_received_endpoints_memo);
this.#continuation = response.on_response_received_endpoints_memo.getType(ContinuationItem).first();

return this;
}
Expand Down Expand Up @@ -90,16 +80,19 @@ export default class CommentThread extends YTNode {
if (!response.on_response_received_endpoints_memo)
throw new InnertubeError('Unexpected response.', response);

this.replies = observe(response.on_response_received_endpoints_memo.getType(Comment, CommentView).map((comment) => {
comment.setActions(this.#actions);
return comment;
}));

this.replies = this.#getPatchedReplies(response.on_response_received_endpoints_memo);
this.#continuation = response.on_response_received_endpoints_memo.getType(ContinuationItem).first();

return this;
}


#getPatchedReplies(data: Memo): ObservedArray<CommentView> {
return observe(data.getType(CommentView).map((comment) => {
comment.setActions(this.#actions);
return comment;
}));
}

get has_continuation(): boolean {
if (!this.replies)
throw new InnertubeError('Cannot determine if there is a continuation because this thread\'s replies have not been loaded.');
Expand Down
1 change: 0 additions & 1 deletion src/parser/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export { default as ContinuationCommand } from './classes/commands/ContinuationC
export { default as GetKidsBlocklistPickerCommand } from './classes/commands/GetKidsBlocklistPickerCommand.js';
export { default as ShowDialogCommand } from './classes/commands/ShowDialogCommand.js';
export { default as AuthorCommentBadge } from './classes/comments/AuthorCommentBadge.js';
export { default as Comment } from './classes/comments/Comment.js';
export { default as CommentActionButtons } from './classes/comments/CommentActionButtons.js';
export { default as CommentDialog } from './classes/comments/CommentDialog.js';
export { default as CommentReplies } from './classes/comments/CommentReplies.js';
Expand Down

0 comments on commit 2f087d4

Please sign in to comment.