1
1
import { config } from "../config" ;
2
2
import { Logger } from "../utils/logger" ;
3
3
import { db , privateDB } from "../databases/databases" ;
4
- import { getMaxResThumbnail , YouTubeAPI } from "../utils/youtubeApi" ;
4
+ import { getMaxResThumbnail } from "../utils/youtubeApi" ;
5
5
import { getSubmissionUUID } from "../utils/getSubmissionUUID" ;
6
6
import { getHash } from "../utils/getHash" ;
7
7
import { getHashCache } from "../utils/getHashCache" ;
@@ -13,7 +13,6 @@ import { ActionType, Category, IncomingSegment, IPAddress, SegmentUUID, Service,
13
13
import { deleteLockCategories } from "./deleteLockCategories" ;
14
14
import { QueryCacher } from "../utils/queryCacher" ;
15
15
import { getReputation } from "../utils/reputation" ;
16
- import { APIVideoData , APIVideoInfo } from "../types/youtubeApi.model" ;
17
16
import { HashedUserID , UserID } from "../types/user.model" ;
18
17
import { isUserVIP } from "../utils/isUserVIP" ;
19
18
import { isUserTempVIP } from "../utils/isUserTempVIP" ;
@@ -22,6 +21,7 @@ import { getService } from "../utils/getService";
22
21
import axios from "axios" ;
23
22
import { vote } from "./voteOnSponsorTime" ;
24
23
import { canSubmit } from "../utils/permissions" ;
24
+ import { getVideoDetails , videoDetails } from "../utils/getVideoDetails" ;
25
25
26
26
type CheckResult = {
27
27
pass : boolean ,
@@ -35,7 +35,7 @@ const CHECK_PASS: CheckResult = {
35
35
errorCode : 0
36
36
} ;
37
37
38
- async function sendWebhookNotification ( userID : string , videoID : string , UUID : string , submissionCount : number , youtubeData : APIVideoData , { submissionStart, submissionEnd } : { submissionStart : number ; submissionEnd : number ; } , segmentInfo : any ) {
38
+ async function sendWebhookNotification ( userID : string , videoID : string , UUID : string , submissionCount : number , youtubeData : videoDetails , { submissionStart, submissionEnd } : { submissionStart : number ; submissionEnd : number ; } , segmentInfo : any ) {
39
39
const row = await db . prepare ( "get" , `SELECT "userName" FROM "userNames" WHERE "userID" = ?` , [ userID ] ) ;
40
40
const userName = row !== undefined ? row . userName : null ;
41
41
@@ -48,7 +48,7 @@ async function sendWebhookNotification(userID: string, videoID: string, UUID: st
48
48
"video" : {
49
49
"id" : videoID ,
50
50
"title" : youtubeData ?. title ,
51
- "thumbnail" : getMaxResThumbnail ( youtubeData ) || null ,
51
+ "thumbnail" : getMaxResThumbnail ( videoID ) ,
52
52
"url" : `https://www.youtube.com/watch?v=${ videoID } ` ,
53
53
} ,
54
54
"submission" : {
@@ -64,16 +64,13 @@ async function sendWebhookNotification(userID: string, videoID: string, UUID: st
64
64
} ) ;
65
65
}
66
66
67
- async function sendWebhooks ( apiVideoInfo : APIVideoInfo , userID : string , videoID : string , UUID : string , segmentInfo : any , service : Service ) {
68
- if ( apiVideoInfo && service == Service . YouTube ) {
67
+ async function sendWebhooks ( apiVideoDetails : videoDetails , userID : string , videoID : string , UUID : string , segmentInfo : any , service : Service ) {
68
+ if ( apiVideoDetails && service == Service . YouTube ) {
69
69
const userSubmissionCountRow = await db . prepare ( "get" , `SELECT count(*) as "submissionCount" FROM "sponsorTimes" WHERE "userID" = ?` , [ userID ] ) ;
70
70
71
- const { data, err } = apiVideoInfo ;
72
- if ( err ) return ;
73
-
74
71
const startTime = parseFloat ( segmentInfo . segment [ 0 ] ) ;
75
72
const endTime = parseFloat ( segmentInfo . segment [ 1 ] ) ;
76
- sendWebhookNotification ( userID , videoID , UUID , userSubmissionCountRow . submissionCount , data , {
73
+ sendWebhookNotification ( userID , videoID , UUID , userSubmissionCountRow . submissionCount , apiVideoDetails , {
77
74
submissionStart : startTime ,
78
75
submissionEnd : endTime ,
79
76
} , segmentInfo ) . catch ( Logger . error ) ;
@@ -84,7 +81,7 @@ async function sendWebhooks(apiVideoInfo: APIVideoInfo, userID: string, videoID:
84
81
85
82
axios . post ( config . discordFirstTimeSubmissionsWebhookURL , {
86
83
embeds : [ {
87
- title : data ? .title ,
84
+ title : apiVideoDetails . title ,
88
85
url : `https://www.youtube.com/watch?v=${ videoID } &t=${ ( parseInt ( startTime . toFixed ( 0 ) ) - 2 ) } s#requiredSegment=${ UUID } ` ,
89
86
description : `Submission ID: ${ UUID } \
90
87
\n\nTimestamp: \
@@ -95,7 +92,7 @@ async function sendWebhooks(apiVideoInfo: APIVideoInfo, userID: string, videoID:
95
92
name : userID ,
96
93
} ,
97
94
thumbnail : {
98
- url : getMaxResThumbnail ( data ) || "" ,
95
+ url : getMaxResThumbnail ( videoID ) ,
99
96
} ,
100
97
} ] ,
101
98
} )
@@ -120,18 +117,10 @@ async function sendWebhooks(apiVideoInfo: APIVideoInfo, userID: string, videoID:
120
117
// Looks like this was broken for no defined youtube key - fixed but IMO we shouldn't return
121
118
// false for a pass - it was confusing and lead to this bug - any use of this function in
122
119
// the future could have the same problem.
123
- async function autoModerateSubmission ( apiVideoInfo : APIVideoInfo ,
120
+ async function autoModerateSubmission ( apiVideoDetails : videoDetails ,
124
121
submission : { videoID : VideoID ; userID : UserID ; segments : IncomingSegment [ ] , service : Service , videoDuration : number } ) {
125
-
126
- const apiVideoDuration = ( apiVideoInfo : APIVideoInfo ) => {
127
- if ( ! apiVideoInfo ) return undefined ;
128
- const { err, data } = apiVideoInfo ;
129
- // return undefined if API error
130
- if ( err ) return undefined ;
131
- return data ?. lengthSeconds ;
132
- } ;
133
122
// get duration from API
134
- const apiDuration = apiVideoDuration ( apiVideoInfo ) ;
123
+ const apiDuration = apiVideoDetails . duration ;
135
124
// if API fail or returns 0, get duration from client
136
125
const duration = apiDuration || submission . videoDuration ;
137
126
// return false on undefined or 0
@@ -165,14 +154,6 @@ async function autoModerateSubmission(apiVideoInfo: APIVideoInfo,
165
154
return false ;
166
155
}
167
156
168
- function getYouTubeVideoInfo ( videoID : VideoID , ignoreCache = false ) : Promise < APIVideoInfo > {
169
- if ( config . newLeafURLs !== null ) {
170
- return YouTubeAPI . listVideos ( videoID , ignoreCache ) ;
171
- } else {
172
- return null ;
173
- }
174
- }
175
-
176
157
async function checkUserActiveWarning ( userID : string ) : Promise < CheckResult > {
177
158
const MILLISECONDS_IN_HOUR = 3600000 ;
178
159
const now = Date . now ( ) ;
@@ -345,10 +326,10 @@ async function checkEachSegmentValid(rawIP: IPAddress, paramUserID: UserID, user
345
326
return CHECK_PASS ;
346
327
}
347
328
348
- async function checkByAutoModerator ( videoID : any , userID : any , segments : Array < any > , service :string , apiVideoInfo : APIVideoInfo , videoDuration : number ) : Promise < CheckResult > {
329
+ async function checkByAutoModerator ( videoID : any , userID : any , segments : Array < any > , service :string , apiVideoDetails : videoDetails , videoDuration : number ) : Promise < CheckResult > {
349
330
// Auto moderator check
350
331
if ( service == Service . YouTube ) {
351
- const autoModerateResult = await autoModerateSubmission ( apiVideoInfo , { userID, videoID, segments, service, videoDuration } ) ;
332
+ const autoModerateResult = await autoModerateSubmission ( apiVideoDetails , { userID, videoID, segments, service, videoDuration } ) ;
352
333
if ( autoModerateResult ) {
353
334
return {
354
335
pass : false ,
@@ -377,12 +358,13 @@ async function updateDataIfVideoDurationChange(videoID: VideoID, service: Servic
377
358
const videoDurationChanged = ( videoDuration : number ) => videoDuration != 0
378
359
&& previousSubmissions . length > 0 && ! previousSubmissions . some ( ( e ) => Math . abs ( videoDuration - e . videoDuration ) < 2 ) ;
379
360
380
- let apiVideoInfo : APIVideoInfo = null ;
361
+ let apiVideoDetails : videoDetails = null ;
381
362
if ( service == Service . YouTube ) {
382
363
// Don't use cache if we don't know the video duration, or the client claims that it has changed
383
- apiVideoInfo = await getYouTubeVideoInfo ( videoID , ! videoDurationParam || previousSubmissions . length === 0 || videoDurationChanged ( videoDurationParam ) ) ;
364
+ const ignoreCache = ! videoDurationParam || previousSubmissions . length === 0 || videoDurationChanged ( videoDurationParam ) ;
365
+ apiVideoDetails = await getVideoDetails ( videoID , ignoreCache ) ;
384
366
}
385
- const apiVideoDuration = apiVideoInfo ?. data ?. lengthSeconds as VideoDuration ;
367
+ const apiVideoDuration = apiVideoDetails ?. duration as VideoDuration ;
386
368
if ( ! videoDurationParam || ( apiVideoDuration && Math . abs ( videoDurationParam - apiVideoDuration ) > 2 ) ) {
387
369
// If api duration is far off, take that one instead (it is only precise to seconds, not millis)
388
370
videoDuration = apiVideoDuration || 0 as VideoDuration ;
@@ -400,7 +382,7 @@ async function updateDataIfVideoDurationChange(videoID: VideoID, service: Servic
400
382
401
383
return {
402
384
videoDuration,
403
- apiVideoInfo ,
385
+ apiVideoDetails ,
404
386
lockedCategoryList
405
387
} ;
406
388
}
@@ -501,10 +483,6 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
501
483
//hash the userID
502
484
const userID = await getHashCache ( paramUserID || "" ) ;
503
485
504
- if ( userID === "a41d853c7328a86f8d712f910c4ef77f6c7a9e467f349781b1a7d405c37b681b" ) {
505
- return res . status ( 200 ) ;
506
- }
507
-
508
486
const invalidCheckResult = await checkInvalidFields ( videoID , paramUserID , userID , segments , videoDurationParam , userAgent ) ;
509
487
if ( ! invalidCheckResult . pass ) {
510
488
return res . status ( invalidCheckResult . errorCode ) . send ( invalidCheckResult . errorMessage ) ;
@@ -521,7 +499,7 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
521
499
522
500
const newData = await updateDataIfVideoDurationChange ( videoID , service , videoDuration , videoDurationParam ) ;
523
501
videoDuration = newData . videoDuration ;
524
- const { lockedCategoryList, apiVideoInfo } = newData ;
502
+ const { lockedCategoryList, apiVideoDetails } = newData ;
525
503
526
504
// Check if all submissions are correct
527
505
const segmentCheckResult = await checkEachSegmentValid ( rawIP , paramUserID , userID , videoID , segments , service , isVIP , lockedCategoryList ) ;
@@ -530,7 +508,7 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
530
508
}
531
509
532
510
if ( ! isVIP ) {
533
- const autoModerateCheckResult = await checkByAutoModerator ( videoID , userID , segments , service , apiVideoInfo , videoDurationParam ) ;
511
+ const autoModerateCheckResult = await checkByAutoModerator ( videoID , userID , segments , service , apiVideoDetails , videoDurationParam ) ;
534
512
if ( ! autoModerateCheckResult . pass ) {
535
513
return res . status ( autoModerateCheckResult . errorCode ) . send ( autoModerateCheckResult . errorMessage ) ;
536
514
}
@@ -583,10 +561,10 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
583
561
//add to private db as well
584
562
await privateDB . prepare ( "run" , `INSERT INTO "sponsorTimes" VALUES(?, ?, ?, ?)` , [ videoID , hashedIP , timeSubmitted , service ] ) ;
585
563
586
- await db . prepare ( "run" , `INSERT INTO "videoInfo" ("videoID", "channelID", "title", "published", "genreUrl" )
587
- SELECT ?, ?, ?, ?, ?
564
+ await db . prepare ( "run" , `INSERT INTO "videoInfo" ("videoID", "channelID", "title", "published")
565
+ SELECT ?, ?, ?, ?
588
566
WHERE NOT EXISTS (SELECT 1 FROM "videoInfo" WHERE "videoID" = ?)` , [
589
- videoID , apiVideoInfo ?. data ?. authorId || "" , apiVideoInfo ?. data ?. title || "" , apiVideoInfo ?. data ?. published || 0 , apiVideoInfo ?. data ?. genreUrl || "" , videoID ] ) ;
567
+ videoID , apiVideoDetails ?. authorId || "" , apiVideoDetails ?. title || "" , apiVideoDetails ?. published || 0 , videoID ] ) ;
590
568
591
569
// Clear redis cache for this video
592
570
QueryCacher . clearSegmentCache ( {
@@ -614,7 +592,7 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
614
592
}
615
593
616
594
for ( let i = 0 ; i < segments . length ; i ++ ) {
617
- sendWebhooks ( apiVideoInfo , userID , videoID , UUIDs [ i ] , segments [ i ] , service ) . catch ( Logger . error ) ;
595
+ sendWebhooks ( apiVideoDetails , userID , videoID , UUIDs [ i ] , segments [ i ] , service ) . catch ( Logger . error ) ;
618
596
}
619
597
return res . json ( newSegments ) ;
620
598
}
0 commit comments