]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/tag.ts
add user account email verificiation (#977)
[github/Chocobozzz/PeerTube.git] / server / models / video / tag.ts
CommitLineData
3fd3ab2d
C
1import * as Bluebird from 'bluebird'
2import { Transaction } from 'sequelize'
3import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
4import { isVideoTagValid } from '../../helpers/custom-validators/videos'
5import { throwIfNotValid } from '../utils'
6import { VideoModel } from './video'
7import { VideoTagModel } from './video-tag'
8
9@Table({
10 tableName: 'tag',
11 timestamps: false,
12 indexes: [
7920c273 13 {
3fd3ab2d
C
14 fields: [ 'name' ],
15 unique: true
7920c273 16 }
e02643f3 17 ]
3fd3ab2d
C
18})
19export class TagModel extends Model<TagModel> {
e02643f3 20
3fd3ab2d
C
21 @AllowNull(false)
22 @Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag'))
23 @Column
24 name: string
7920c273 25
3fd3ab2d
C
26 @CreatedAt
27 createdAt: Date
7920c273 28
3fd3ab2d
C
29 @UpdatedAt
30 updatedAt: Date
31
32 @BelongsToMany(() => VideoModel, {
7920c273 33 foreignKey: 'tagId',
3fd3ab2d 34 through: () => VideoTagModel,
0a6658fd 35 onDelete: 'CASCADE'
7920c273 36 })
3fd3ab2d
C
37 Videos: VideoModel[]
38
39 static findOrCreateTags (tags: string[], transaction: Transaction) {
2efd32f6
C
40 if (tags === null) return []
41
3fd3ab2d
C
42 const tasks: Bluebird<TagModel>[] = []
43 tags.forEach(tag => {
44 const query = {
45 where: {
46 name: tag
47 },
48 defaults: {
49 name: tag
50 }
4ff0d862 51 }
4ff0d862 52
3fd3ab2d 53 if (transaction) query['transaction'] = transaction
4ff0d862 54
3fd3ab2d
C
55 const promise = TagModel.findOrCreate(query)
56 .then(([ tagInstance ]) => tagInstance)
57 tasks.push(promise)
58 })
6fcd19ba 59
3fd3ab2d
C
60 return Promise.all(tasks)
61 }
4ff0d862 62}