Lowercase video tags search
This commit is contained in:
parent
aafbc63aae
commit
4b1f1b810a
3 changed files with 16 additions and 8 deletions
|
@ -1,5 +1,5 @@
|
||||||
import * as Bluebird from 'bluebird'
|
import * as Bluebird from 'bluebird'
|
||||||
import { QueryTypes, Transaction } from 'sequelize'
|
import { fn, QueryTypes, Transaction, col } from 'sequelize'
|
||||||
import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
||||||
import { isVideoTagValid } from '../../helpers/custom-validators/videos'
|
import { isVideoTagValid } from '../../helpers/custom-validators/videos'
|
||||||
import { throwIfNotValid } from '../utils'
|
import { throwIfNotValid } from '../utils'
|
||||||
|
@ -15,6 +15,10 @@ import { MTag } from '@server/typings/models'
|
||||||
{
|
{
|
||||||
fields: [ 'name' ],
|
fields: [ 'name' ],
|
||||||
unique: true
|
unique: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'tag_lower_name',
|
||||||
|
fields: [ fn('lower', col('name')) ] as any // FIXME: typings
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
|
@ -468,13 +468,15 @@ export type AvailableForListIDsOptions = {
|
||||||
// FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN()
|
// 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.tagsAllOf || options.tagsOneOf) {
|
||||||
if (options.tagsOneOf) {
|
if (options.tagsOneOf) {
|
||||||
|
const tagsOneOfLower = options.tagsOneOf.map(t => t.toLowerCase())
|
||||||
|
|
||||||
whereAnd.push({
|
whereAnd.push({
|
||||||
id: {
|
id: {
|
||||||
[ Op.in ]: Sequelize.literal(
|
[ Op.in ]: Sequelize.literal(
|
||||||
'(' +
|
'(' +
|
||||||
'SELECT "videoId" FROM "videoTag" ' +
|
'SELECT "videoId" FROM "videoTag" ' +
|
||||||
'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
|
'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
|
||||||
'WHERE "tag"."name" IN (' + createSafeIn(VideoModel, options.tagsOneOf) + ')' +
|
'WHERE lower("tag"."name") IN (' + createSafeIn(VideoModel, tagsOneOfLower) + ')' +
|
||||||
')'
|
')'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -482,14 +484,16 @@ export type AvailableForListIDsOptions = {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.tagsAllOf) {
|
if (options.tagsAllOf) {
|
||||||
|
const tagsAllOfLower = options.tagsAllOf.map(t => t.toLowerCase())
|
||||||
|
|
||||||
whereAnd.push({
|
whereAnd.push({
|
||||||
id: {
|
id: {
|
||||||
[ Op.in ]: Sequelize.literal(
|
[ Op.in ]: Sequelize.literal(
|
||||||
'(' +
|
'(' +
|
||||||
'SELECT "videoId" FROM "videoTag" ' +
|
'SELECT "videoId" FROM "videoTag" ' +
|
||||||
'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
|
'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
|
||||||
'WHERE "tag"."name" IN (' + createSafeIn(VideoModel, options.tagsAllOf) + ')' +
|
'WHERE lower("tag"."name") IN (' + createSafeIn(VideoModel, tagsAllOfLower) + ')' +
|
||||||
'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + options.tagsAllOf.length +
|
'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length +
|
||||||
')'
|
')'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -206,7 +206,7 @@ describe('Test videos search', function () {
|
||||||
const query = {
|
const query = {
|
||||||
search: '9999',
|
search: '9999',
|
||||||
categoryOneOf: [ 1 ],
|
categoryOneOf: [ 1 ],
|
||||||
tagsOneOf: [ 'aaaa', 'ffff' ]
|
tagsOneOf: [ 'aAaa', 'ffff' ]
|
||||||
}
|
}
|
||||||
const res1 = await advancedVideosSearch(server.url, query)
|
const res1 = await advancedVideosSearch(server.url, query)
|
||||||
expect(res1.body.total).to.equal(2)
|
expect(res1.body.total).to.equal(2)
|
||||||
|
@ -219,15 +219,15 @@ describe('Test videos search', function () {
|
||||||
const query = {
|
const query = {
|
||||||
search: '9999',
|
search: '9999',
|
||||||
categoryOneOf: [ 1 ],
|
categoryOneOf: [ 1 ],
|
||||||
tagsAllOf: [ 'cccc' ]
|
tagsAllOf: [ 'CCcc' ]
|
||||||
}
|
}
|
||||||
const res1 = await advancedVideosSearch(server.url, query)
|
const res1 = await advancedVideosSearch(server.url, query)
|
||||||
expect(res1.body.total).to.equal(2)
|
expect(res1.body.total).to.equal(2)
|
||||||
|
|
||||||
const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsAllOf: [ 'blabla' ] }))
|
const res2 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsAllOf: [ 'blAbla' ] }))
|
||||||
expect(res2.body.total).to.equal(0)
|
expect(res2.body.total).to.equal(0)
|
||||||
|
|
||||||
const res3 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsAllOf: [ 'bbbb', 'cccc' ] }))
|
const res3 = await advancedVideosSearch(server.url, immutableAssign(query, { tagsAllOf: [ 'bbbb', 'CCCC' ] }))
|
||||||
expect(res3.body.total).to.equal(1)
|
expect(res3.body.total).to.equal(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue