Skip to content

Commit 5e03fc6

Browse files
Update addBannerToLiveChatCommand from stu43005/masterchat
- Ver: 1.5.0 - SHA: 5e30cf7
1 parent e873c9d commit 5e03fc6

File tree

9 files changed

+635
-286
lines changed

9 files changed

+635
-286
lines changed
Lines changed: 130 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import { toUnknownAction } from "..";
12
import {
23
AddBannerAction,
3-
AddRedirectBannerAction,
4+
AddIncomingRaidBannerAction,
5+
AddOutgoingRaidBannerAction,
6+
AddProductBannerAction,
7+
AddChatSummaryBannerAction,
8+
AddCallForQuestionsBannerAction,
49
} from "../../interfaces/actions";
510
import { YTAddBannerToLiveChatCommand } from "../../interfaces/yt/chat";
6-
import { debugLog, stringify, tsToDate } from "../../utils";
11+
import { debugLog, endpointToUrl, stringify, tsToDate } from "../../utils";
712
import { parseBadges } from "../badge";
813
import { pickThumbUrl } from "../utils";
914

@@ -13,20 +18,12 @@ export function parseAddBannerToLiveChatCommand(
1318
// add pinned item
1419
const bannerRdr = payload["bannerRenderer"]["liveChatBannerRenderer"];
1520

16-
if (
17-
bannerRdr.header &&
18-
bannerRdr.header.liveChatBannerHeaderRenderer.icon.iconType !== "KEEP"
19-
) {
20-
debugLog(
21-
"[action required] Unknown icon type (addBannerToLiveChatCommand)",
22-
JSON.stringify(bannerRdr.header)
23-
);
24-
}
25-
2621
// banner
2722
const actionId = bannerRdr.actionId;
2823
const targetId = bannerRdr.targetId;
2924
const viewerIsCreator = bannerRdr.viewerIsCreator;
25+
const isStackable = bannerRdr.isStackable;
26+
const bannerType = bannerRdr.bannerType;
3027

3128
// contents
3229
const contents = bannerRdr.contents;
@@ -40,7 +37,7 @@ export function parseAddBannerToLiveChatCommand(
4037
const authorName = stringify(rdr.authorName);
4138
const authorPhoto = pickThumbUrl(rdr.authorPhoto);
4239
const authorChannelId = rdr.authorExternalChannelId;
43-
const { isVerified, isOwner, isModerator, membership } = parseBadges(rdr);
40+
const badges = parseBadges(rdr);
4441

4542
// header
4643
const header = bannerRdr.header!.liveChatBannerHeaderRenderer;
@@ -65,33 +62,137 @@ export function parseAddBannerToLiveChatCommand(
6562
authorName,
6663
authorPhoto,
6764
authorChannelId,
68-
isVerified,
69-
isOwner,
70-
isModerator,
71-
membership,
65+
...badges,
7266
viewerIsCreator,
7367
contextMenuEndpointParams:
7468
rdr.contextMenuEndpoint?.liveChatItemContextMenuEndpoint.params,
7569
};
7670
return parsed;
77-
} else if ("liveChatBannerRedirectRenderer" in contents) {
78-
// TODO:
71+
}
72+
73+
if ("liveChatBannerRedirectRenderer" in contents) {
7974
const rdr = contents.liveChatBannerRedirectRenderer;
80-
const authorName = rdr.bannerMessage.runs[0].text;
75+
const targetVideoId =
76+
"watchEndpoint" in rdr.inlineActionButton.buttonRenderer.command
77+
? rdr.inlineActionButton.buttonRenderer.command.watchEndpoint.videoId
78+
: undefined;
79+
80+
const photo = pickThumbUrl(rdr.authorPhoto);
81+
82+
if (targetVideoId) {
83+
// Outgoing
84+
const targetName = rdr.bannerMessage.runs[1].text;
85+
const payload: AddOutgoingRaidBannerAction = {
86+
type: "addOutgoingRaidBannerAction",
87+
actionId,
88+
targetId,
89+
targetName,
90+
targetPhoto: photo,
91+
targetVideoId,
92+
};
93+
return payload;
94+
} else {
95+
// Incoming
96+
const sourceName = rdr.bannerMessage.runs[0].text;
97+
const payload: AddIncomingRaidBannerAction = {
98+
type: "addIncomingRaidBannerAction",
99+
actionId,
100+
targetId,
101+
sourceName,
102+
sourcePhoto: photo,
103+
};
104+
return payload;
105+
}
106+
}
107+
108+
if ("liveChatProductItemRenderer" in contents) {
109+
const rdr = contents.liveChatProductItemRenderer;
110+
const title = rdr.title;
111+
const description = rdr.accessibilityTitle;
112+
const thumbnail = rdr.thumbnail.thumbnails[0].url;
113+
const price = rdr.price;
114+
const vendorName = rdr.vendorName;
115+
const creatorMessage = rdr.creatorMessage;
116+
const creatorName = rdr.creatorName;
81117
const authorPhoto = pickThumbUrl(rdr.authorPhoto);
82-
const payload: AddRedirectBannerAction = {
83-
type: "addRedirectBannerAction",
118+
const url = endpointToUrl(rdr.onClickCommand)!;
119+
120+
if (!url) {
121+
debugLog(
122+
`Empty url at liveChatProductItemRenderer: ${JSON.stringify(rdr)}`
123+
);
124+
}
125+
126+
const dialogMessage =
127+
rdr.informationDialog.liveChatDialogRenderer.dialogMessages;
128+
const isVerified = rdr.isVerified;
129+
130+
const payload: AddProductBannerAction = {
131+
type: "addProductBannerAction",
84132
actionId,
85133
targetId,
86-
authorName,
134+
viewerIsCreator,
135+
isStackable,
136+
title,
137+
description,
138+
thumbnail,
139+
price,
140+
vendorName,
141+
creatorMessage,
142+
creatorName,
87143
authorPhoto,
144+
url,
145+
dialogMessage,
146+
isVerified,
88147
};
89148
return payload;
90-
} else {
91-
throw new Error(
92-
`[action required] Unrecognized content type found in parseAddBannerToLiveChatCommand: ${JSON.stringify(
93-
payload
94-
)}`
95-
);
96149
}
150+
151+
if ("liveChatCallForQuestionsRenderer" in contents) {
152+
const rdr = contents.liveChatCallForQuestionsRenderer;
153+
const creatorAvatar = rdr.creatorAvatar.thumbnails[0].url;
154+
const creatorAuthorName = stringify(rdr.creatorAuthorName);
155+
const questionMessage = rdr.questionMessage.runs;
156+
157+
const parsed: AddCallForQuestionsBannerAction = {
158+
type: "addCallForQuestionsBannerAction",
159+
actionId,
160+
targetId,
161+
isStackable,
162+
bannerType,
163+
creatorAvatar,
164+
creatorAuthorName,
165+
questionMessage,
166+
};
167+
return parsed;
168+
}
169+
170+
if ("liveChatBannerChatSummaryRenderer" in contents) {
171+
const rdr = contents.liveChatBannerChatSummaryRenderer;
172+
const id = rdr.liveChatSummaryId;
173+
const timestampUsec = id.split("_").at(-1)!;
174+
const timestamp = tsToDate(timestampUsec);
175+
const chatSummary = rdr.chatSummary.runs;
176+
177+
const parsed: AddChatSummaryBannerAction = {
178+
type: "addChatSummaryBannerAction",
179+
id,
180+
actionId,
181+
targetId,
182+
isStackable,
183+
bannerType,
184+
timestampUsec,
185+
timestamp,
186+
chatSummary,
187+
};
188+
return parsed;
189+
}
190+
191+
debugLog(
192+
`[action required] Unrecognized content type found in parseAddBannerToLiveChatCommand: ${JSON.stringify(
193+
payload
194+
)}`
195+
);
196+
197+
return toUnknownAction(payload);
97198
}

0 commit comments

Comments
 (0)