]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/tag.ts
Support video views/viewers stats in server
[github/Chocobozzz/PeerTube.git] / server / models / video / tag.ts
index 0fc3cfd4cfae9f8bac8c09c81a899c73ea965401..7900e070dd8a0dfa4205859b20fb081fc1dc49ed 100644 (file)
@@ -1,11 +1,12 @@
-import * as Bluebird from 'bluebird'
-import { QueryTypes, Transaction } 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'
 
 @Table({
   tableName: 'tag',
@@ -14,10 +15,14 @@ import { VideoPrivacy, VideoState } from '../../../shared/models/videos'
     {
       fields: [ 'name' ],
       unique: true
+    },
+    {
+      name: 'tag_lower_name',
+      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'))
@@ -37,10 +42,10 @@ export class TagModel extends Model<TagModel> {
   })
   Videos: VideoModel[]
 
-  static findOrCreateTags (tags: string[], transaction: Transaction) {
-    if (tags === null) return []
+  static findOrCreateTags (tags: string[], transaction: Transaction): Promise<MTag[]> {
+    if (tags === null) return Promise.resolve([])
 
-    const tasks: Bluebird<TagModel>[] = []
+    const tasks: Promise<MTag>[] = []
     tags.forEach(tag => {
       const query = {
         where: {
@@ -52,7 +57,7 @@ export class TagModel extends Model<TagModel> {
         transaction
       }
 
-      const promise = TagModel.findOrCreate(query)
+      const promise = TagModel.findOrCreate<MTag>(query)
         .then(([ tagInstance ]) => tagInstance)
       tasks.push(promise)
     })
@@ -61,7 +66,7 @@ export class TagModel extends Model<TagModel> {
   }
 
   // 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" ' +