feat: finalize comment section nodes (#280)

* fix: comment translation proto missing channel id

* feat: finalize nodes

* docs: update API ref

* chore: update tests
This commit is contained in:
LuanRT
2023-01-09 08:14:31 -03:00
committed by GitHub
parent 56e6e23453
commit dca61c3a22
28 changed files with 539 additions and 146 deletions

View File

@@ -1,16 +1,20 @@
import Parser from '../../index';
import Text from '../misc/Text';
import Thumbnail from '../misc/Thumbnail';
import Author from '../misc/Author';
import ToggleButton from '../ToggleButton';
import CommentReplyDialog from './CommentReplyDialog';
import CommentActionButtons from './CommentActionButtons';
import AuthorCommentBadge from './AuthorCommentBadge';
import Author from '../misc/Author';
import type Menu from '../menus/Menu';
import type CommentActionButtons from './CommentActionButtons';
import type SponsorCommentBadge from './SponsorCommentBadge';
import type PdgCommentChip from './PdgCommentChip';
import type { ApiResponse } from '../../../core/Actions';
import type Actions from '../../../core/Actions';
import Proto from '../../../proto/index';
import Actions from '../../../core/Actions';
import { InnertubeError } from '../../../utils/Utils';
import { YTNode, SuperParsedResult } from '../../helpers';
class Comment extends YTNode {
@@ -22,22 +26,23 @@ class Comment extends YTNode {
published: Text;
author_is_channel_owner: boolean;
current_user_reply_thumbnail: Thumbnail[];
author_badge;
sponsor_comment_badge: SponsorCommentBadge | null;
paid_comment_chip: PdgCommentChip | null;
author_badge: AuthorCommentBadge | null;
author: Author;
action_menu;
action_buttons;
action_menu: Menu | null;
action_buttons: CommentActionButtons | null;
comment_id: string;
vote_status: string;
vote_count: {
text: string;
short_text: string;
};
vote_count: string;
reply_count: number;
is_liked: boolean;
is_disliked: boolean;
is_hearted: boolean;
is_pinned: boolean;
is_member: boolean;
constructor(data: any) {
super();
@@ -45,6 +50,8 @@ class Comment extends YTNode {
this.published = new Text(data.publishedTimeText);
this.author_is_channel_owner = data.authorIsChannelOwner;
this.current_user_reply_thumbnail = Thumbnail.fromResponse(data.currentUserReplyThumbnail);
this.sponsor_comment_badge = Parser.parseItem<SponsorCommentBadge>(data.sponsorCommentBadge);
this.paid_comment_chip = Parser.parseItem<PdgCommentChip>(data.paidCommentChipRenderer);
this.author_badge = Parser.parseItem<AuthorCommentBadge>(data.authorCommentBadge, AuthorCommentBadge);
this.author = new Author({
@@ -54,30 +61,32 @@ class Comment extends YTNode {
metadataBadgeRenderer: this.author_badge?.orig_badge
} ] : null, data.authorThumbnail);
this.action_menu = Parser.parse(data.actionMenu);
this.action_buttons = Parser.parse(data.actionButtons);
this.action_menu = Parser.parseItem<Menu>(data.actionMenu);
this.action_buttons = Parser.parseItem<CommentActionButtons>(data.actionButtons);
this.comment_id = data.commentId;
this.vote_status = data.voteStatus;
this.vote_count = {
text: data.voteCount ? data.voteCount.accessibility.accessibilityData?.label.replace(/\D/g, '') : '0',
short_text: data.voteCount ? new Text(data.voteCount).toString() : '0'
};
this.vote_count = data.voteCount ? new Text(data.voteCount).toString() : '0';
this.reply_count = data.replyCount || 0;
this.is_liked = this.action_buttons.item().as(CommentActionButtons).like_button.item().as(ToggleButton).is_toggled;
this.is_disliked = this.action_buttons.item().as(CommentActionButtons).dislike_button.item().as(ToggleButton).is_toggled;
this.is_liked = !!this.action_buttons?.like_button?.is_toggled;
this.is_disliked = !!this.action_buttons?.dislike_button?.is_toggled;
this.is_hearted = !!this.action_buttons?.creator_heart?.is_hearted;
this.is_pinned = !!data.pinnedCommentBadge;
this.is_member = !!data.sponsorCommentBadge;
}
/**
* Likes the comment.
*/
async like() {
async like(): Promise<ApiResponse> {
if (!this.#actions)
throw new InnertubeError('An active caller must be provide to perform this operation.');
const button = this.action_buttons.item().as(CommentActionButtons).like_button.item().as(ToggleButton);
const button = this.action_buttons?.like_button;
if (!button)
throw new InnertubeError('Like button was not found.', { comment_id: this.comment_id });
if (button.is_toggled)
throw new InnertubeError('This comment is already liked', { comment_id: this.comment_id });
@@ -89,11 +98,14 @@ class Comment extends YTNode {
/**
* Dislikes the comment.
*/
async dislike() {
async dislike(): Promise<ApiResponse> {
if (!this.#actions)
throw new InnertubeError('An active caller must be provide to perform this operation.');
const button = this.action_buttons.item().as(CommentActionButtons).dislike_button.item().as(ToggleButton);
const button = this.action_buttons?.dislike_button;
if (!button)
throw new InnertubeError('Dislike button was not found.', { comment_id: this.comment_id });
if (button.is_toggled)
throw new InnertubeError('This comment is already disliked', { comment_id: this.comment_id });
@@ -106,26 +118,28 @@ class Comment extends YTNode {
/**
* Creates a reply to the comment.
*/
async reply(text: string) {
async reply(text: string): Promise<ApiResponse> {
if (!this.#actions)
throw new InnertubeError('An active caller must be provide to perform this operation.');
if (!this.action_buttons.item().as(CommentActionButtons).reply_button)
if (!this.action_buttons?.reply_button)
throw new InnertubeError('Cannot reply to another reply. Try mentioning the user instead.', { comment_id: this.comment_id });
const button = this.action_buttons.item().as(CommentActionButtons).reply_button.item().as(ToggleButton);
const button = this.action_buttons?.reply_button;
if (!button.endpoint.dialog)
if (!button.endpoint?.dialog)
throw new InnertubeError('Reply button endpoint did not have a dialog.');
const dialog = button.endpoint.dialog as SuperParsedResult<YTNode>;
const dialog_button = dialog.item().as(CommentReplyDialog).reply_button.item().as(ToggleButton);
const dialog_button = dialog.item().as(CommentReplyDialog).reply_button;
const payload = {
commentText: text
};
if (!dialog_button)
throw new InnertubeError('Reply button was not found in the dialog.', { comment_id: this.comment_id });
const response = await dialog_button.endpoint.call(this.#actions, payload);
if (!dialog_button.endpoint)
throw new InnertubeError('Reply button endpoint was not found.', { comment_id: this.comment_id });
const response = await dialog_button.endpoint.call(this.#actions, { commentText: text });
return response;
}
@@ -134,7 +148,12 @@ class Comment extends YTNode {
* Translates the comment to the given language.
* @param target_language - Ex; en, ja
*/
async translate(target_language: string) {
async translate(target_language: string): Promise<{
content: any;
success: boolean;
status_code: number;
data: any;
}> {
if (!this.#actions)
throw new InnertubeError('An active caller must be provide to perform this operation.');
@@ -151,8 +170,8 @@ class Comment extends YTNode {
const response = await this.#actions.execute('comment/perform_comment_action', { action, client: 'ANDROID' });
// TODO: maybe add these to Parser#parseResponse?
const mutations = response.data.frameworkUpdates.entityBatchUpdate.mutations;
const content = mutations[0].payload.commentEntityPayload.translatedContent.content;
const mutations = response.data.frameworkUpdates?.entityBatchUpdate?.mutations;
const content = mutations?.[0]?.payload?.commentEntityPayload?.translatedContent?.content;
return { ...response, content };
}

View File

@@ -1,4 +1,7 @@
import Parser from '../../index';
import type Button from '../Button';
import type ToggleButton from '../ToggleButton';
import type CreatorHeart from './CreatorHeart';
import { YTNode } from '../../helpers';
class CommentActionButtons extends YTNode {
@@ -7,12 +10,14 @@ class CommentActionButtons extends YTNode {
like_button;
dislike_button;
reply_button;
creator_heart;
constructor(data: any) {
super();
this.like_button = Parser.parse(data.likeButton);
this.dislike_button = Parser.parse(data.dislikeButton);
this.reply_button = Parser.parse(data.replyButton);
this.like_button = Parser.parseItem<ToggleButton>(data.likeButton);
this.dislike_button = Parser.parseItem<ToggleButton>(data.dislikeButton);
this.reply_button = Parser.parseItem<Button>(data.replyButton);
this.creator_heart = Parser.parseItem<CreatorHeart>(data.creatorHeart);
}
}

View File

@@ -0,0 +1,31 @@
import Parser from '../..';
import Text from '../misc/Text';
import Thumbnail from '../misc/Thumbnail';
import type Button from '../Button';
import type EmojiPicker from './EmojiPicker';
import { YTNode } from '../../helpers';
class CommentDialog extends YTNode {
static type = 'CommentDialog';
editable_text: Text;
author_thumbnail: Thumbnail[];
submit_button: Button | null;
cancel_button: Button | null;
placeholder: Text;
emoji_button: Button | null;
emoji_picker: any | null;
constructor(data: any) {
super();
this.editable_text = new Text(data.editableText);
this.author_thumbnail = Thumbnail.fromResponse(data.authorThumbnail);
this.submit_button = Parser.parseItem<Button>(data.submitButton);
this.cancel_button = Parser.parseItem<Button>(data.cancelButton);
this.placeholder = new Text(data.placeholderText);
this.emoji_button = Parser.parseItem<Button>(data.emojiButton);
this.emoji_picker = Parser.parseItem<EmojiPicker>(data.emojiPicker);
}
}
export default CommentDialog;

View File

@@ -1,18 +1,24 @@
import Parser from '../../index';
import Thumbnail from '../misc/Thumbnail';
import type Button from '../Button';
import { YTNode } from '../../helpers';
class CommentReplies extends YTNode {
static type = 'CommentReplies';
contents;
view_replies;
hide_replies;
view_replies: Button | null;
hide_replies: Button | null;
view_replies_creator_thumbnail: Thumbnail[];
has_channel_owner_replied: boolean;
constructor(data: any) {
super();
this.contents = Parser.parse(data.contents);
this.view_replies = Parser.parse(data.viewReplies);
this.hide_replies = Parser.parse(data.hideReplies);
this.contents = Parser.parseArray(data.contents);
this.view_replies = Parser.parseItem<Button>(data.viewReplies);
this.hide_replies = Parser.parseItem<Button>(data.hideReplies);
this.view_replies_creator_thumbnail = Thumbnail.fromResponse(data.viewRepliesCreatorThumbnail);
this.has_channel_owner_replied = !!data.viewRepliesCreatorThumbnail;
}
}

View File

@@ -1,21 +1,22 @@
import Parser from '../../index';
import Thumbnail from '../misc/Thumbnail';
import Text from '../misc/Text';
import type Button from '../Button';
import { YTNode } from '../../helpers';
class CommentReplyDialog extends YTNode {
static type = 'CommentReplyDialog';
reply_button;
cancel_button;
author_thumbnail;
placeholder;
error_message;
reply_button: Button | null;
cancel_button: Button | null;
author_thumbnail: Thumbnail[];
placeholder: Text;
error_message: Text;
constructor(data: any) {
super();
this.reply_button = Parser.parse(data.replyButton);
this.cancel_button = Parser.parse(data.cancelButton);
this.reply_button = Parser.parseItem<Button>(data.replyButton);
this.cancel_button = Parser.parseItem<Button>(data.cancelButton);
this.author_thumbnail = Thumbnail.fromResponse(data.authorThumbnail);
this.placeholder = new Text(data.placeholderText);
this.error_message = new Text(data.errorMessage);

View File

@@ -1,21 +1,22 @@
import Parser from '../../index';
import Thumbnail from '../misc/Thumbnail';
import Text from '../misc/Text';
import type Button from '../Button';
import { YTNode } from '../../helpers';
class CommentSimplebox extends YTNode {
static type = 'CommentSimplebox';
submit_button;
cancel_button;
submit_button: Button | null;
cancel_button: Button | null;
author_thumbnails: Thumbnail[];
placeholder: Text;
avatar_size;
constructor(data: any) {
super();
this.submit_button = Parser.parse(data.submitButton);
this.cancel_button = Parser.parse(data.cancelButton);
this.submit_button = Parser.parseItem<Button>(data.submitButton);
this.cancel_button = Parser.parseItem<Button>(data.cancelButton);
this.author_thumbnails = Thumbnail.fromResponse(data.authorThumbnail);
this.placeholder = new Text(data.placeholderText);
this.avatar_size = data.avatarSize;

View File

@@ -1,48 +1,56 @@
import Parser from '../../index';
import Comment from './Comment';
import ContinuationItem from '../ContinuationItem';
import Actions from '../../../core/Actions';
import NavigationEndpoint from '../NavigationEndpoint';
import CommentReplies from './CommentReplies';
import Button from '../Button';
import type Actions from '../../../core/Actions';
import type { ObservedArray } from '../../helpers';
import { InnertubeError } from '../../../utils/Utils';
import { YTNode } from '../../helpers';
import { observe, YTNode } from '../../helpers';
class CommentThread extends YTNode {
static type = 'CommentThread';
#replies;
#actions?: Actions;
#continuation?: ContinuationItem;
comment: Comment | null;
replies?: ObservedArray<Comment>;
comment_replies_data: CommentReplies | null;
is_moderated_elq_comment: boolean;
comment;
replies: Comment[] | undefined;
has_replies: boolean;
constructor(data: any) {
super();
this.comment = Parser.parseItem(data.comment, Comment);
this.#replies = Parser.parseItem(data.replies);
this.comment = Parser.parseItem<Comment>(data.comment, Comment);
this.comment_replies_data = Parser.parseItem<CommentReplies>(data.replies);
this.is_moderated_elq_comment = data.isModeratedElqComment;
this.has_replies = !!this.comment_replies_data;
}
/**
* Retrieves replies to this comment thread.
*/
async getReplies() {
async getReplies(): Promise<CommentThread> {
if (!this.#actions)
throw new InnertubeError('Actions not set for this CommentThread.');
throw new InnertubeError('Actions instance not set for this thread.');
if (!this.#replies)
if (!this.comment_replies_data)
throw new InnertubeError('This comment has no replies.', { comment_id: this.comment?.comment_id });
const continuation = this.#replies.key('contents').parsed().array().get({ type: 'ContinuationItem' })?.as(ContinuationItem);
const response = await continuation?.endpoint.call(this.#actions, { parse: true });
const continuation = this.comment_replies_data.contents?.firstOfType(ContinuationItem);
this.replies = response?.on_response_received_endpoints_memo?.getType(Comment).map((comment) => {
if (!continuation)
throw new InnertubeError('Replies continuation not found.');
const response = await continuation.endpoint.call(this.#actions, { parse: true });
this.replies = observe(response.on_response_received_endpoints_memo?.getType(Comment).map((comment) => {
comment.setActions(this.#actions);
return comment;
});
}));
this.#continuation = response?.on_response_received_endpoints_memo.getType(ContinuationItem)?.[0];
this.#continuation = response?.on_response_received_endpoints_memo.getType(ContinuationItem).first();
return this;
}
@@ -50,28 +58,39 @@ class CommentThread extends YTNode {
/**
* Retrieves next batch of replies.
*/
async getContinuation() {
async getContinuation(): Promise<CommentThread> {
if (!this.replies)
throw new InnertubeError('Continuation not available.');
throw new InnertubeError('Cannot retrieve continuation because this thread\'s replies have not been loaded.');
if (!this.#continuation)
throw new InnertubeError('Continuation not found.');
if (!this.#actions)
throw new InnertubeError('Actions not set for this CommentThread.');
throw new InnertubeError('Actions instance not set for this thread.');
const response = await this.#continuation.button?.item().key('endpoint').nodeOfType(NavigationEndpoint).call(this.#actions, { parse: true });
const load_more_button = this.#continuation.button?.as(Button);
this.replies = response?.on_response_received_endpoints_memo.getType(Comment).map((comment) => {
if (!load_more_button)
throw new InnertubeError('"Load more" button not found.');
const response = await load_more_button.endpoint.call(this.#actions, { parse: true });
this.replies = observe(response?.on_response_received_endpoints_memo.getType(Comment).map((comment) => {
comment.setActions(this.#actions);
return comment;
});
}));
this.#continuation = response?.on_response_received_endpoints_memo.getType(ContinuationItem)?.[0];
return this;
}
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.');
return !!this.#continuation;
}
setActions(actions: Actions) {
this.#actions = actions;
}

View File

@@ -1,6 +1,7 @@
import Parser from '../../index';
import Text from '../misc/Text';
import Thumbnail from '../misc/Thumbnail';
import type SortFilterSubMenu from '../SortFilterSubMenu';
import { YTNode } from '../../helpers';
class CommentsHeader extends YTNode {
@@ -10,7 +11,7 @@ class CommentsHeader extends YTNode {
count: Text;
comments_count: Text;
create_renderer;
sort_menu;
sort_menu: SortFilterSubMenu | null;
custom_emojis: {
emoji_id: string;
@@ -26,7 +27,7 @@ class CommentsHeader extends YTNode {
this.count = new Text(data.countText);
this.comments_count = new Text(data.commentsCount);
this.create_renderer = Parser.parseItem(data.createRenderer);
this.sort_menu = Parser.parse(data.sortMenu);
this.sort_menu = Parser.parseItem<SortFilterSubMenu>(data.sortMenu);
this.custom_emojis = data.customEmojis?.map((emoji: any) => ({
emoji_id: emoji.emojiId,

View File

@@ -0,0 +1,35 @@
import { YTNode } from '../../helpers';
import Thumbnail from '../misc/Thumbnail';
class CreatorHeart extends YTNode {
static type = 'CreatorHeart';
creator_thumbnail: Thumbnail[];
heart_icon_type: string;
heart_color: {
basic_color_palette_data: {
foreground_title_color: string;
}
};
hearted_tooltip: string;
is_hearted: boolean;
is_enabled: boolean;
kennedy_heart_color_string: string;
constructor(data: any) {
super();
this.creator_thumbnail = Thumbnail.fromResponse(data.creatorThumbnail);
this.heart_icon_type = data.heartIcon?.iconType;
this.heart_color = {
basic_color_palette_data: {
foreground_title_color: data.heartColor?.basicColorPaletteData?.foregroundTitleColor
}
};
this.hearted_tooltip = data.heartedTooltip;
this.is_hearted = data.isHearted;
this.is_enabled = data.isEnabled;
this.kennedy_heart_color_string = data.kennedyHeartColorString;
}
}
export default CreatorHeart;

View File

@@ -0,0 +1,40 @@
import Text from '../misc/Text';
import { YTNode } from '../../helpers';
import Parser from '../..';
class EmojiPicker extends YTNode {
static type = 'EmojiPicker';
id: string;
categories: any[];
category_buttons: any[];
search_placeholder: Text;
search_no_results: Text;
pick_skin_tone: Text;
clear_search_label: string;
skin_tone_generic_label: string;
skin_tone_light_label: string;
skin_tone_medium_light_label: string;
skin_tone_medium_label: string;
skin_tone_medium_dark_label: string;
skin_tone_dark_label: string;
constructor(data: any) {
super();
this.id = data.id;
this.categories = Parser.parseArray(data.categories);
this.category_buttons = Parser.parseArray(data.categoryButtons);
this.search_placeholder = new Text(data.searchPlaceholderText);
this.search_no_results = new Text(data.searchNoResultsText);
this.pick_skin_tone = new Text(data.pickSkinToneText);
this.clear_search_label = data.clearSearchLabel;
this.skin_tone_generic_label = data.skinToneGenericLabel;
this.skin_tone_light_label = data.skinToneLightLabel;
this.skin_tone_medium_light_label = data.skinToneMediumLightLabel;
this.skin_tone_medium_label = data.skinToneMediumLabel;
this.skin_tone_medium_dark_label = data.skinToneMediumDarkLabel;
this.skin_tone_dark_label = data.skinToneDarkLabel;
}
}
export default EmojiPicker;

View File

@@ -0,0 +1,25 @@
import Text from '../misc/Text';
import { YTNode } from '../../helpers';
class PdgCommentChip extends YTNode {
static type = 'PdgCommentChip';
text: Text;
color_pallette: {
background_color: string;
foreground_title_color: string;
};
icon_type: string;
constructor(data: any) {
super();
this.text = new Text(data.chipText);
this.color_pallette = {
background_color: data.chipColorPalette?.backgroundColor,
foreground_title_color: data.chipColorPalette?.foregroundTitleColor
};
this.icon_type = data.chipIcon?.iconType;
}
}
export default PdgCommentChip;

View File

@@ -0,0 +1,17 @@
import Thumbnail from '../misc/Thumbnail';
import { YTNode } from '../../helpers';
class SponsorCommentBadge extends YTNode {
static type = 'SponsorCommentBadge';
custom_badge: Thumbnail[];
tooltip: string;
constructor(data: any) {
super();
this.custom_badge = Thumbnail.fromResponse(data.customBadge);
this.tooltip = data.tooltip;
}
}
export default SponsorCommentBadge;