]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/tag.ts
Add import.video.torrent configuration
[github/Chocobozzz/PeerTube.git] / server / models / video / tag.ts
index 2992da56db8f92cc6eed4ab7cb50cfd96d48bc09..6d79a55756ad212f84977a383be0757525d950dd 100644 (file)
@@ -1,73 +1,62 @@
-import * as Sequelize from 'sequelize'
-import * as Promise from 'bluebird'
-
-import { addMethodsToModel } from '../utils'
-import {
-  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',
+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: [
     {
-      name: {
-        type: DataTypes.STRING,
-        allowNull: false
-      }
-    },
-    {
-      timestamps: false,
-      indexes: [
-        {
-          fields: [ 'name' ],
-          unique: true
-        }
-      ]
+      fields: [ 'name' ],
+      unique: true
     }
-  )
-
-  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
 
-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) {
-  const tasks: Promise<TagInstance>[] = []
-  tags.forEach(tag => {
-    const query: Sequelize.FindOrInitializeOptions<TagAttributes> = {
-      where: {
-        name: tag
-      },
-      defaults: {
-        name: tag
+  Videos: VideoModel[]
+
+  static findOrCreateTags (tags: string[], transaction: Transaction) {
+    if (tags === null) return []
+
+    const tasks: Bluebird<TagModel>[] = []
+    tags.forEach(tag => {
+      const query = {
+        where: {
+          name: tag
+        },
+        defaults: {
+          name: tag
+        }
       }
-    }
 
-    if (transaction) query.transaction = transaction
+      if (transaction) query['transaction'] = transaction
 
-    const promise = Tag.findOrCreate(query).then(([ tagInstance ]) => tagInstance)
-    tasks.push(promise)
-  })
+      const promise = TagModel.findOrCreate(query)
+        .then(([ tagInstance ]) => tagInstance)
+      tasks.push(promise)
+    })
 
-  return Promise.all(tasks)
+    return Promise.all(tasks)
+  }
 }