Skip to content

Commit a4a42e3

Browse files
committed
partial rewrite
1 parent 95dd36a commit a4a42e3

File tree

5 files changed

+133
-69
lines changed

5 files changed

+133
-69
lines changed

src/routes/postSkipSegments.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,25 @@ async function sendWebhookNotification(userID: string, videoID: string, UUID: st
4545
}
4646

4747
dispatchEvent(scopeName, {
48-
"video": {
49-
"id": videoID,
50-
"title": youtubeData?.title,
51-
"thumbnail": getMaxResThumbnail(videoID),
52-
"url": `https://www.youtube.com/watch?v=${videoID}`,
48+
user: {
49+
status: "new"
5350
},
54-
"submission": {
55-
"UUID": UUID,
56-
"category": segmentInfo.category,
57-
"startTime": submissionStart,
58-
"endTime": submissionEnd,
59-
"user": {
60-
"UUID": userID,
61-
"username": userName,
62-
},
51+
video: {
52+
id: (videoID as VideoID),
53+
title: youtubeData?.title,
54+
url: `https://www.youtube.com/watch?v=${videoID}`,
55+
thumbnail: getMaxResThumbnail(videoID),
6356
},
57+
submission: {
58+
UUID: UUID as SegmentUUID,
59+
category: segmentInfo.category,
60+
startTime: submissionStart,
61+
endTime: submissionEnd,
62+
user: {
63+
UUID: userID as HashedUserID,
64+
username: userName,
65+
},
66+
}
6467
});
6568
}
6669

@@ -70,14 +73,15 @@ async function sendWebhooks(apiVideoDetails: videoDetails, userID: string, video
7073

7174
const startTime = parseFloat(segmentInfo.segment[0]);
7275
const endTime = parseFloat(segmentInfo.segment[1]);
76+
const webhookData =
7377
sendWebhookNotification(userID, videoID, UUID, userSubmissionCountRow.submissionCount, apiVideoDetails, {
7478
submissionStart: startTime,
7579
submissionEnd: endTime,
7680
}, segmentInfo).catch(Logger.error);
7781

7882
// If it is a first time submission
7983
// Then send a notification to discord
80-
if (config.discordFirstTimeSubmissionsWebhookURL === null || userSubmissionCountRow.submissionCount > 1) return;
84+
if (!config.discordFirstTimeSubmissionsWebhookURL || userSubmissionCountRow.submissionCount > 1) return;
8185

8286
axios.post(config.discordFirstTimeSubmissionsWebhookURL, {
8387
embeds: [{

src/routes/voteOnSponsorTime.ts

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { isUserVIP } from "../utils/isUserVIP";
44
import { isUserTempVIP } from "../utils/isUserTempVIP";
55
import { getMaxResThumbnail, YouTubeAPI } from "../utils/youtubeApi";
66
import { db, privateDB } from "../databases/databases";
7-
import { dispatchEvent, getVoteAuthor, getVoteAuthorRaw } from "../utils/webhookUtils";
8-
import { getFormattedTime } from "../utils/getFormattedTime";
7+
import { dispatchEvent, getVoteAuthorRaw, createDiscordVoteEmbed } from "../utils/webhookUtils";
8+
import { WebhookData } from "../types/webhook.model";
99
import { getIP } from "../utils/getIP";
1010
import { getHashCache } from "../utils/getHashCache";
1111
import { config } from "../config";
@@ -34,7 +34,7 @@ interface FinalResponse {
3434
}
3535

3636
interface VoteData {
37-
UUID: string;
37+
UUID: SegmentUUID;
3838
nonAnonUserID: string;
3939
originalType: VoteType;
4040
voteTypeEnum: number;
@@ -46,7 +46,7 @@ interface VoteData {
4646
views: number;
4747
locked: boolean;
4848
};
49-
category: string;
49+
category: Category;
5050
incrementAmount: number;
5151
oldIncrementAmount: number;
5252
finalResponse: FinalResponse;
@@ -99,7 +99,7 @@ async function checkVideoDuration(UUID: SegmentUUID) {
9999
}
100100

101101
async function sendWebhooks(voteData: VoteData) {
102-
const submissionInfoRow = await db.prepare("get", `SELECT "s"."videoID", "s"."userID", s."startTime", s."endTime", s."category", u."userName",
102+
const submissionInfoRow = await db.prepare("get", `SELECT "s"."videoID", "s"."userID", s."startTime", s."endTime", s."category", u."userName",
103103
(select count(1) from "sponsorTimes" where "userID" = s."userID") count,
104104
(select count(1) from "sponsorTimes" where "userID" = s."userID" and votes <= -2) disregarded
105105
FROM "sponsorTimes" s left join "userNames" u on s."userID" = u."userID" where s."UUID"=?`,
@@ -131,62 +131,44 @@ async function sendWebhooks(voteData: VoteData) {
131131

132132
const isUpvote = voteData.incrementAmount > 0;
133133
// Send custom webhooks
134-
dispatchEvent(isUpvote ? "vote.up" : "vote.down", {
135-
"user": {
136-
"status": getVoteAuthorRaw(userSubmissionCountRow.submissionCount, voteData.isTempVIP, voteData.isVIP, voteData.isOwnSubmission),
134+
const webhookData: WebhookData = {
135+
user: {
136+
status: getVoteAuthorRaw(userSubmissionCountRow.submissionCount, voteData.isTempVIP, voteData.isVIP, voteData.isOwnSubmission),
137137
},
138-
"video": {
139-
"id": submissionInfoRow.videoID,
140-
"title": data?.title,
141-
"url": `https://www.youtube.com/watch?v=${videoID}`,
142-
"thumbnail": getMaxResThumbnail(videoID),
138+
video: {
139+
id: submissionInfoRow.videoID,
140+
title: data?.title,
141+
url: `https://www.youtube.com/watch?v=${videoID}`,
142+
thumbnail: getMaxResThumbnail(videoID),
143143
},
144-
"submission": {
145-
"UUID": voteData.UUID,
146-
"views": voteData.row.views,
147-
"category": voteData.category,
148-
"startTime": submissionInfoRow.startTime,
149-
"endTime": submissionInfoRow.endTime,
150-
"user": {
151-
"UUID": submissionInfoRow.userID,
152-
"username": submissionInfoRow.userName,
153-
"submissions": {
154-
"total": submissionInfoRow.count,
155-
"ignored": submissionInfoRow.disregarded,
144+
submission: {
145+
UUID: voteData.UUID as SegmentUUID,
146+
views: voteData.row.views,
147+
locked: voteData.row.locked,
148+
category: voteData.category as Category,
149+
startTime: submissionInfoRow.startTime,
150+
endTime: submissionInfoRow.endTime,
151+
user: {
152+
UUID: submissionInfoRow.userID,
153+
username: submissionInfoRow.userName,
154+
submissions: {
155+
total: submissionInfoRow.count,
156+
ignored: submissionInfoRow.disregarded,
156157
},
157158
},
158159
},
159-
"votes": {
160-
"before": voteData.row.votes,
161-
"after": (voteData.row.votes + voteData.incrementAmount - voteData.oldIncrementAmount),
160+
votes: {
161+
before: voteData.row.votes,
162+
after: (voteData.row.votes + voteData.incrementAmount - voteData.oldIncrementAmount),
162163
},
163-
});
164+
authorName: voteData.finalResponse?.webhookMessage ?? voteData.finalResponse?.finalMessage
165+
};
166+
dispatchEvent(isUpvote ? "vote.up" : "vote.down", webhookData);
164167

165168
// Send discord message
166169
if (webhookURL !== null && !isUpvote) {
167170
axios.post(webhookURL, {
168-
"embeds": [{
169-
"title": data?.title,
170-
"url": `https://www.youtube.com/watch?v=${submissionInfoRow.videoID}&t=${(submissionInfoRow.startTime.toFixed(0) - 2)}s#requiredSegment=${voteData.UUID}`,
171-
"description": `**${voteData.row.votes} Votes Prior | \
172-
${(voteData.row.votes + voteData.incrementAmount - voteData.oldIncrementAmount)} Votes Now | ${voteData.row.views} \
173-
Views**\n\n**Locked**: ${voteData.row.locked}\n\n**Submission ID:** ${voteData.UUID}\
174-
\n**Category:** ${submissionInfoRow.category}\
175-
\n\n**Submitted by:** ${submissionInfoRow.userName}\n${submissionInfoRow.userID}\
176-
\n\n**Total User Submissions:** ${submissionInfoRow.count}\
177-
\n**Ignored User Submissions:** ${submissionInfoRow.disregarded}\
178-
\n\n**Timestamp:** \
179-
${getFormattedTime(submissionInfoRow.startTime)} to ${getFormattedTime(submissionInfoRow.endTime)}`,
180-
"color": 10813440,
181-
"author": {
182-
"name": voteData.finalResponse?.webhookMessage ??
183-
voteData.finalResponse?.finalMessage ??
184-
`${getVoteAuthor(userSubmissionCountRow.submissionCount, voteData.isTempVIP, voteData.isVIP, voteData.isOwnSubmission)}${voteData.row.locked ? " (Locked)" : ""}`,
185-
},
186-
"thumbnail": {
187-
"url": getMaxResThumbnail(videoID),
188-
},
189-
}],
171+
"embeds": [createDiscordVoteEmbed(webhookData)],
190172
})
191173
.then(res => {
192174
if (res.status >= 400) {

src/types/webhook.model.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as segments from "./segments.model";
2+
import { HashedUserID } from "./user.model";
3+
4+
export enum voteType {
5+
"up" = "vote.up",
6+
"down" = "vote.down",
7+
}
8+
9+
export type authorType = "self" | "temp vip" | "vip" | "new" | "other";
10+
11+
export interface WebhookData {
12+
user: {
13+
status: authorType
14+
}
15+
video: {
16+
id: segments.VideoID
17+
title: string | undefined,
18+
url: URL | string,
19+
thumbnail: URL | string,
20+
},
21+
submission: {
22+
UUID: segments.SegmentUUID,
23+
views?: number,
24+
locked?: boolean,
25+
category: segments.Category,
26+
startTime: number,
27+
endTime: number,
28+
user: {
29+
UUID: HashedUserID,
30+
username: string | HashedUserID,
31+
submissions?: {
32+
total: number,
33+
ignored: number,
34+
},
35+
},
36+
},
37+
votes?: {
38+
before: number,
39+
after: number
40+
}
41+
authorName?: string
42+
}

src/utils/webhookUtils.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { config } from "../config";
22
import { Logger } from "../utils/logger";
3+
import { authorType, WebhookData } from "../types/webhook.model";
4+
import { getFormattedTime } from "../utils/getFormattedTime";
35
import axios from "axios";
46

5-
function getVoteAuthorRaw(submissionCount: number, isTempVIP: boolean, isVIP: boolean, isOwnSubmission: boolean): string {
7+
function getVoteAuthorRaw(submissionCount: number, isTempVIP: boolean, isVIP: boolean, isOwnSubmission: boolean): authorType {
68
if (isOwnSubmission) {
79
return "self";
810
} else if (isTempVIP) {
@@ -30,7 +32,40 @@ function getVoteAuthor(submissionCount: number, isTempVIP: boolean, isVIP: boole
3032
return "";
3133
}
3234

33-
function dispatchEvent(scope: string, data: Record<string, unknown>): void {
35+
const voteAuthorMap: Record<authorType, string> = {
36+
"self": "Report by Submitter",
37+
"temp vip": "Report by Temp VIP",
38+
"vip": "Report by VIP User",
39+
"new": "Report by New User",
40+
"other": ""
41+
};
42+
43+
const createDiscordVoteEmbed = (data: WebhookData) => {
44+
const startTime = Math.max(0, data.submission.startTime - 2);
45+
const startTimeParam = startTime > 0 ? `&t=${startTime}s` : "";
46+
return {
47+
title: data.video.title,
48+
url: `https://www.youtube.com/watch?v=${data.video.id}${startTimeParam}#requiredSegment=${data.submission.UUID}`,
49+
description: `**${data.votes.before} Votes Prior | \
50+
${(data.votes.after)} Votes Now | ${data.submission.views} \
51+
Views**\n\n**Locked**: ${data.submission.locked}\n\n**Submission ID:** ${data.submission.UUID}\
52+
\n**Category:** ${data.submission.category}\
53+
\n\n**Submitted by:** ${data.submission.user.username}\n${data.submission.user.UUID}\
54+
\n\n**Total User Submissions:** ${data.submission.user.submissions.total}\
55+
\n**Ignored User Submissions:** ${data.submission.user.submissions.ignored}\
56+
\n\n**Timestamp:** \
57+
${getFormattedTime(data.submission.startTime)} to ${getFormattedTime(data.submission.endTime)}`,
58+
color: 10813440,
59+
author: {
60+
name: data.authorName ?? `${voteAuthorMap[data.user.status]}${data.submission.locked ? " (Locked)" : ""}`,
61+
},
62+
thumbnail: {
63+
url: data.video.thumbnail,
64+
},
65+
};
66+
};
67+
68+
function dispatchEvent(scope: string, data: WebhookData): void {
3469
const webhooks = config.webhooks;
3570
if (webhooks === undefined || webhooks.length === 0) return;
3671
Logger.debug("Dispatching webhooks");
@@ -61,4 +96,5 @@ export {
6196
getVoteAuthorRaw,
6297
getVoteAuthor,
6398
dispatchEvent,
64-
};
99+
createDiscordVoteEmbed
100+
};

test/cases/webhoook.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)