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