]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/tag.ts
Move typescript utils in its own directory
[github/Chocobozzz/PeerTube.git] / server / models / video / tag.ts
1 import { col, fn, QueryTypes, Transaction } from 'sequelize'
2 import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MTag } from '@server/types/models'
4 import { AttributesOnly } from '@shared/typescript-utils'
5 import { VideoPrivacy, VideoState } from '../../../shared/models/videos'
6 import { isVideoTagValid } from '../../helpers/custom-validators/videos'
7 import { throwIfNotValid } from '../utils'
8 import { VideoModel } from './video'
9 import { VideoTagModel } from './video-tag'
10
11 @Table({
12 tableName: 'tag',
13 timestamps: false,
14 indexes: [
15 {
16 fields: [ 'name' ],
17 unique: true
18 },
19 {
20 name: 'tag_lower_name',
21 fields: [ fn('lower', col('name')) ]
22 }
23 ]
24 })
25 export class TagModel extends Model<Partial<AttributesOnly<TagModel>>> {
26
27 @AllowNull(false)
28 @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag'))
29 @Column
30 name: string
31
32 @CreatedAt
33 createdAt: Date
34
35 @UpdatedAt
36 updatedAt: Date
37
38 @BelongsToMany(() => VideoModel, {
39 foreignKey: 'tagId',
40 through: () => VideoTagModel,
41 onDelete: 'CASCADE'
42 })
43 Videos: VideoModel[]
44
45 static findOrCreateTags (tags: string[], transaction: Transaction): Promise<MTag[]> {
46 if (tags === null) return Promise.resolve([])
47
48 const tasks: Promise<MTag>[] = []
49 tags.forEach(tag => {
50 const query = {
51 where: {
52 name: tag
53 },
54 defaults: {
55 name: tag
56 },
57 transaction
58 }
59
60 const promise = TagModel.findOrCreate<MTag>(query)
61 .then(([ tagInstance ]) => tagInstance)
62 tasks.push(promise)
63 })
64
65 return Promise.all(tasks)
66 }
67
68 // threshold corresponds to how many video the field should have to be returned
69 static getRandomSamples (threshold: number, count: number): Promise<string[]> {
70 const query = 'SELECT tag.name FROM tag ' +
71 'INNER JOIN "videoTag" ON "videoTag"."tagId" = tag.id ' +
72 'INNER JOIN video ON video.id = "videoTag"."videoId" ' +
73 'WHERE video.privacy = $videoPrivacy AND video.state = $videoState ' +
74 'GROUP BY tag.name HAVING COUNT(tag.name) >= $threshold ' +
75 'ORDER BY random() ' +
76 'LIMIT $count'
77
78 const options = {
79 bind: { threshold, count, videoPrivacy: VideoPrivacy.PUBLIC, videoState: VideoState.PUBLISHED },
80 type: QueryTypes.SELECT as QueryTypes.SELECT
81 }
82
83 return TagModel.sequelize.query<{ name: string }>(query, options)
84 .then(data => data.map(d => d.name))
85 }
86 }