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