X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Ftag.ts;h=7900e070dd8a0dfa4205859b20fb081fc1dc49ed;hb=64324ac646b0938e35cd88771492623b640bd0d8;hp=b39621eaf4a4daf55cdb93a3a5f0fe903674bd7f;hpb=0491173a61aed66205c017e0d7e0503ea316c144;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/tag.ts b/server/models/video/tag.ts index b39621eaf..7900e070d 100644 --- a/server/models/video/tag.ts +++ b/server/models/video/tag.ts @@ -1,11 +1,12 @@ -import * as Bluebird from 'bluebird' -import * as Sequelize from 'sequelize' +import { col, fn, QueryTypes, Transaction } from 'sequelize' import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { MTag } from '@server/types/models' +import { AttributesOnly } from '@shared/typescript-utils' +import { VideoPrivacy, VideoState } from '../../../shared/models/videos' import { isVideoTagValid } from '../../helpers/custom-validators/videos' import { throwIfNotValid } from '../utils' import { VideoModel } from './video' import { VideoTagModel } from './video-tag' -import { VideoPrivacy, VideoState } from '../../../shared/models/videos' @Table({ tableName: 'tag', @@ -14,10 +15,14 @@ import { VideoPrivacy, VideoState } from '../../../shared/models/videos' { fields: [ 'name' ], unique: true + }, + { + name: 'tag_lower_name', + fields: [ fn('lower', col('name')) ] } ] }) -export class TagModel extends Model { +export class TagModel extends Model>> { @AllowNull(false) @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag')) @@ -37,10 +42,10 @@ export class TagModel extends Model { }) Videos: VideoModel[] - static findOrCreateTags (tags: string[], transaction: Sequelize.Transaction) { - if (tags === null) return [] + static findOrCreateTags (tags: string[], transaction: Transaction): Promise { + if (tags === null) return Promise.resolve([]) - const tasks: Bluebird[] = [] + const tasks: Promise[] = [] tags.forEach(tag => { const query = { where: { @@ -52,7 +57,7 @@ export class TagModel extends Model { transaction } - const promise = TagModel.findOrCreate(query) + const promise = TagModel.findOrCreate(query) .then(([ tagInstance ]) => tagInstance) tasks.push(promise) }) @@ -61,7 +66,7 @@ export class TagModel extends Model { } // threshold corresponds to how many video the field should have to be returned - static getRandomSamples (threshold: number, count: number): Bluebird { + static getRandomSamples (threshold: number, count: number): Promise { const query = 'SELECT tag.name FROM tag ' + 'INNER JOIN "videoTag" ON "videoTag"."tagId" = tag.id ' + 'INNER JOIN video ON video.id = "videoTag"."videoId" ' + @@ -72,10 +77,10 @@ export class TagModel extends Model { const options = { bind: { threshold, count, videoPrivacy: VideoPrivacy.PUBLIC, videoState: VideoState.PUBLISHED }, - type: Sequelize.QueryTypes.SELECT + type: QueryTypes.SELECT as QueryTypes.SELECT } - return TagModel.sequelize.query(query, options) + return TagModel.sequelize.query<{ name: string }>(query, options) .then(data => data.map(d => d.name)) } }