]>
Commit | Line | Data |
---|---|---|
b49f22d8 | 1 | import { col, fn, QueryTypes, Transaction } from 'sequelize' |
3fd3ab2d | 2 | import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
b49f22d8 | 3 | import { MTag } from '@server/types/models' |
6b5f72be | 4 | import { AttributesOnly } from '@shared/typescript-utils' |
b49f22d8 | 5 | import { VideoPrivacy, VideoState } from '../../../shared/models/videos' |
3fd3ab2d | 6 | import { isVideoTagValid } from '../../helpers/custom-validators/videos' |
8c4bbd94 | 7 | import { throwIfNotValid } from '../shared' |
3fd3ab2d C |
8 | import { VideoModel } from './video' |
9 | import { VideoTagModel } from './video-tag' | |
10 | ||
11 | @Table({ | |
12 | tableName: 'tag', | |
13 | timestamps: false, | |
14 | indexes: [ | |
7920c273 | 15 | { |
3fd3ab2d C |
16 | fields: [ 'name' ], |
17 | unique: true | |
4b1f1b81 C |
18 | }, |
19 | { | |
20 | name: 'tag_lower_name', | |
0c691a18 | 21 | fields: [ fn('lower', col('name')) ] |
7920c273 | 22 | } |
e02643f3 | 23 | ] |
3fd3ab2d | 24 | }) |
16c016e8 | 25 | export class TagModel extends Model<Partial<AttributesOnly<TagModel>>> { |
e02643f3 | 26 | |
3fd3ab2d C |
27 | @AllowNull(false) |
28 | @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag')) | |
29 | @Column | |
30 | name: string | |
7920c273 | 31 | |
3fd3ab2d C |
32 | @CreatedAt |
33 | createdAt: Date | |
7920c273 | 34 | |
3fd3ab2d C |
35 | @UpdatedAt |
36 | updatedAt: Date | |
37 | ||
38 | @BelongsToMany(() => VideoModel, { | |
7920c273 | 39 | foreignKey: 'tagId', |
3fd3ab2d | 40 | through: () => VideoTagModel, |
0a6658fd | 41 | onDelete: 'CASCADE' |
7920c273 | 42 | }) |
3fd3ab2d C |
43 | Videos: VideoModel[] |
44 | ||
96ca24f0 C |
45 | static findOrCreateTags (tags: string[], transaction: Transaction): Promise<MTag[]> { |
46 | if (tags === null) return Promise.resolve([]) | |
2efd32f6 | 47 | |
f2623feb C |
48 | const uniqueTags = new Set(tags) |
49 | ||
50 | const tasks = Array.from(uniqueTags).map(tag => { | |
3fd3ab2d C |
51 | const query = { |
52 | where: { | |
53 | name: tag | |
54 | }, | |
55 | defaults: { | |
56 | name: tag | |
d9bdd007 C |
57 | }, |
58 | transaction | |
4ff0d862 | 59 | } |
4ff0d862 | 60 | |
f2623feb | 61 | return TagModel.findOrCreate<MTag>(query) |
3fd3ab2d | 62 | .then(([ tagInstance ]) => tagInstance) |
3fd3ab2d | 63 | }) |
6fcd19ba | 64 | |
3fd3ab2d C |
65 | return Promise.all(tasks) |
66 | } | |
2d3741d6 C |
67 | |
68 | // threshold corresponds to how many video the field should have to be returned | |
b49f22d8 | 69 | static getRandomSamples (threshold: number, count: number): Promise<string[]> { |
2d3741d6 C |
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 }, | |
1735c825 | 80 | type: QueryTypes.SELECT as QueryTypes.SELECT |
2d3741d6 C |
81 | } |
82 | ||
3acc5084 | 83 | return TagModel.sequelize.query<{ name: string }>(query, options) |
2d3741d6 C |
84 | .then(data => data.map(d => d.name)) |
85 | } | |
4ff0d862 | 86 | } |