From daaba3745e07a8f6b4cda91b3459675b355e1bac Mon Sep 17 00:00:00 2001 From: LuanRT Date: Mon, 1 Aug 2022 15:54:54 -0300 Subject: [PATCH] feat: improve LiveChat types --- src/parser/youtube/LiveChat.ts | 57 ++++++++++++++++++++------------- src/parser/youtube/VideoInfo.ts | 6 ++-- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/parser/youtube/LiveChat.ts b/src/parser/youtube/LiveChat.ts index c74981ef..66fd38f4 100644 --- a/src/parser/youtube/LiveChat.ts +++ b/src/parser/youtube/LiveChat.ts @@ -2,15 +2,39 @@ import Parser, { LiveChatContinuation } from '../index'; import EventEmitter from '../../utils/EventEmitterLike'; import VideoInfo from './VideoInfo'; +import AddChatItemAction from '../classes/livechat/AddChatItemAction'; +import AddLiveChatTickerItemAction from '../classes/livechat/AddLiveChatTickerItemAction'; +import MarkChatItemAsDeletedAction from '../classes/livechat/MarkChatItemAsDeletedAction'; +import MarkChatItemsByAuthorAsDeletedAction from '../classes/livechat/MarkChatItemsByAuthorAsDeletedAction'; +import ReplaceChatItemAction from '../classes/livechat/ReplaceChatItemAction'; +import ReplayChatItemAction from '../classes/livechat/ReplayChatItemAction'; +import ShowLiveChatActionPanelAction from '../classes/livechat/ShowLiveChatActionPanelAction'; + import UpdateTitleAction from '../classes/livechat/UpdateTitleAction'; import UpdateDescriptionAction from '../classes/livechat/UpdateDescriptionAction'; import UpdateViewershipAction from '../classes/livechat/UpdateViewershipAction'; import UpdateDateTextAction from '../classes/livechat/UpdateDateTextAction'; import UpdateToggleButtonTextAction from '../classes/livechat/UpdateToggleButtonTextAction'; -import AddChatItemAction from '../classes/livechat/AddChatItemAction'; + +import AddBannerToLiveChatCommand from '../classes/livechat/AddBannerToLiveChatCommand'; +import RemoveBannerForLiveChatCommand from '../classes/livechat/RemoveBannerForLiveChatCommand'; +import ShowLiveChatTooltipCommand from '../classes/livechat/ShowLiveChatTooltipCommand'; import { InnertubeError } from '../../utils/Utils'; -import { ObservedArray, YTNode } from '../helpers'; +import { ObservedArray } from '../helpers'; + +export type ChatAction = + AddChatItemAction | AddBannerToLiveChatCommand | AddLiveChatTickerItemAction | + MarkChatItemAsDeletedAction | MarkChatItemsByAuthorAsDeletedAction | RemoveBannerForLiveChatCommand | + ReplaceChatItemAction | ReplayChatItemAction | ShowLiveChatActionPanelAction | ShowLiveChatTooltipCommand; + +export interface LiveMetadata { + title: UpdateTitleAction | undefined; + description: UpdateDescriptionAction | undefined; + views: UpdateViewershipAction | undefined; + likes: UpdateToggleButtonTextAction | undefined; + date: UpdateDateTextAction | undefined; +} class LiveChat extends EventEmitter { #actions; @@ -22,15 +46,7 @@ class LiveChat extends EventEmitter { #md_polling_interval_ms = 5000; initial_info?: LiveChatContinuation; - live_metadata; - - metadata?: { - title: UpdateTitleAction | undefined; - description: UpdateDescriptionAction | undefined; - views: UpdateViewershipAction | undefined; - likes: UpdateToggleButtonTextAction | undefined; - date: UpdateDateTextAction | undefined; - }; + metadata?: LiveMetadata; running = false; is_replay = false; @@ -42,14 +58,6 @@ class LiveChat extends EventEmitter { this.#actions = video_info.actions; this.#continuation = video_info.livechat?.continuation || undefined; this.is_replay = video_info.livechat?.is_replay || false; - - this.live_metadata = { - title: null as UpdateTitleAction | null, - description: null as UpdateDescriptionAction | null, - views: null as UpdateViewershipAction | null, - likes: null as UpdateToggleButtonTextAction | null, - date: null as UpdateDateTextAction | null - }; } start() { @@ -93,11 +101,12 @@ class LiveChat extends EventEmitter { })().catch((err) => Promise.reject(err)); }, this.#lc_polling_interval_ms); } + /** * Ensures actions are emitted at the right speed. * This was adapted from YouTube's compiled code (Android). */ - async #emitSmoothedActions(actions: ObservedArray) { + async #emitSmoothedActions(actions: ObservedArray) { const base = 1E4; let delay = actions.length < base / 80 ? 1 : 0; @@ -154,7 +163,7 @@ class LiveChat extends EventEmitter { /** * Sends a message. */ - async sendMessage(text: string) { + async sendMessage(text: string): Promise> { const response = await this.#actions.livechat('live_chat/send_message', { text, ...{ @@ -164,7 +173,11 @@ class LiveChat extends EventEmitter { }); const data = Parser.parseResponse(response.data); - return data.actions?.array().as(AddChatItemAction); + + if (!data.actions) + throw new InnertubeError('Response did not have an "actions" property. The call may have failed.'); + + return data.actions.array().as(AddChatItemAction); } async #wait(ms: number) { diff --git a/src/parser/youtube/VideoInfo.ts b/src/parser/youtube/VideoInfo.ts index 9cdb89a1..31d83a4a 100644 --- a/src/parser/youtube/VideoInfo.ts +++ b/src/parser/youtube/VideoInfo.ts @@ -134,12 +134,12 @@ class VideoInfo { this.primary_info = results.get({ type: 'VideoPrimaryInfo' })?.as(VideoPrimaryInfo); this.secondary_info = results.get({ type: 'VideoSecondaryInfo' })?.as(VideoSecondaryInfo); this.merchandise = results.get({ type: 'MerchandiseShelf' })?.as(MerchandiseShelf); - this.related_chip_cloud = secondary_results?.get({ type: 'RelatedChipCloud' })?.as(RelatedChipCloud)?.content.item().as(ChipCloud); + this.related_chip_cloud = secondary_results.get({ type: 'RelatedChipCloud' })?.as(RelatedChipCloud)?.content.item().as(ChipCloud); - this.watch_next_feed = secondary_results?.get({ type: 'ItemSection' })?.as(ItemSection)?.contents; + this.watch_next_feed = secondary_results.get({ type: 'ItemSection' })?.as(ItemSection)?.contents; if (this.watch_next_feed && Array.isArray(this.watch_next_feed)) - this.#watch_next_continuation = this.watch_next_feed?.pop()?.as(ContinuationItem); + this.#watch_next_continuation = this.watch_next_feed.pop()?.as(ContinuationItem); this.player_overlays = next?.player_overlays.item().as(PlayerOverlay);