aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-query-builder.ts
blob: 455f9f30ff032c158404535196db985de1e0e36d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
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 validator from 'validator'
import { exists } from '@server/helpers/custom-validators/misc'

export type BuildVideosQueryOptions = {
  attributes?: string[]

  serverAccountId: number
  followerActorId: number
  includeLocalVideos: boolean

  count: number
  start: number
  sort: string

  filter?: VideoFilter
  categoryOneOf?: number[]
  nsfw?: boolean
  licenceOneOf?: number[]
  languageOneOf?: string[]
  tagsOneOf?: string[]
  tagsAllOf?: string[]

  withFiles?: boolean

  accountId?: number
  videoChannelId?: number

  videoPlaylistId?: number

  trendingDays?: number
  user?: MUserAccountId
  historyOfUser?: MUserId

  startDate?: string // ISO 8601
  endDate?: string // ISO 8601
  originallyPublishedStartDate?: string
  originallyPublishedEndDate?: string

  durationMin?: number // seconds
  durationMax?: number // seconds

  search?: string

  isCount?: boolean

  group?: string
  having?: string
}

function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) {
  const and: string[] = []
  const joins: string[] = []
  const replacements: any = {}
  const cte: string[] = []

  let attributes: string[] = options.attributes || [ '"video"."id"' ]
  let group = options.group || ''
  const having = options.having || ''

  joins.push(
    'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId"' +
    'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId"' +
    'INNER JOIN "actor" "accountActor" ON "account"."actorId" = "accountActor"."id"'
  )

  and.push('"video"."id" NOT IN (SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")')

  if (options.serverAccountId) {
    const blockerIds = [ options.serverAccountId ]
    if (options.user) blockerIds.push(options.user.Account.id)

    const inClause = createSafeIn(model, blockerIds)

    and.push(
      'NOT EXISTS (' +
      '  SELECT 1 FROM "accountBlocklist" ' +
      '  WHERE "accountBlocklist"."accountId" IN (' + inClause + ') ' +
      '  AND "accountBlocklist"."targetAccountId" = "account"."id" ' +
      ')' +
      'AND NOT EXISTS (' +
      '  SELECT 1 FROM "serverBlocklist" WHERE "serverBlocklist"."accountId" IN (' + inClause + ') ' +
      '  AND "serverBlocklist"."targetServerId" = "accountActor"."serverId"' +
      ')'
    )
  }

  // Only list public/published videos
  if (!options.filter || options.filter !== 'all-local') {
    and.push(
      `("video"."state" = ${VideoState.PUBLISHED} OR ` +
      `("video"."state" = ${VideoState.TO_TRANSCODE} AND "video"."waitTranscoding" IS false))`
    )

    if (options.user) {
      and.push(
        `("video"."privacy" = ${VideoPrivacy.PUBLIC} OR "video"."privacy" = ${VideoPrivacy.INTERNAL})`
      )
    } else { // Or only public videos
      and.push(
        `"video"."privacy" = ${VideoPrivacy.PUBLIC}`
      )
    }
  }

  if (options.videoPlaylistId) {
    joins.push(
      'INNER JOIN "videoPlaylistElement" "video"."id" = "videoPlaylistElement"."videoId" ' +
      'AND "videoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
    )

    replacements.videoPlaylistId = options.videoPlaylistId
  }

  if (options.filter && (options.filter === 'local' || options.filter === 'all-local')) {
    and.push('"video"."remote" IS FALSE')
  }

  if (options.accountId) {
    and.push('"account"."id" = :accountId')
    replacements.accountId = options.accountId
  }

  if (options.videoChannelId) {
    and.push('"videoChannel"."id" = :videoChannelId')
    replacements.videoChannelId = options.videoChannelId
  }

  if (options.followerActorId) {
    let query =
      '(' +
      '  EXISTS (' +
      '    SELECT 1 FROM "videoShare" ' +
      '    INNER JOIN "actorFollow" "actorFollowShare" ON "actorFollowShare"."targetActorId" = "videoShare"."actorId" ' +
      '    AND "actorFollowShare"."actorId" = :followerActorId WHERE "videoShare"."videoId" = "video"."id"' +
      '  )' +
      '  OR' +
      '  EXISTS (' +
      '    SELECT 1 from "actorFollow" ' +
      '    WHERE "actorFollow"."targetActorId" = "videoChannel"."actorId" AND "actorFollow"."actorId" = :followerActorId' +
      '  )'

    if (options.includeLocalVideos) {
      query += '  OR "video"."remote" IS FALSE'
    }

    query += ')'

    and.push(query)
    replacements.followerActorId = options.followerActorId
  }

  if (options.withFiles === true) {
    and.push('EXISTS (SELECT 1 FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id")')
  }

  if (options.tagsOneOf) {
    const tagsOneOfLower = options.tagsOneOf.map(t => t.toLowerCase())

    and.push(
      'EXISTS (' +
      '  SELECT 1 FROM "videoTag" ' +
      '  INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
      '  WHERE lower("tag"."name") IN (' + createSafeIn(model, tagsOneOfLower) + ') ' +
      '  AND "video"."id" = "videoTag"."videoId"' +
      ')'
    )
  }

  if (options.tagsAllOf) {
    const tagsAllOfLower = options.tagsAllOf.map(t => t.toLowerCase())

    and.push(
      'EXISTS (' +
      '  SELECT 1 FROM "videoTag" ' +
      '  INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
      '  WHERE lower("tag"."name") IN (' + createSafeIn(model, tagsAllOfLower) + ') ' +
      '  AND "video"."id" = "videoTag"."videoId" ' +
      '  GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length +
      ')'
    )
  }

  if (options.nsfw === true) {
    and.push('"video"."nsfw" IS TRUE')
  }

  if (options.nsfw === false) {
    and.push('"video"."nsfw" IS FALSE')
  }

  if (options.categoryOneOf) {
    and.push('"video"."category" IN (:categoryOneOf)')
    replacements.categoryOneOf = options.categoryOneOf
  }

  if (options.licenceOneOf) {
    and.push('"video"."licence" IN (:licenceOneOf)')
    replacements.licenceOneOf = options.licenceOneOf
  }

  if (options.languageOneOf) {
    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')) {
      languagesQueryParts.push('"video"."language" IS NULL')
    }

    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
  if (options.trendingDays && options.isCount !== true) {
    const viewsGteDate = new Date(new Date().getTime() - (24 * 3600 * 1000) * options.trendingDays)

    joins.push('LEFT JOIN "videoView" ON "video"."id" = "videoView"."videoId" AND "videoView"."startDate" >= :viewsGteDate')
    replacements.viewsGteDate = viewsGteDate

    attributes.push('COALESCE(SUM("videoView"."views"), 0) AS "videoViewsSum"')

    group = 'GROUP BY "video"."id"'
  }

  if (options.historyOfUser) {
    joins.push('INNER JOIN "userVideoHistory" on "video"."id" = "userVideoHistory"."videoId"')

    and.push('"userVideoHistory"."userId" = :historyOfUser')
    replacements.historyOfUser = options.historyOfUser.id
  }

  if (options.startDate) {
    and.push('"video"."publishedAt" >= :startDate')
    replacements.startDate = options.startDate
  }

  if (options.endDate) {
    and.push('"video"."publishedAt" <= :endDate')
    replacements.endDate = options.endDate
  }

  if (options.originallyPublishedStartDate) {
    and.push('"video"."originallyPublishedAt" >= :originallyPublishedStartDate')
    replacements.originallyPublishedStartDate = options.originallyPublishedStartDate
  }

  if (options.originallyPublishedEndDate) {
    and.push('"video"."originallyPublishedAt" <= :originallyPublishedEndDate')
    replacements.originallyPublishedEndDate = options.originallyPublishedEndDate
  }

  if (options.durationMin) {
    and.push('"video"."duration" >= :durationMin')
    replacements.durationMin = options.durationMin
  }

  if (options.durationMax) {
    and.push('"video"."duration" <= :durationMax')
    replacements.durationMax = options.durationMax
  }

  if (options.search) {
    const escapedSearch = model.sequelize.escape(options.search)
    const escapedLikeSearch = model.sequelize.escape('%' + options.search + '%')

    cte.push(
      '"trigramSearch" AS (' +
      '  SELECT "video"."id", ' +
      `  similarity(lower(immutable_unaccent("video"."name")), lower(immutable_unaccent(${escapedSearch}))) as similarity ` +
      '  FROM "video" ' +
      '  WHERE lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' +
      '        lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
      ')'
    )

    joins.push('LEFT JOIN "trigramSearch" ON "video"."id" = "trigramSearch"."id"')

    let base = '(' +
    '  "trigramSearch"."id" IS NOT NULL OR ' +
    '  EXISTS (' +
    '    SELECT 1 FROM "videoTag" ' +
    '    INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
    `    WHERE lower("tag"."name") = ${escapedSearch} ` +
    '    AND "video"."id" = "videoTag"."videoId"' +
    '  )'

    if (validator.isUUID(options.search)) {
      base += ` OR "video"."uuid" = ${escapedSearch}`
    }

    base += ')'
    and.push(base)

    attributes.push(`COALESCE("trigramSearch"."similarity", 0) as similarity`)
  } else {
    attributes.push('0 as similarity')
  }

  if (options.isCount === true) attributes = [ 'COUNT(*) as "total"' ]

  let suffix = ''
  let order = ''
  if (options.isCount !== true) {

    if (exists(options.sort)) {
      if (options.sort === '-originallyPublishedAt' || options.sort === 'originallyPublishedAt') {
        attributes.push('COALESCE("video"."originallyPublishedAt", "video"."publishedAt") AS "publishedAtForOrder"')
      }

      order = buildOrder(model, options.sort)
      suffix += `${order} `
    }

    if (exists(options.count)) {
      const count = parseInt(options.count + '', 10)
      suffix += `LIMIT ${count} `
    }

    if (exists(options.start)) {
      const start = parseInt(options.start + '', 10)
      suffix += `OFFSET ${start} `
    }
  }

  const cteString = cte.length !== 0
    ? `WITH ${cte.join(', ')} `
    : ''

  const query = cteString +
    'SELECT ' + attributes.join(', ') + ' ' +
    'FROM "video" ' + joins.join(' ') + ' ' +
    'WHERE ' + and.join(' AND ') + ' ' +
    group + ' ' +
    having + ' ' +
    suffix

  return { query, replacements, order }
}

