chore(parser): lint

This commit is contained in:
Luan
2024-11-22 05:11:17 -03:00
parent 2f087d47a0
commit 91fa215235
3 changed files with 75 additions and 74 deletions

View File

@@ -1,27 +1,27 @@
import { Parser } from '../../index.js';
import Button from '../Button.js';
import ContinuationItem from '../ContinuationItem.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, Memo } from '../../helpers.js';
import type { RawNode } from '../../index.js';
import type Actions from '../../../core/Actions.js';
import type { Memo, ObservedArray } from '../../helpers.js';
export default class CommentThread extends YTNode {
static type = 'CommentThread';
#actions?: Actions;
#continuation?: ContinuationItem;
public comment: CommentView | null;
public replies?: ObservedArray<CommentView>;
public comment_replies_data: CommentReplies | null;
public is_moderated_elq_comment: boolean;
public has_replies: boolean;
#actions?: Actions;
#continuation?: ContinuationItem;
constructor(data: RawNode) {
super();
@@ -31,6 +31,12 @@ export default class CommentThread extends YTNode {
this.has_replies = !!this.comment_replies_data;
}
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;
}
/**
* Retrieves replies to this comment thread.
*/
@@ -85,21 +91,15 @@ export default class CommentThread extends YTNode {
return this;
}
setActions(actions: Actions) {
this.#actions = actions;
}
#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.');
return !!this.#continuation;
}
setActions(actions: Actions) {
this.#actions = actions;
}
}

View File

@@ -10,42 +10,47 @@ import type Actions from '../../../core/Actions.js';
import type { ApiResponse } from '../../../core/Actions.js';
import type { RawNode } from '../../index.js';
// TODO: Move these types to a different file.
export type CommentKeys = {
comment: string;
comment_surface: string;
toolbar_state: string;
toolbar_surface: string;
shared: string;
}
export type MemberBadge = {
url: string;
a11y: string;
}
export default class CommentView extends YTNode {
static type = 'CommentView';
#actions?: Actions;
like_command?: NavigationEndpoint;
dislike_command?: NavigationEndpoint;
unlike_command?: NavigationEndpoint;
undislike_command?: NavigationEndpoint;
reply_command?: NavigationEndpoint;
public like_command?: NavigationEndpoint;
public dislike_command?: NavigationEndpoint;
public unlike_command?: NavigationEndpoint;
public undislike_command?: NavigationEndpoint;
public reply_command?: NavigationEndpoint;
comment_id: string;
is_pinned: boolean;
keys: {
comment: string;
comment_surface: string;
toolbar_state: string;
toolbar_surface: string;
shared: string;
};
public comment_id: string;
public is_pinned: boolean;
public keys: CommentKeys;
content?: Text;
published_time?: string;
author_is_channel_owner?: boolean;
like_count?: string;
reply_count?: string;
is_member?: boolean;
member_badge?: {
url: string,
a11y: string;
};
author?: Author;
public content?: Text;
public published_time?: string;
public author_is_channel_owner?: boolean;
public like_count?: string;
public reply_count?: string;
public is_member?: boolean;
public member_badge?: MemberBadge;
public author?: Author;
is_liked?: boolean;
is_disliked?: boolean;
is_hearted?: boolean;
public is_liked?: boolean;
public is_disliked?: boolean;
public is_hearted?: boolean;
constructor(data: RawNode) {
super();
@@ -205,7 +210,7 @@ export default class CommentView extends YTNode {
/**
* Translates the comment to the specified target language.
* @param target_language - The target language to translate the comment to, e.g. 'en', 'ja'.
* @returns A Promise that resolves to an ApiResponse object with the translated content, if available.
* @returns Resolves to an ApiResponse object with the translated content, if available.
* @throws if the Actions instance is not set for this comment or if the comment content is not found.
*/
async translate(target_language: string): Promise<ApiResponse & { content?: string }> {
@@ -218,13 +223,10 @@ export default class CommentView extends YTNode {
// Emojis must be removed otherwise InnerTube throws a 400 status code at us.
const text = this.content.toString().replace(/[^\p{L}\p{N}\p{P}\p{Z}]/gu, '');
const payload = {
text,
target_language
};
const payload = { text, target_language };
const action = ProtoUtils.encodeCommentActionParams(22, payload);
const response = await this.#actions.execute('comment/perform_comment_action', { action, client: 'ANDROID' });
const response = await this.#actions.execute('comment/perform_comment_action', { action });
// XXX: Should move this to Parser#parseResponse
const mutations = response.data.frameworkUpdates?.entityBatchUpdate?.mutations;

View File

@@ -6,22 +6,23 @@ import Thumbnail from '../misc/Thumbnail.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
export type CustomEmoji = {
emoji_id: string;
shortcuts: string[];
search_terms: string[];
image: Thumbnail[];
is_custom_emoji: boolean;
}
export default class CommentsHeader extends YTNode {
static type = 'CommentsHeader';
title: Text;
count: Text;
comments_count: Text;
create_renderer;
sort_menu: SortFilterSubMenu | null;
custom_emojis?: {
emoji_id: string;
shortcuts: string[];
search_terms: string[];
image: Thumbnail[];
is_custom_emoji: boolean;
}[];
public title: Text;
public count: Text;
public comments_count: Text;
public create_renderer;
public sort_menu: SortFilterSubMenu | null;
public custom_emojis?: CustomEmoji[];
constructor(data: RawNode) {
super();
@@ -32,15 +33,13 @@ export default class CommentsHeader extends YTNode {
this.sort_menu = Parser.parseItem(data.sortMenu, SortFilterSubMenu);
if (Reflect.has(data, 'customEmojis')) {
this.custom_emojis = data.customEmojis.map((emoji: RawNode) => {
return {
emoji_id: emoji.emojiId,
shortcuts: emoji.shortcuts,
search_terms: emoji.searchTerms,
image: Thumbnail.fromResponse(emoji.image),
is_custom_emoji: emoji.isCustomEmoji
};
});
this.custom_emojis = data.customEmojis.map((emoji: RawNode) => ({
emoji_id: emoji.emojiId,
shortcuts: emoji.shortcuts,
search_terms: emoji.searchTerms,
image: Thumbnail.fromResponse(emoji.image),
is_custom_emoji: emoji.isCustomEmoji
}));
}
}
}