X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-query-builder.ts;h=25d5042b79e47174e9d50e02012e543a04258714;hb=bb4ba6d94c5051fdd665ebe63fffcc105778b8be;hp=655abf19871f7e23991e59fc5f5feac6a1a80bf5;hpb=fab6746354f9d9cb65c35d8bd9352c4b773b4c69;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-query-builder.ts b/server/models/video/video-query-builder.ts index 655abf198..25d5042b7 100644 --- a/server/models/video/video-query-builder.ts +++ b/server/models/video/video-query-builder.ts @@ -1,7 +1,7 @@ import { VideoFilter, VideoPrivacy, VideoState } from '@shared/models' import { buildDirectionAndField, createSafeIn } from '@server/models/utils' import { Model } from 'sequelize-typescript' -import { MUserAccountId, MUserId } from '@server/typings/models' +import { MUserAccountId, MUserId } from '@server/types/models' import validator from 'validator' import { exists } from '@server/helpers/custom-validators/misc' @@ -89,7 +89,7 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) } // Only list public/published videos - if (!options.filter || options.filter !== 'all-local') { + if (!options.filter || (options.filter !== 'all-local' && options.filter !== 'all')) { and.push( `("video"."state" = ${VideoState.PUBLISHED} OR ` + `("video"."state" = ${VideoState.TO_TRANSCODE} AND "video"."waitTranscoding" IS false))` @@ -135,12 +135,14 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) ' EXISTS (' + ' SELECT 1 FROM "videoShare" ' + ' INNER JOIN "actorFollow" "actorFollowShare" ON "actorFollowShare"."targetActorId" = "videoShare"."actorId" ' + - ' AND "actorFollowShare"."actorId" = :followerActorId WHERE "videoShare"."videoId" = "video"."id"' + + ' AND "actorFollowShare"."actorId" = :followerActorId AND "actorFollowShare"."state" = \'accepted\' ' + + ' WHERE "videoShare"."videoId" = "video"."id"' + ' )' + ' OR' + ' EXISTS (' + ' SELECT 1 from "actorFollow" ' + - ' WHERE "actorFollow"."targetActorId" = "videoChannel"."actorId" AND "actorFollow"."actorId" = :followerActorId' + + ' WHERE "actorFollow"."targetActorId" = "videoChannel"."actorId" AND "actorFollow"."actorId" = :followerActorId ' + + ' AND "actorFollow"."state" = \'accepted\'' + ' )' if (options.includeLocalVideos) { @@ -154,7 +156,16 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) } if (options.withFiles === true) { - and.push('EXISTS (SELECT 1 FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id")') + and.push( + '(' + + ' EXISTS (SELECT 1 FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id") ' + + ' OR EXISTS (' + + ' SELECT 1 FROM "videoStreamingPlaylist" ' + + ' INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ' + + ' WHERE "videoStreamingPlaylist"."videoId" = "video"."id"' + + ' )' + + ')' + ) } if (options.tagsOneOf) { @@ -203,23 +214,29 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) } if (options.languageOneOf) { - replacements.languageOneOf = options.languageOneOf.filter(l => l && l !== '_unknown') - - let languagesQuery = '("video"."language" IN (:languageOneOf) OR ' + const languages = options.languageOneOf.filter(l => l && l !== '_unknown') + const languagesQueryParts: string[] = [] + + if (languages.length !== 0) { + languagesQueryParts.push('"video"."language" IN (:languageOneOf)') + replacements.languageOneOf = languages + + languagesQueryParts.push( + 'EXISTS (' + + ' SELECT 1 FROM "videoCaption" WHERE "videoCaption"."language" ' + + ' IN (' + createSafeIn(model, languages) + ') AND ' + + ' "videoCaption"."videoId" = "video"."id"' + + ')' + ) + } if (options.languageOneOf.includes('_unknown')) { - languagesQuery += '"video"."language" IS NULL OR ' + languagesQueryParts.push('"video"."language" IS NULL') } - and.push( - languagesQuery + - ' EXISTS (' + - ' SELECT 1 FROM "videoCaption" WHERE "videoCaption"."language" ' + - ' IN (' + createSafeIn(model, options.languageOneOf) + ') AND ' + - ' "videoCaption"."videoId" = "video"."id"' + - ' )' + - ')' - ) + if (languagesQueryParts.length !== 0) { + and.push('(' + languagesQueryParts.join(' OR ') + ')') + } } // We don't exclude results in this if so if we do a count we don't need to add this complex clauses @@ -315,7 +332,11 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) if (options.isCount !== true) { if (exists(options.sort)) { - order = buildOrder(model, options.sort) + if (options.sort === '-originallyPublishedAt' || options.sort === 'originallyPublishedAt') { + attributes.push('COALESCE("video"."originallyPublishedAt", "video"."publishedAt") AS "publishedAtForOrder"') + } + + order = buildOrder(options.sort) suffix += `${order} ` } @@ -345,7 +366,7 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) return { query, replacements, order } } -function buildOrder (model: typeof Model, value: string) { +function buildOrder (value: string) { const { direction, field } = buildDirectionAndField(value) if (field.match(/^[a-zA-Z."]+$/) === null) throw new Error('Invalid sort column ' + field) @@ -359,6 +380,8 @@ function buildOrder (model: typeof Model, value: string) { if (field.toLowerCase() === 'match') { // Search firstSort = '"similarity"' + } else if (field === 'originallyPublishedAt') { + firstSort = '"publishedAtForOrder"' } else if (field.includes('.')) { firstSort = field } else { @@ -429,7 +452,13 @@ function wrapForAPIResults (baseQuery: string, replacements: any, options: Build ] if (options.withFiles) { - joins.push('INNER JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"') + joins.push('LEFT JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"') + + joins.push('LEFT JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"') + joins.push( + 'LEFT JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' + + 'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"' + ) Object.assign(attributes, { '"VideoFiles"."id"': '"VideoFiles.id"', @@ -440,7 +469,18 @@ function wrapForAPIResults (baseQuery: string, replacements: any, options: Build '"VideoFiles"."extname"': '"VideoFiles.extname"', '"VideoFiles"."infoHash"': '"VideoFiles.infoHash"', '"VideoFiles"."fps"': '"VideoFiles.fps"', - '"VideoFiles"."videoId"': '"VideoFiles.videoId"' + '"VideoFiles"."videoId"': '"VideoFiles.videoId"', + + '"VideoStreamingPlaylists"."id"': '"VideoStreamingPlaylists.id"', + '"VideoStreamingPlaylists->VideoFiles"."id"': '"VideoStreamingPlaylists.VideoFiles.id"', + '"VideoStreamingPlaylists->VideoFiles"."createdAt"': '"VideoStreamingPlaylists.VideoFiles.createdAt"', + '"VideoStreamingPlaylists->VideoFiles"."updatedAt"': '"VideoStreamingPlaylists.VideoFiles.updatedAt"', + '"VideoStreamingPlaylists->VideoFiles"."resolution"': '"VideoStreamingPlaylists.VideoFiles.resolution"', + '"VideoStreamingPlaylists->VideoFiles"."size"': '"VideoStreamingPlaylists.VideoFiles.size"', + '"VideoStreamingPlaylists->VideoFiles"."extname"': '"VideoStreamingPlaylists.VideoFiles.extname"', + '"VideoStreamingPlaylists->VideoFiles"."infoHash"': '"VideoStreamingPlaylists.VideoFiles.infoHash"', + '"VideoStreamingPlaylists->VideoFiles"."fps"': '"VideoStreamingPlaylists.VideoFiles.fps"', + '"VideoStreamingPlaylists->VideoFiles"."videoId"': '"VideoStreamingPlaylists.VideoFiles.videoId"' }) }