-
- query.subQuery = false
- }
-
- if (options.filter && (options.filter === 'local' || options.filter === 'all-local')) {
- whereAnd.push({
- remote: false
- })
- }
-
- if (options.accountId || options.videoChannelId) {
- const videoChannelInclude: IncludeOptions = {
- attributes: [],
- model: VideoChannelModel.unscoped(),
- required: true
- }
-
- if (options.videoChannelId) {
- videoChannelInclude.where = {
- id: options.videoChannelId
- }
- }
-
- if (options.accountId) {
- const accountInclude: IncludeOptions = {
- attributes: [],
- model: AccountModel.unscoped(),
- required: true
- }
-
- accountInclude.where = { id: options.accountId }
- videoChannelInclude.include = [ accountInclude ]
- }
-
- query.include.push(videoChannelInclude)
- }
-
- if (options.followerActorId) {
- let localVideosReq = ''
- if (options.includeLocalVideos === true) {
- localVideosReq = ' UNION ALL SELECT "video"."id" FROM "video" WHERE remote IS FALSE'
- }
-
- // Force actorId to be a number to avoid SQL injections
- const actorIdNumber = parseInt(options.followerActorId.toString(), 10)
- whereAnd.push({
- id: {
- [Op.in]: Sequelize.literal(
- '(' +
- 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
- 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
- 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
- ' UNION ALL ' +
- '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" ' +
- 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
- 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
- localVideosReq +
- ')'
- )
- }
- })
- }
-
- if (options.withFiles === true) {
- whereAnd.push({
- id: {
- [Op.in]: Sequelize.literal(
- '(SELECT "videoId" FROM "videoFile")'
- )
- }
- })
- }
-
- // FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN()
- if (options.tagsAllOf || options.tagsOneOf) {
- if (options.tagsOneOf) {
- const tagsOneOfLower = options.tagsOneOf.map(t => t.toLowerCase())
-
- whereAnd.push({
- id: {
- [Op.in]: Sequelize.literal(
- '(' +
- 'SELECT "videoId" FROM "videoTag" ' +
- 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
- 'WHERE lower("tag"."name") IN (' + createSafeIn(VideoModel, tagsOneOfLower) + ')' +
- ')'
- )
- }
- })
- }
-
- if (options.tagsAllOf) {
- const tagsAllOfLower = options.tagsAllOf.map(t => t.toLowerCase())
-
- whereAnd.push({
- id: {
- [Op.in]: Sequelize.literal(
- '(' +
- 'SELECT "videoId" FROM "videoTag" ' +
- 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
- 'WHERE lower("tag"."name") IN (' + createSafeIn(VideoModel, tagsAllOfLower) + ')' +
- 'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length +
- ')'
- )
- }
- })
- }
- }
-
- if (options.nsfw === true || options.nsfw === false) {
- whereAnd.push({ nsfw: options.nsfw })
- }
-
- if (options.categoryOneOf) {
- whereAnd.push({
- category: {
- [Op.or]: options.categoryOneOf
- }
- })
- }
-
- if (options.licenceOneOf) {
- whereAnd.push({
- licence: {
- [Op.or]: options.licenceOneOf
- }
- })
- }
-
- if (options.languageOneOf) {
- let videoLanguages = options.languageOneOf
- if (options.languageOneOf.find(l => l === '_unknown')) {
- videoLanguages = videoLanguages.concat([ null ])
- }
-
- whereAnd.push({
- [Op.or]: [
- {
- language: {
- [Op.or]: videoLanguages
- }
- },
- {
- id: {
- [Op.in]: Sequelize.literal(
- '(' +
- 'SELECT "videoId" FROM "videoCaption" ' +
- 'WHERE "language" IN (' + createSafeIn(VideoModel, options.languageOneOf) + ') ' +
- ')'
- )
- }
- }
- ]
- })
- }
-
- if (options.trendingDays) {
- query.include.push(VideoModel.buildTrendingQuery(options.trendingDays))
-
- query.subQuery = false
- }
-
- if (options.historyOfUser) {
- query.include.push({
- model: UserVideoHistoryModel,
- required: true,
- where: {
- userId: options.historyOfUser.id
- }
- })
-
- // Even if the relation is n:m, we know that a user only have 0..1 video history
- // So we won't have multiple rows for the same video
- // Without this, we would not be able to sort on "updatedAt" column of UserVideoHistoryModel
- query.subQuery = false