]> 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 d04205703b7a12b1121a38e571b09cfebc9f6504..653b9694b8a6adef8a3c68cfb8b1822e6c2667c3 100644 (file)
@@ -1,6 +1,7 @@
 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'
@@ -17,11 +18,11 @@ import { VideoTagModel } from './video-tag'
     },
     {
       name: 'tag_lower_name',
-      fields: [ fn('lower', col('name')) ] as any // FIXME: typings
+      fields: [ fn('lower', col('name')) ]
     }
   ]
 })
-export class TagModel extends Model {
+export class TagModel extends Model<Partial<AttributesOnly<TagModel>>> {
 
   @AllowNull(false)
   @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag'))
@@ -44,8 +45,9 @@ export class TagModel extends Model {
   static findOrCreateTags (tags: string[], transaction: Transaction): Promise<MTag[]> {
     if (tags === null) return Promise.resolve([])
 
-    const tasks: Promise<MTag>[] = []
-    tags.forEach(tag => {
+    const uniqueTags = new Set(tags)
+
+    const tasks = Array.from(uniqueTags).map(tag => {
       const query = {
         where: {
           name: tag
@@ -56,9 +58,8 @@ export class TagModel extends Model {
         transaction
       }
 
-      const promise = TagModel.findOrCreate<MTag>(query)
+      return TagModel.findOrCreate<MTag>(query)
         .then(([ tagInstance ]) => tagInstance)
-      tasks.push(promise)
     })
 
     return Promise.all(tasks)