From 3cf53828db9b1f0d65e91a07d2a65f2f4ad82270 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 9 Dec 2019 15:32:53 +0100 Subject: Add ability to search by UUID --- server/models/video/video.ts | 50 ++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'server/models') diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 7e3512fe1..73e0d2345 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -59,8 +59,6 @@ import { ACTIVITY_PUB, API_VERSION, CONSTRAINTS_FIELDS, - HLS_REDUNDANCY_DIRECTORY, - HLS_STREAMING_PLAYLIST_DIRECTORY, LAZY_STATIC_PATHS, REMOTE_SCHEME, STATIC_DOWNLOAD_PATHS, @@ -143,7 +141,8 @@ import { import { MVideoFile, MVideoFileStreamingPlaylistVideo } from '../../typings/models/video/video-file' import { MThumbnail } from '../../typings/models/video/thumbnail' import { VideoFile } from '@shared/models/videos/video-file.model' -import { getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath, getHLSDirectory } from '@server/lib/video-paths' +import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths' +import * as validator from 'validator' // 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 })[] = [ @@ -1352,24 +1351,35 @@ export class VideoModel extends Model { const escapedSearch = VideoModel.sequelize.escape(options.search) const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%') if (options.search) { - whereAnd.push( - { - id: { - [ Op.in ]: Sequelize.literal( - '(' + - 'SELECT "video"."id" FROM "video" ' + - 'WHERE ' + - 'lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' + - 'lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' + - 'UNION ALL ' + - 'SELECT "video"."id" FROM "video" LEFT JOIN "videoTag" ON "videoTag"."videoId" = "video"."id" ' + - 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + - 'WHERE "tag"."name" = ' + escapedSearch + - ')' - ) - } + const trigramSearch = { + id: { + [ Op.in ]: Sequelize.literal( + '(' + + 'SELECT "video"."id" FROM "video" ' + + 'WHERE ' + + 'lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' + + 'lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' + + 'UNION ALL ' + + 'SELECT "video"."id" FROM "video" LEFT JOIN "videoTag" ON "videoTag"."videoId" = "video"."id" ' + + 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + + 'WHERE "tag"."name" = ' + escapedSearch + + ')' + ) } - ) + } + + if (validator.isUUID(options.search)) { + whereAnd.push({ + [Op.or]: [ + trigramSearch, + { + uuid: options.search + } + ] + }) + } else { + whereAnd.push(trigramSearch) + } attributesInclude.push(createSimilarityAttribute('VideoModel.name', options.search)) } -- cgit v1.2.3