feat: improve LiveChat types

This commit is contained in:
LuanRT
2022-08-01 15:54:54 -03:00
parent 323b90a98c
commit daaba3745e
2 changed files with 38 additions and 25 deletions

View File

@@ -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<YTNode>) {
async #emitSmoothedActions(actions: ObservedArray<ChatAction>) {
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<ObservedArray<AddChatItemAction>> {
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) {

View File

@@ -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);