Litte SQL optimzation in videos list

pull/2356/head
Chocobozzz 2019-12-23 09:19:40 +01:00
parent 5def76ebba
commit 1c5fed88c5
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 24 additions and 24 deletions

View File

@ -143,6 +143,7 @@ import { MThumbnail } from '../../typings/models/video/thumbnail'
import { VideoFile } from '@shared/models/videos/video-file.model' import { VideoFile } from '@shared/models/videos/video-file.model'
import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths' import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
import * as validator from 'validator' import * as validator from 'validator'
import { ActorFollowModel } from '@server/models/activitypub/actor-follow'
// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
const indexes: (ModelIndexesOptions & { where?: WhereOptions })[] = [ const indexes: (ModelIndexesOptions & { where?: WhereOptions })[] = [
@ -440,36 +441,35 @@ export type AvailableForListIDsOptions = {
} }
if (options.followerActorId) { if (options.followerActorId) {
let localVideosReq = '' let localVideosReq: WhereOptions = {}
if (options.includeLocalVideos === true) { if (options.includeLocalVideos === true) {
localVideosReq = ' UNION ALL ' + localVideosReq = { remote: false }
'SELECT "video"."id" AS "id" FROM "video" ' +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
'WHERE "actor"."serverId" IS NULL'
} }
// Force actorId to be a number to avoid SQL injections // Force actorId to be a number to avoid SQL injections
const actorIdNumber = parseInt(options.followerActorId.toString(), 10) const actorIdNumber = parseInt(options.followerActorId.toString(), 10)
whereAnd.push({ whereAnd.push({
id: { [Op.or]: [
[ Op.in ]: Sequelize.literal( {
'(' + id: {
'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' + [ Op.in ]: Sequelize.literal(
'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' + '(' +
'WHERE "actorFollow"."actorId" = ' + actorIdNumber + 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
' UNION ALL ' + 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
'SELECT "video"."id" AS "id" FROM "video" ' + 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + ' UNION ALL ' +
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + 'SELECT "video"."id" AS "id" FROM "video" ' +
'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' + 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
'WHERE "actorFollow"."actorId" = ' + actorIdNumber + 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
localVideosReq + 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
')' 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
) ')'
} )
}
},
localVideosReq
]
}) })
} }