From c06af5012ecc925ca924e6e20db3a1d909b1148e Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Thu, 26 Dec 2019 11:52:46 +0100 Subject: Add playlist search option and search input for add-to-video-playlist dropdown fixes #2138 --- server/models/video/video-playlist.ts | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'server/models/video/video-playlist.ts') diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index 278d80ac0..ef87a7ee9 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -13,10 +13,11 @@ import { Model, Scopes, Table, - UpdatedAt + UpdatedAt, + Sequelize } from 'sequelize-typescript' import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' -import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getSort, isOutdated, throwIfNotValid } from '../utils' +import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getSort, isOutdated, throwIfNotValid, createSimilarityAttribute } from '../utils' import { isVideoPlaylistDescriptionValid, isVideoPlaylistNameValid, @@ -67,7 +68,8 @@ type AvailableForListOptions = { type?: VideoPlaylistType accountId?: number videoChannelId?: number - privateAndUnlisted?: boolean + privateAndUnlisted?: boolean, + search?: string } @Scopes(() => ({ @@ -163,6 +165,23 @@ type AvailableForListOptions = { }) } + if (options.search) { + const escapedSearch = VideoPlaylistModel.sequelize.escape(options.search) + const escapedLikeSearch = VideoPlaylistModel.sequelize.escape('%' + options.search + '%') + whereAnd.push({ + id: { + [ Op.in ]: Sequelize.literal( + '(' + + 'SELECT "videoPlaylist"."id" FROM "videoPlaylist" ' + + 'WHERE ' + + 'lower(immutable_unaccent("videoPlaylist"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' + + 'lower(immutable_unaccent("videoPlaylist"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' + + ')' + ) + } + }) + } + const where = { [Op.and]: whereAnd } @@ -291,7 +310,8 @@ export class VideoPlaylistModel extends Model { type?: VideoPlaylistType, accountId?: number, videoChannelId?: number, - privateAndUnlisted?: boolean + privateAndUnlisted?: boolean, + search?: string }) { const query = { offset: options.start, @@ -308,7 +328,8 @@ export class VideoPlaylistModel extends Model { followerActorId: options.followerActorId, accountId: options.accountId, videoChannelId: options.videoChannelId, - privateAndUnlisted: options.privateAndUnlisted + privateAndUnlisted: options.privateAndUnlisted, + search: options.search } as AvailableForListOptions ] }, -- cgit v1.2.3