From e0b1e74a8f517db8988fc38f2fcb6e3b74883895 Mon Sep 17 00:00:00 2001 From: jonz94 Date: Sat, 2 Nov 2024 00:09:45 +0800 Subject: [PATCH 1/6] feat(parser): Add `LiveChatSponsorshipsGiftPurchaseAnnouncement` and `LiveChatSponsorshipsHeader` nodes --- ...hatSponsorshipsGiftPurchaseAnnouncement.ts | 20 ++++++++++ .../items/LiveChatSponsorshipsHeader.ts | 39 +++++++++++++++++++ src/parser/nodes.ts | 2 + 3 files changed, 61 insertions(+) create mode 100644 src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts create mode 100644 src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts diff --git a/src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts b/src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts new file mode 100644 index 000000000..39f51f90a --- /dev/null +++ b/src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts @@ -0,0 +1,20 @@ +import { YTNode } from '../../../helpers.js'; +import { Parser, type RawNode } from '../../../index.js'; +import LiveChatSponsorshipsHeader from './LiveChatSponsorshipsHeader.js'; + +export default class LiveChatSponsorshipsGiftPurchaseAnnouncement extends YTNode { + static type = 'LiveChatSponsorshipsGiftPurchaseAnnouncement'; + + id: string; + timestamp_usec: string; + author_external_channel_id: string; + header: LiveChatSponsorshipsHeader | null; + + constructor(data: RawNode) { + super(); + this.id = data.id; + this.timestamp_usec = data.timestampUsec; + this.author_external_channel_id = data.authorExternalChannelId; + this.header = Parser.parseItem(data.header, LiveChatSponsorshipsHeader); + } +} \ No newline at end of file diff --git a/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts b/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts new file mode 100644 index 000000000..de6aab2aa --- /dev/null +++ b/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts @@ -0,0 +1,39 @@ +import { Parser } from '../../../index.js'; +import { YTNode } from '../../../helpers.js'; +import type { ObservedArray } from '../../../helpers.js'; +import type { RawNode } from '../../../index.js'; +import NavigationEndpoint from '../../NavigationEndpoint.js'; +import Text from '../../misc/Text.js'; +import Thumbnail from '../../misc/Thumbnail.js'; +import LiveChatAuthorBadge from '../../LiveChatAuthorBadge.js'; + +export default class LiveChatSponsorshipsHeader extends YTNode { + static type = 'LiveChatSponsorshipsHeader'; + + author_name: Text; + author_photo: Thumbnail[]; + primary_text: Text; + author_badges: ObservedArray | null; + menu_endpoint: NavigationEndpoint; + context_menu_accessibility: { + accessibility_data: { + label: string + } + }; + image: Thumbnail[]; + + constructor(data: RawNode) { + super(); + this.author_name = new Text(data.authorName); + this.author_photo = Thumbnail.fromResponse(data.authorPhoto); + this.primary_text = new Text(data.primaryText); + this.author_badges = Parser.parse(data.authorBadges, true, LiveChatAuthorBadge); + this.menu_endpoint = new NavigationEndpoint(data.contextMenuEndpoint); + this.context_menu_accessibility = { + accessibility_data: { + label: data.contextMenuAccessibility.accessibilityData.label + } + }; + this.image = Thumbnail.fromResponse(data.image); + } +} diff --git a/src/parser/nodes.ts b/src/parser/nodes.ts index a6dc9d53e..f254c5558 100644 --- a/src/parser/nodes.ts +++ b/src/parser/nodes.ts @@ -187,6 +187,8 @@ export { default as LiveChatPaidSticker } from './classes/livechat/items/LiveCha export { default as LiveChatPlaceholderItem } from './classes/livechat/items/LiveChatPlaceholderItem.js'; export { default as LiveChatProductItem } from './classes/livechat/items/LiveChatProductItem.js'; export { default as LiveChatRestrictedParticipation } from './classes/livechat/items/LiveChatRestrictedParticipation.js'; +export { default as LiveChatSponsorshipsGiftPurchaseAnnouncement } from './classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.js'; +export { default as LiveChatSponsorshipsHeader } from './classes/livechat/items/LiveChatSponsorshipsHeader.js'; export { default as LiveChatTextMessage } from './classes/livechat/items/LiveChatTextMessage.js'; export { default as LiveChatTickerPaidMessageItem } from './classes/livechat/items/LiveChatTickerPaidMessageItem.js'; export { default as LiveChatTickerPaidStickerItem } from './classes/livechat/items/LiveChatTickerPaidStickerItem.js'; From f563b9b654f64692fcb5a0b44c87222f16a4ad51 Mon Sep 17 00:00:00 2001 From: jonz94 Date: Sat, 2 Nov 2024 02:08:38 +0800 Subject: [PATCH 2/6] refactor: flatten nested field --- .../livechat/items/LiveChatSponsorshipsHeader.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts b/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts index de6aab2aa..8c2d4440c 100644 --- a/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts +++ b/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts @@ -15,11 +15,7 @@ export default class LiveChatSponsorshipsHeader extends YTNode { primary_text: Text; author_badges: ObservedArray | null; menu_endpoint: NavigationEndpoint; - context_menu_accessibility: { - accessibility_data: { - label: string - } - }; + context_menu_accessibility_label: string; image: Thumbnail[]; constructor(data: RawNode) { @@ -29,11 +25,7 @@ export default class LiveChatSponsorshipsHeader extends YTNode { this.primary_text = new Text(data.primaryText); this.author_badges = Parser.parse(data.authorBadges, true, LiveChatAuthorBadge); this.menu_endpoint = new NavigationEndpoint(data.contextMenuEndpoint); - this.context_menu_accessibility = { - accessibility_data: { - label: data.contextMenuAccessibility.accessibilityData.label - } - }; + this.context_menu_accessibility_label = data.contextMenuAccessibility.accessibilityData.label; this.image = Thumbnail.fromResponse(data.image); } } From af4442d898dabafbaf24f6c7978d686cb3b4d5b6 Mon Sep 17 00:00:00 2001 From: jonz94 Date: Sat, 2 Nov 2024 02:21:36 +0800 Subject: [PATCH 3/6] refactor: attempt to replace `author_*` fields with a single `Author` class --- .../LiveChatSponsorshipsGiftPurchaseAnnouncement.ts | 12 ++++++++++-- .../livechat/items/LiveChatSponsorshipsHeader.ts | 9 --------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts b/src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts index 39f51f90a..12dd1118e 100644 --- a/src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts +++ b/src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts @@ -1,5 +1,6 @@ import { YTNode } from '../../../helpers.js'; import { Parser, type RawNode } from '../../../index.js'; +import Author from '../../misc/Author.js'; import LiveChatSponsorshipsHeader from './LiveChatSponsorshipsHeader.js'; export default class LiveChatSponsorshipsGiftPurchaseAnnouncement extends YTNode { @@ -7,14 +8,21 @@ export default class LiveChatSponsorshipsGiftPurchaseAnnouncement extends YTNode id: string; timestamp_usec: string; - author_external_channel_id: string; + author: Author; header: LiveChatSponsorshipsHeader | null; constructor(data: RawNode) { super(); this.id = data.id; this.timestamp_usec = data.timestampUsec; - this.author_external_channel_id = data.authorExternalChannelId; + + this.author = new Author( + data.header.liveChatSponsorshipsHeaderRenderer.authorName, + data.header.liveChatSponsorshipsHeaderRenderer.authorBadges, + data.header.liveChatSponsorshipsHeaderRenderer.authorPhoto, + data.authorExternalChannelId + ); + this.header = Parser.parseItem(data.header, LiveChatSponsorshipsHeader); } } \ No newline at end of file diff --git a/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts b/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts index 8c2d4440c..5b3ddff5b 100644 --- a/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts +++ b/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts @@ -1,29 +1,20 @@ -import { Parser } from '../../../index.js'; import { YTNode } from '../../../helpers.js'; -import type { ObservedArray } from '../../../helpers.js'; import type { RawNode } from '../../../index.js'; import NavigationEndpoint from '../../NavigationEndpoint.js'; import Text from '../../misc/Text.js'; import Thumbnail from '../../misc/Thumbnail.js'; -import LiveChatAuthorBadge from '../../LiveChatAuthorBadge.js'; export default class LiveChatSponsorshipsHeader extends YTNode { static type = 'LiveChatSponsorshipsHeader'; - author_name: Text; - author_photo: Thumbnail[]; primary_text: Text; - author_badges: ObservedArray | null; menu_endpoint: NavigationEndpoint; context_menu_accessibility_label: string; image: Thumbnail[]; constructor(data: RawNode) { super(); - this.author_name = new Text(data.authorName); - this.author_photo = Thumbnail.fromResponse(data.authorPhoto); this.primary_text = new Text(data.primaryText); - this.author_badges = Parser.parse(data.authorBadges, true, LiveChatAuthorBadge); this.menu_endpoint = new NavigationEndpoint(data.contextMenuEndpoint); this.context_menu_accessibility_label = data.contextMenuAccessibility.accessibilityData.label; this.image = Thumbnail.fromResponse(data.image); From d31bdb3d8ab456cf044a93d92d05c0c9530c6c30 Mon Sep 17 00:00:00 2001 From: jonz94 Date: Sat, 2 Nov 2024 03:22:29 +0800 Subject: [PATCH 4/6] fix: add back `author_*` fields in `LiveChatSponsorshipsHeader` --- .../livechat/items/LiveChatSponsorshipsHeader.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts b/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts index 5b3ddff5b..0e638ca1d 100644 --- a/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts +++ b/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts @@ -1,12 +1,18 @@ +import { Parser } from '../../../index.js'; import { YTNode } from '../../../helpers.js'; +import type { ObservedArray } from '../../../helpers.js'; import type { RawNode } from '../../../index.js'; import NavigationEndpoint from '../../NavigationEndpoint.js'; import Text from '../../misc/Text.js'; import Thumbnail from '../../misc/Thumbnail.js'; +import LiveChatAuthorBadge from '../../LiveChatAuthorBadge.js'; export default class LiveChatSponsorshipsHeader extends YTNode { static type = 'LiveChatSponsorshipsHeader'; + author_name: Text; + author_photo: Thumbnail[]; + author_badges: ObservedArray | null; primary_text: Text; menu_endpoint: NavigationEndpoint; context_menu_accessibility_label: string; @@ -14,9 +20,12 @@ export default class LiveChatSponsorshipsHeader extends YTNode { constructor(data: RawNode) { super(); + this.author_name = new Text(data.authorName); + this.author_photo = Thumbnail.fromResponse(data.authorPhoto); + this.author_badges = Parser.parse(data.authorBadges, true, LiveChatAuthorBadge); this.primary_text = new Text(data.primaryText); this.menu_endpoint = new NavigationEndpoint(data.contextMenuEndpoint); this.context_menu_accessibility_label = data.contextMenuAccessibility.accessibilityData.label; this.image = Thumbnail.fromResponse(data.image); } -} +} \ No newline at end of file From b3b7777fbca4dc0c3efe8d7659daa2526b03de2b Mon Sep 17 00:00:00 2001 From: jonz94 Date: Fri, 1 Nov 2024 12:40:38 -0700 Subject: [PATCH 5/6] fix: use `parseArray` to parse author_badges data Co-authored-by: absidue <48293849+absidue@users.noreply.github.com> --- src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts b/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts index 0e638ca1d..7c1873573 100644 --- a/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts +++ b/src/parser/classes/livechat/items/LiveChatSponsorshipsHeader.ts @@ -22,7 +22,7 @@ export default class LiveChatSponsorshipsHeader extends YTNode { super(); this.author_name = new Text(data.authorName); this.author_photo = Thumbnail.fromResponse(data.authorPhoto); - this.author_badges = Parser.parse(data.authorBadges, true, LiveChatAuthorBadge); + this.author_badges = Parser.parseArray(data.authorBadges, LiveChatAuthorBadge); this.primary_text = new Text(data.primaryText); this.menu_endpoint = new NavigationEndpoint(data.contextMenuEndpoint); this.context_menu_accessibility_label = data.contextMenuAccessibility.accessibilityData.label; From 9d9011c5405298c2ca1f212677352a953dc1511f Mon Sep 17 00:00:00 2001 From: jonz94 Date: Sat, 2 Nov 2024 04:18:17 +0800 Subject: [PATCH 6/6] refactor: revert `Author` logic --- .../LiveChatSponsorshipsGiftPurchaseAnnouncement.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts b/src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts index 12dd1118e..39f51f90a 100644 --- a/src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts +++ b/src/parser/classes/livechat/items/LiveChatSponsorshipsGiftPurchaseAnnouncement.ts @@ -1,6 +1,5 @@ import { YTNode } from '../../../helpers.js'; import { Parser, type RawNode } from '../../../index.js'; -import Author from '../../misc/Author.js'; import LiveChatSponsorshipsHeader from './LiveChatSponsorshipsHeader.js'; export default class LiveChatSponsorshipsGiftPurchaseAnnouncement extends YTNode { @@ -8,21 +7,14 @@ export default class LiveChatSponsorshipsGiftPurchaseAnnouncement extends YTNode id: string; timestamp_usec: string; - author: Author; + author_external_channel_id: string; header: LiveChatSponsorshipsHeader | null; constructor(data: RawNode) { super(); this.id = data.id; this.timestamp_usec = data.timestampUsec; - - this.author = new Author( - data.header.liveChatSponsorshipsHeaderRenderer.authorName, - data.header.liveChatSponsorshipsHeaderRenderer.authorBadges, - data.header.liveChatSponsorshipsHeaderRenderer.authorPhoto, - data.authorExternalChannelId - ); - + this.author_external_channel_id = data.authorExternalChannelId; this.header = Parser.parseItem(data.header, LiveChatSponsorshipsHeader); } } \ No newline at end of file