X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Ftag.ts;h=6d79a55756ad212f84977a383be0757525d950dd;hb=2efd32f697549c59337db5177a93749af8e605d8;hp=3c657d7517961d71a0aec962c96f4e96668385b8;hpb=74889a71fe687dda74f2a687653122327807af36;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/tag.ts b/server/models/video/tag.ts index 3c657d751..6d79a5575 100644 --- a/server/models/video/tag.ts +++ b/server/models/video/tag.ts @@ -1,81 +1,62 @@ -import { each } from 'async' -import * as Sequelize from 'sequelize' - -import { addMethodsToModel } from '../utils' -import { - TagClass, - TagInstance, - TagAttributes, - - TagMethods -} from './tag-interface' - -let Tag: Sequelize.Model -let findOrCreateTags: TagMethods.FindOrCreateTags - -export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { - Tag = sequelize.define('Tag', - { - name: { - type: DataTypes.STRING, - allowNull: false - } - }, +import * as Bluebird from 'bluebird' +import { Transaction } 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' + +@Table({ + tableName: 'tag', + timestamps: false, + indexes: [ { - timestamps: false, - indexes: [ - { - fields: [ 'name' ], - unique: true - } - ] + fields: [ 'name' ], + unique: true } - ) - - const classMethods = [ - associate, - - findOrCreateTags ] - addMethodsToModel(Tag, classMethods) +}) +export class TagModel extends Model { - return Tag -} + @AllowNull(false) + @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag')) + @Column + name: string -// --------------------------------------------------------------------------- + @CreatedAt + createdAt: Date -function associate (models) { - Tag.belongsToMany(models.Video, { + @UpdatedAt + updatedAt: Date + + @BelongsToMany(() => VideoModel, { foreignKey: 'tagId', - through: models.VideoTag, - onDelete: 'cascade' + through: () => VideoTagModel, + onDelete: 'CASCADE' }) -} - -findOrCreateTags = function (tags: string[], transaction: Sequelize.Transaction, callback: TagMethods.FindOrCreateTagsCallback) { - const tagInstances = [] - - each(tags, function (tag, callbackEach) { - const query: any = { - where: { - name: tag - }, - defaults: { - name: tag + Videos: VideoModel[] + + static findOrCreateTags (tags: string[], transaction: Transaction) { + if (tags === null) return [] + + const tasks: Bluebird[] = [] + tags.forEach(tag => { + const query = { + where: { + name: tag + }, + defaults: { + name: tag + } } - } - - if (transaction) query.transaction = transaction - Tag.findOrCreate(query).asCallback(function (err, res) { - if (err) return callbackEach(err) + if (transaction) query['transaction'] = transaction - // res = [ tag, isCreated ] - const tag = res[0] - tagInstances.push(tag) - return callbackEach() + const promise = TagModel.findOrCreate(query) + .then(([ tagInstance ]) => tagInstance) + tasks.push(promise) }) - }, function (err) { - return callback(err, tagInstances) - }) + + return Promise.all(tasks) + } }