aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-11 14:09:33 +0200
committerChocobozzz <me@florianbigard.com>2021-06-11 14:09:52 +0200
commit71d4af1efc810f853e1a0d986bf758c201692594 (patch)
tree2066053638baefb6430772c2e0a0aa1774019a51 /server/models/video/video.ts
parent3c79c2ce86eaf9e151ab6c2c9d1f646968a16744 (diff)
downloadPeerTube-71d4af1efc810f853e1a0d986bf758c201692594.tar.gz
PeerTube-71d4af1efc810f853e1a0d986bf758c201692594.tar.zst
PeerTube-71d4af1efc810f853e1a0d986bf758c201692594.zip
Use raw SQL for most of video queries
Diffstat (limited to 'server/models/video/video.ts')
-rw-r--r--server/models/video/video.ts150
1 files changed, 30 insertions, 120 deletions
diff --git a/server/models/video/video.ts b/server/models/video/video.ts
index 00fbb18f6..2d8b7b653 100644
--- a/server/models/video/video.ts
+++ b/server/models/video/video.ts
@@ -88,13 +88,12 @@ import {
88 MVideoFormattableDetails, 88 MVideoFormattableDetails,
89 MVideoForUser, 89 MVideoForUser,
90 MVideoFullLight, 90 MVideoFullLight,
91 MVideoIdThumbnail, 91 MVideoId,
92 MVideoImmutable, 92 MVideoImmutable,
93 MVideoThumbnail, 93 MVideoThumbnail,
94 MVideoThumbnailBlacklist, 94 MVideoThumbnailBlacklist,
95 MVideoWithAllFiles, 95 MVideoWithAllFiles,
96 MVideoWithFile, 96 MVideoWithFile
97 MVideoWithRights
98} from '../../types/models' 97} from '../../types/models'
99import { MThumbnail } from '../../types/models/video/thumbnail' 98import { MThumbnail } from '../../types/models/video/thumbnail'
100import { MVideoFile, MVideoFileStreamingPlaylistVideo } from '../../types/models/video/video-file' 99import { MVideoFile, MVideoFileStreamingPlaylistVideo } from '../../types/models/video/video-file'
@@ -1301,27 +1300,16 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> {
1301 return VideoModel.count(options) 1300 return VideoModel.count(options)
1302 } 1301 }
1303 1302
1304 static load (id: number | string, t?: Transaction): Promise<MVideoThumbnail> { 1303 static load (id: number | string, transaction?: Transaction): Promise<MVideoThumbnail> {
1305 const where = buildWhereIdOrUUID(id) 1304 const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize)
1306 const options = {
1307 where,
1308 transaction: t
1309 }
1310 1305
1311 return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) 1306 return queryBuilder.queryVideo({ id, transaction, type: 'thumbnails' })
1312 } 1307 }
1313 1308
1314 static loadWithBlacklist (id: number | string, t?: Transaction): Promise<MVideoThumbnailBlacklist> { 1309 static loadWithBlacklist (id: number | string, transaction?: Transaction): Promise<MVideoThumbnailBlacklist> {
1315 const where = buildWhereIdOrUUID(id) 1310 const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize)
1316 const options = {
1317 where,
1318 transaction: t
1319 }
1320 1311
1321 return VideoModel.scope([ 1312 return queryBuilder.queryVideo({ id, transaction, type: 'thumbnails-blacklist' })
1322 ScopeNames.WITH_THUMBNAILS,
1323 ScopeNames.WITH_BLACKLISTED
1324 ]).findOne(options)
1325 } 1313 }
1326 1314
1327 static loadImmutableAttributes (id: number | string, t?: Transaction): Promise<MVideoImmutable> { 1315 static loadImmutableAttributes (id: number | string, t?: Transaction): Promise<MVideoImmutable> {
@@ -1342,68 +1330,6 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> {
1342 }) 1330 })
1343 } 1331 }
1344 1332
1345 static loadWithRights (id: number | string, t?: Transaction): Promise<MVideoWithRights> {
1346 const where = buildWhereIdOrUUID(id)
1347 const options = {
1348 where,
1349 transaction: t
1350 }
1351
1352 return VideoModel.scope([
1353 ScopeNames.WITH_BLACKLISTED,
1354 ScopeNames.WITH_USER_ID
1355 ]).findOne(options)
1356 }
1357
1358 static loadOnlyId (id: number | string, t?: Transaction): Promise<MVideoIdThumbnail> {
1359 const where = buildWhereIdOrUUID(id)
1360
1361 const options = {
1362 attributes: [ 'id' ],
1363 where,
1364 transaction: t
1365 }
1366
1367 return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options)
1368 }
1369
1370 static loadWithFiles (id: number | string, t?: Transaction, logging?: boolean): Promise<MVideoWithAllFiles> {
1371 const where = buildWhereIdOrUUID(id)
1372
1373 const query = {
1374 where,
1375 transaction: t,
1376 logging
1377 }
1378
1379 return VideoModel.scope([
1380 ScopeNames.WITH_WEBTORRENT_FILES,
1381 ScopeNames.WITH_STREAMING_PLAYLISTS,
1382 ScopeNames.WITH_THUMBNAILS
1383 ]).findOne(query)
1384 }
1385
1386 static loadByUUID (uuid: string): Promise<MVideoThumbnail> {
1387 const options = {
1388 where: {
1389 uuid
1390 }
1391 }
1392
1393 return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options)
1394 }
1395
1396 static loadByUrl (url: string, transaction?: Transaction): Promise<MVideoThumbnail> {
1397 const query: FindOptions = {
1398 where: {
1399 url
1400 },
1401 transaction
1402 }
1403
1404 return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(query)
1405 }
1406
1407 static loadByUrlImmutableAttributes (url: string, transaction?: Transaction): Promise<MVideoImmutable> { 1333 static loadByUrlImmutableAttributes (url: string, transaction?: Transaction): Promise<MVideoImmutable> {
1408 const fun = () => { 1334 const fun = () => {
1409 const query: FindOptions = { 1335 const query: FindOptions = {
@@ -1424,50 +1350,34 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> {
1424 }) 1350 })
1425 } 1351 }
1426 1352
1427 static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction): Promise<MVideoAccountLightBlacklistAllFiles> { 1353 static loadOnlyId (id: number | string, transaction?: Transaction): Promise<MVideoId> {
1428 const query: FindOptions = { 1354 const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize)
1429 where: {
1430 url
1431 },
1432 transaction
1433 }
1434 1355
1435 return VideoModel.scope([ 1356 return queryBuilder.queryVideo({ id, transaction, type: 'id' })
1436 ScopeNames.WITH_ACCOUNT_DETAILS,
1437 ScopeNames.WITH_WEBTORRENT_FILES,
1438 ScopeNames.WITH_STREAMING_PLAYLISTS,
1439 ScopeNames.WITH_THUMBNAILS,
1440 ScopeNames.WITH_BLACKLISTED
1441 ]).findOne(query)
1442 } 1357 }
1443 1358
1444 static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number): Promise<MVideoFullLight> { 1359 static loadWithFiles (id: number | string, transaction?: Transaction, logging?: boolean): Promise<MVideoWithAllFiles> {
1445 const where = buildWhereIdOrUUID(id) 1360 const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize)
1446 1361
1447 const options = { 1362 return queryBuilder.queryVideo({ id, transaction, type: 'all-files', logging })
1448 order: [ [ 'Tags', 'name', 'ASC' ] ] as any, 1363 }
1449 where,
1450 transaction: t
1451 }
1452 1364
1453 const scopes: (string | ScopeOptions)[] = [ 1365 static loadByUrl (url: string, transaction?: Transaction): Promise<MVideoThumbnail> {
1454 ScopeNames.WITH_TAGS, 1366 const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize)
1455 ScopeNames.WITH_BLACKLISTED,
1456 ScopeNames.WITH_ACCOUNT_DETAILS,
1457 ScopeNames.WITH_SCHEDULED_UPDATE,
1458 ScopeNames.WITH_WEBTORRENT_FILES,
1459 ScopeNames.WITH_STREAMING_PLAYLISTS,
1460 ScopeNames.WITH_THUMBNAILS,
1461 ScopeNames.WITH_LIVE
1462 ]
1463 1367
1464 if (userId) { 1368 return queryBuilder.queryVideo({ url, transaction, type: 'thumbnails' })
1465 scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] }) 1369 }
1466 } 1370
1371 static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction): Promise<MVideoAccountLightBlacklistAllFiles> {
1372 const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize)
1373
1374 return queryBuilder.queryVideo({ url, transaction, type: 'account-blacklist-files' })
1375 }
1376
1377 static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number): Promise<MVideoFullLight> {
1378 const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize)
1467 1379
1468 return VideoModel 1380 return queryBuilder.queryVideo({ id, transaction: t, type: 'full-light', userId })
1469 .scope(scopes)
1470 .findOne(options)
1471 } 1381 }
1472 1382
1473 static loadForGetAPI (parameters: { 1383 static loadForGetAPI (parameters: {
@@ -1478,7 +1388,7 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> {
1478 const { id, transaction, userId } = parameters 1388 const { id, transaction, userId } = parameters
1479 const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize) 1389 const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize)
1480 1390
1481 return queryBuilder.queryVideos({ id, transaction, forGetAPI: true, userId }) 1391 return queryBuilder.queryVideo({ id, transaction, type: 'api', userId })
1482 } 1392 }
1483 1393
1484 static async getStats () { 1394 static async getStats () {