-import { each } from 'async'
-import * as Sequelize from 'sequelize'
+import * as Bluebird from 'bluebird'
+import { fn, QueryTypes, Transaction, col } from 'sequelize'
+import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
+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'
+import { MTag } from '@server/types/models'
-import { addMethodsToModel } from '../utils'
-import {
- TagClass,
- TagInstance,
- TagAttributes,
-
- TagMethods
-} from './tag-interface'
-
-let Tag: Sequelize.Model<TagInstance, TagAttributes>
-let findOrCreateTags: TagMethods.FindOrCreateTags
-
-export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
- Tag = sequelize.define<TagInstance, TagAttributes>('Tag',
+@Table({
+ tableName: 'tag',
+ timestamps: false,
+ indexes: [
{
- name: {
- type: DataTypes.STRING,
- allowNull: false
- }
+ fields: [ 'name' ],
+ unique: true
},
{
- timestamps: false,
- indexes: [
- {
- fields: [ 'name' ],
- unique: true
- }
- ]
+ name: 'tag_lower_name',
+ fields: [ fn('lower', col('name')) ] as any // FIXME: typings
}
- )
-
- const classMethods = [
- associate,
-
- findOrCreateTags
]
- addMethodsToModel(Tag, classMethods)
+})
+export class TagModel extends Model<TagModel> {
- return Tag
-}
+ @AllowNull(false)
+ @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag'))
+ @Column
+ name: string
+
+ @CreatedAt
+ createdAt: Date
-// ---------------------------------------------------------------------------
+ @UpdatedAt
+ updatedAt: Date
-function associate (models) {
- Tag.belongsToMany(models.Video, {
+ @BelongsToMany(() => VideoModel, {
foreignKey: 'tagId',
- through: models.VideoTag,
- onDelete: 'cascade'
+ through: () => VideoTagModel,
+ onDelete: 'CASCADE'
})
-}
+ Videos: VideoModel[]
-findOrCreateTags = function (tags: string[], transaction: Sequelize.Transaction, callback: TagMethods.FindOrCreateTagsCallback) {
- const tagInstances = []
+ static findOrCreateTags (tags: string[], transaction: Transaction): Promise<MTag[]> {
+ if (tags === null) return Promise.resolve([])
- each<string, Error>(tags, function (tag, callbackEach) {
- const query: any = {
- where: {
- name: tag
- },
- defaults: {
- name: tag
+ const tasks: Bluebird<MTag>[] = []
+ tags.forEach(tag => {
+ const query = {
+ where: {
+ name: tag
+ },
+ defaults: {
+ name: tag
+ },
+ transaction
}
- }
- if (transaction) query.transaction = transaction
+ const promise = TagModel.findOrCreate<MTag>(query)
+ .then(([ tagInstance ]) => tagInstance)
+ tasks.push(promise)
+ })
- Tag.findOrCreate(query).asCallback(function (err, res) {
- if (err) return callbackEach(err)
+ return Promise.all(tasks)
+ }
- // res = [ tag, isCreated ]
- const tag = res[0]
- tagInstances.push(tag)
- return callbackEach()
- })
- }, function (err) {
- return callback(err, tagInstances)
- })
+ // threshold corresponds to how many video the field should have to be returned
+ static getRandomSamples (threshold: number, count: number): Bluebird<string[]> {
+ const query = 'SELECT tag.name FROM tag ' +
+ 'INNER JOIN "videoTag" ON "videoTag"."tagId" = tag.id ' +
+ 'INNER JOIN video ON video.id = "videoTag"."videoId" ' +
+ 'WHERE video.privacy = $videoPrivacy AND video.state = $videoState ' +
+ 'GROUP BY tag.name HAVING COUNT(tag.name) >= $threshold ' +
+ 'ORDER BY random() ' +
+ 'LIMIT $count'
+
+ const options = {
+ bind: { threshold, count, videoPrivacy: VideoPrivacy.PUBLIC, videoState: VideoState.PUBLISHED },
+ type: QueryTypes.SELECT as QueryTypes.SELECT
+ }
+
+ return TagModel.sequelize.query<{ name: string }>(query, options)
+ .then(data => data.map(d => d.name))
+ }
}