Skip to content

Commit 078d449

Browse files
committed
partial rewrite
1 parent 2ed23d7 commit 078d449

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
@@ -46,22 +46,25 @@ async function sendWebhookNotification(userID: string, videoID: string, UUID: st
4646
}
4747

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

@@ -71,14 +74,15 @@ async function sendWebhooks(apiVideoDetails: videoDetails, userID: string, video
7174

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

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

8387
axios.post(config.discordFirstTimeSubmissionsWebhookURL, {
8488
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";
@@ -35,7 +35,7 @@ interface FinalResponse {
3535
}
3636

3737
interface VoteData {
38-
UUID: string;
38+
UUID: SegmentUUID;
3939
nonAnonUserID: string;
4040
originalType: VoteType;
4141
voteTypeEnum: number;
@@ -47,7 +47,7 @@ interface VoteData {
4747
views: number;
4848
locked: boolean;
4949
};
50-
category: string;
50+
category: Category;
5151
incrementAmount: number;
5252
oldIncrementAmount: number;
5353
finalResponse: FinalResponse;
@@ -101,7 +101,7 @@ async function checkVideoDuration(UUID: SegmentUUID) {
101101
}
102102

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

134134
const isUpvote = voteData.incrementAmount > 0;
135135
// Send custom webhooks
136-
dispatchEvent(isUpvote ? "vote.up" : "vote.down", {
137-
"user": {
138-
"status": getVoteAuthorRaw(userSubmissionCountRow.submissionCount, voteData.isTempVIP, voteData.isVIP, voteData.isOwnSubmission),
136+
const webhookData: WebhookData = {
137+
user: {
138+
status: getVoteAuthorRaw(userSubmissionCountRow.submissionCount, voteData.isTempVIP, voteData.isVIP, voteData.isOwnSubmission),
139139
},
140-
"video": {
141-
"id": submissionInfoRow.videoID,
142-
"title": data?.title,
143-
"url": `https://www.youtube.com/watch?v=${videoID}`,
144-
"thumbnail": getMaxResThumbnail(videoID),
140+
video: {
141+
id: submissionInfoRow.videoID,
142+
title: data?.title,
143+
url: `https://www.youtube.com/watch?v=${videoID}`,
144+
thumbnail: getMaxResThumbnail(videoID),
145145
},
146-
"submission": {
147-
"UUID": voteData.UUID,
148-
"views": voteData.row.views,
149-
"category": voteData.category,
150-
"startTime": submissionInfoRow.startTime,
151-
"endTime": submissionInfoRow.endTime,
152-
"user": {
153-
"UUID": submissionInfoRow.userID,
154-
"username": submissionInfoRow.userName,
155-
"submissions": {
156-
"total": submissionInfoRow.count,
157-
"ignored": submissionInfoRow.disregarded,
146+
submission: {
147+
UUID: voteData.UUID as SegmentUUID,
148+
views: voteData.row.views,
149+
locked: voteData.row.locked,
150+
category: voteData.category as Category,
151+
startTime: submissionInfoRow.startTime,
152+
endTime: submissionInfoRow.endTime,
153+
user: {
154+
UUID: submissionInfoRow.userID,
155+
username: submissionInfoRow.userName,
156+
submissions: {
157+
total: submissionInfoRow.count,
158+
ignored: submissionInfoRow.disregarded,
158159
},
159160
},
160161
},
161-
"votes": {
162-
"before": voteData.row.votes,
163-
"after": (voteData.row.votes + voteData.incrementAmount - voteData.oldIncrementAmount),
162+
votes: {
163+
before: voteData.row.votes,
164+
after: (voteData.row.votes + voteData.incrementAmount - voteData.oldIncrementAmount),
164165
},
165-
});
166+
authorName: voteData.finalResponse?.webhookMessage ?? voteData.finalResponse?.finalMessage
167+
};
168+
dispatchEvent(isUpvote ? "vote.up" : "vote.down", webhookData);
166169

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