]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/tag.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / video / tag.ts
index ed8df8b48aa84367003161035dda9256eac13c76..653b9694b8a6adef8a3c68cfb8b1822e6c2667c3 100644 (file)
@@ -1,12 +1,12 @@
-import * as Bluebird from 'bluebird'
-import { fn, QueryTypes, Transaction, col } 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'
-import { MTag } from '@server/typings/models'
 
 @Table({
   tableName: 'tag',
@@ -18,11 +18,11 @@ import { MTag } from '@server/typings/models'
     },
     {
       name: 'tag_lower_name',
-      fields: [ fn('lower', col('name')) ] as any // FIXME: typings
+      fields: [ fn('lower', col('name')) ]
     }
   ]
 })
-export class TagModel extends Model<TagModel> {
+export class TagModel extends Model<Partial<AttributesOnly<TagModel>>> {
 
   @AllowNull(false)
   @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag'))
@@ -45,8 +45,9 @@ export class TagModel extends Model<TagModel> {
   static findOrCreateTags (tags: string[], transaction: Transaction): Promise<MTag[]> {
     if (tags === null) return Promise.resolve([])
 
-    const tasks: Bluebird<MTag>[] = []
-    tags.forEach(tag => {
+    const uniqueTags = new Set(tags)
+
+    const tasks = Array.from(uniqueTags).map(tag => {
       const query = {
         where: {
           name: tag
@@ -57,16 +58,15 @@ export class TagModel extends Model<TagModel> {
         transaction
       }
 
-      const promise = TagModel.findOrCreate<MTag>(query)
+      return TagModel.findOrCreate<MTag>(query)
         .then(([ tagInstance ]) => tagInstance)
-      tasks.push(promise)
     })
 
     return Promise.all(tasks)
   }
 
   // threshold corresponds to how many video the field should have to be returned
-  static getRandomSamples (threshold: number, count: number): Bluebird<string[]> {
+  static getRandomSamples (threshold: number, count: number): Promise<string[]> {
     const query = 'SELECT tag.name FROM tag ' +
       'INNER JOIN "videoTag" ON "videoTag"."tagId" = tag.id ' +
       'INNER JOIN video ON video.id = "videoTag"."videoId" ' +