+ // FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN()
+ if (options.tagsAllOf || options.tagsOneOf) {
+ const createTagsIn = (tags: string[]) => {
+ return tags.map(t => VideoModel.sequelize.escape(t))
+ .join(', ')
+ }
+
+ if (options.tagsOneOf) {
+ query.where['id'][Sequelize.Op.in] = Sequelize.literal(
+ '(' +
+ 'SELECT "videoId" FROM "videoTag" ' +
+ 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
+ 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsOneOf) + ')' +
+ ')'
+ )
+ }
+
+ if (options.tagsAllOf) {
+ query.where['id'][Sequelize.Op.in] = Sequelize.literal(
+ '(' +
+ 'SELECT "videoId" FROM "videoTag" ' +
+ 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
+ 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsAllOf) + ')' +
+ 'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + options.tagsAllOf.length +
+ ')'
+ )
+ }
+ }
+
+ if (options.nsfw === true || options.nsfw === false) {
+ query.where['nsfw'] = options.nsfw
+ }
+
+ if (options.categoryOneOf) {
+ query.where['category'] = {
+ [Sequelize.Op.or]: options.categoryOneOf
+ }
+ }
+
+ if (options.licenceOneOf) {
+ query.where['licence'] = {
+ [Sequelize.Op.or]: options.licenceOneOf
+ }
+ }
+
+ if (options.languageOneOf) {
+ query.where['language'] = {
+ [Sequelize.Op.or]: options.languageOneOf
+ }