function buildOrder (model: typeof Model, value: string) {
  const { direction, field } = buildDirectionAndField(value)
  if (field.match(/^[a-zA-Z."]+$/) === null) throw new Error('Invalid sort column ' + field)

  if (field.toLowerCase() === 'random') return 'ORDER BY RANDOM()'

  if (field.toLowerCase() === 'trending') { // Sort by aggregation
    return `ORDER BY "videoViewsSum" ${direction}, "video"."views" ${direction}`
  }

  let firstSort: string

  if (field.toLowerCase() === 'match') { // Search
    firstSort = '"similarity"'
  } else if (field === 'originallyPublishedAt') {
    firstSort = '"publishedAtForOrder"'
  } else if (field.includes('.')) {
    firstSort = field
  } else {
    firstSort = `"video"."${field}"`
  }

  return `ORDER BY ${firstSort} ${direction}, "video"."id" ASC`
}

function wrapForAPIResults (baseQuery: string, replacements: any, options: BuildVideosQueryOptions, order: string) {
  const attributes = {
    '"video".*': '',
    '"VideoChannel"."id"': '"VideoChannel.id"',
    '"VideoChannel"."name"': '"VideoChannel.name"',
    '"VideoChannel"."description"': '"VideoChannel.description"',
    '"VideoChannel"."actorId"': '"VideoChannel.actorId"',
    '"VideoChannel->Actor"."id"': '"VideoChannel.Actor.id"',
    '"VideoChannel->Actor"."preferredUsername"': '"VideoChannel.Actor.preferredUsername"',
    '"VideoChannel->Actor"."url"': '"VideoChannel.Actor.url"',
    '"VideoChannel->Actor"."serverId"': '"VideoChannel.Actor.serverId"',
    '"VideoChannel->Actor"."avatarId"': '"VideoChannel.Actor.avatarId"',
    '"VideoChannel->Account"."id"': '"VideoChannel.Account.id"',
    '"VideoChannel->Account"."name"': '"VideoChannel.Account.name"',
    '"VideoChannel->Account->Actor"."id"': '"VideoChannel.Account.Actor.id"',
    '"VideoChannel->Account->Actor"."preferredUsername"': '"VideoChannel.Account.Actor.preferredUsername"',
    '"VideoChannel->Account->Actor"."url"': '"VideoChannel.Account.Actor.url"',
    '"VideoChannel->Account->Actor"."serverId"': '"VideoChannel.Account.Actor.serverId"',
    '"VideoChannel->Account->Actor"."avatarId"': '"VideoChannel.Account.Actor.avatarId"',
    '"VideoChannel->Actor->Server"."id"': '"VideoChannel.Actor.Server.id"',
    '"VideoChannel->Actor->Server"."host"': '"VideoChannel.Actor.Server.host"',
    '"VideoChannel->Actor->Avatar"."id"': '"VideoChannel.Actor.Avatar.id"',
    '"VideoChannel->Actor->Avatar"."filename"': '"VideoChannel.Actor.Avatar.filename"',
    '"VideoChannel->Actor->Avatar"."fileUrl"': '"VideoChannel.Actor.Avatar.fileUrl"',
    '"VideoChannel->Actor->Avatar"."onDisk"': '"VideoChannel.Actor.Avatar.onDisk"',
    '"VideoChannel->Actor->Avatar"."createdAt"': '"VideoChannel.Actor.Avatar.createdAt"',
    '"VideoChannel->Actor->Avatar"."updatedAt"': '"VideoChannel.Actor.Avatar.updatedAt"',
    '"VideoChannel->Account->Actor->Server"."id"': '"VideoChannel.Account.Actor.Server.id"',
    '"VideoChannel->Account->Actor->Server"."host"': '"VideoChannel.Account.Actor.Server.host"',
    '"VideoChannel->Account->Actor->Avatar"."id"': '"VideoChannel.Account.Actor.Avatar.id"',
    '"VideoChannel->Account->Actor->Avatar"."filename"': '"VideoChannel.Account.Actor.Avatar.filename"',
    '"VideoChannel->Account->Actor->Avatar"."fileUrl"': '"VideoChannel.Account.Actor.Avatar.fileUrl"',
    '"VideoChannel->Account->Actor->Avatar"."onDisk"': '"VideoChannel.Account.Actor.Avatar.onDisk"',
    '"VideoChannel->Account->Actor->Avatar"."createdAt"': '"VideoChannel.Account.Actor.Avatar.createdAt"',
    '"VideoChannel->Account->Actor->Avatar"."updatedAt"': '"VideoChannel.Account.Actor.Avatar.updatedAt"',
    '"Thumbnails"."id"': '"Thumbnails.id"',
    '"Thumbnails"."type"': '"Thumbnails.type"',
    '"Thumbnails"."filename"': '"Thumbnails.filename"'
  }

  const joins = [
    'INNER JOIN "video" ON "tmp"."id" = "video"."id"',

    'INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"',
    'INNER JOIN "actor" AS "VideoChannel->Actor" ON "VideoChannel"."actorId" = "VideoChannel->Actor"."id"',
    'INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"',
    'INNER JOIN "actor" AS "VideoChannel->Account->Actor" ON "VideoChannel->Account"."actorId" = "VideoChannel->Account->Actor"."id"',

    'LEFT OUTER JOIN "server" AS "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id"',
    'LEFT OUTER JOIN "avatar" AS "VideoChannel->Actor->Avatar" ON "VideoChannel->Actor"."avatarId" = "VideoChannel->Actor->Avatar"."id"',

    'LEFT OUTER JOIN "server" AS "VideoChannel->Account->Actor->Server" ' +
      'ON "VideoChannel->Account->Actor"."serverId" = "VideoChannel->Account->Actor->Server"."id"',

    'LEFT OUTER JOIN "avatar" AS "VideoChannel->Account->Actor->Avatar" ' +
      'ON "VideoChannel->Account->Actor"."avatarId" = "VideoChannel->Account->Actor->Avatar"."id"',

    'LEFT OUTER JOIN "thumbnail" AS "Thumbnails" ON "video"."id" = "Thumbnails"."videoId"'
  ]

  if (options.withFiles) {
    joins.push('INNER JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"')

    Object.assign(attributes, {
      '"VideoFiles"."id"': '"VideoFiles.id"',
      '"VideoFiles"."createdAt"': '"VideoFiles.createdAt"',
      '"VideoFiles"."updatedAt"': '"VideoFiles.updatedAt"',
      '"VideoFiles"."resolution"': '"VideoFiles.resolution"',
      '"VideoFiles"."size"': '"VideoFiles.size"',
      '"VideoFiles"."extname"': '"VideoFiles.extname"',
      '"VideoFiles"."infoHash"': '"VideoFiles.infoHash"',
      '"VideoFiles"."fps"': '"VideoFiles.fps"',
      '"VideoFiles"."videoId"': '"VideoFiles.videoId"'
    })
  }

  if (options.user) {
    joins.push(
      'LEFT OUTER JOIN "userVideoHistory" ' +
      'ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :userVideoHistoryId'
    )
    replacements.userVideoHistoryId = options.user.id

    Object.assign(attributes, {
      '"userVideoHistory"."id"': '"userVideoHistory.id"',
      '"userVideoHistory"."currentTime"': '"userVideoHistory.currentTime"'
    })
  }

  if (options.videoPlaylistId) {
    joins.push(
      'INNER JOIN "videoPlaylistElement" as "VideoPlaylistElement" ON "videoPlaylistElement"."videoId" = "video"."id" ' +
      'AND "VideoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
    )
    replacements.videoPlaylistId = options.videoPlaylistId

    Object.assign(attributes, {
      '"VideoPlaylistElement"."createdAt"': '"VideoPlaylistElement.createdAt"',
      '"VideoPlaylistElement"."updatedAt"': '"VideoPlaylistElement.updatedAt"',
      '"VideoPlaylistElement"."url"': '"VideoPlaylistElement.url"',
      '"VideoPlaylistElement"."position"': '"VideoPlaylistElement.position"',
      '"VideoPlaylistElement"."startTimestamp"': '"VideoPlaylistElement.startTimestamp"',
      '"VideoPlaylistElement"."stopTimestamp"': '"VideoPlaylistElement.stopTimestamp"',
      '"VideoPlaylistElement"."videoPlaylistId"': '"VideoPlaylistElement.videoPlaylistId"'
    })
  }

  const select = 'SELECT ' + Object.keys(attributes).map(key => {
    const value = attributes[key]
    if (value) return `${key} AS ${value}`

    return key
  }).join(', ')

  return `${select} FROM (${baseQuery}) AS "tmp" ${joins.join(' ')} ${order}`
}

export {
  buildListQuery,
  wrapForAPIResults
}