PeerTube/server/models/video/tag.ts

87 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-12-08 14:30:29 +01:00
import { col, fn, QueryTypes, Transaction } from 'sequelize'
2017-12-12 17:53:50 +01:00
import { AllowNull, BelongsToMany, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2020-12-08 14:30:29 +01:00
import { MTag } from '@server/types/models'
import { AttributesOnly } from '@shared/typescript-utils'
2020-12-08 14:30:29 +01:00
import { VideoPrivacy, VideoState } from '../../../shared/models/videos'
2017-12-12 17:53:50 +01:00
import { isVideoTagValid } from '../../helpers/custom-validators/videos'
import { throwIfNotValid } from '../utils'
import { VideoModel } from './video'
import { VideoTagModel } from './video-tag'
@Table({
tableName: 'tag',
timestamps: false,
indexes: [
2016-12-24 16:59:17 +01:00
{
2017-12-12 17:53:50 +01:00
fields: [ 'name' ],
unique: true
2019-08-29 16:47:32 +02:00
},
{
name: 'tag_lower_name',
2021-10-22 16:39:37 +02:00
fields: [ fn('lower', col('name')) ]
2016-12-24 16:59:17 +01:00
}
2017-05-22 20:58:25 +02:00
]
2017-12-12 17:53:50 +01:00
})
2021-05-12 14:09:04 +02:00
export class TagModel extends Model<Partial<AttributesOnly<TagModel>>> {
2017-05-22 20:58:25 +02:00
2017-12-12 17:53:50 +01:00
@AllowNull(false)
@Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag'))
@Column
name: string
2016-12-24 16:59:17 +01:00
2017-12-12 17:53:50 +01:00
@CreatedAt
createdAt: Date
2016-12-24 16:59:17 +01:00
2017-12-12 17:53:50 +01:00
@UpdatedAt
updatedAt: Date
@BelongsToMany(() => VideoModel, {
2016-12-24 16:59:17 +01:00
foreignKey: 'tagId',
2017-12-12 17:53:50 +01:00
through: () => VideoTagModel,
onDelete: 'CASCADE'
2016-12-24 16:59:17 +01:00
})
2017-12-12 17:53:50 +01:00
Videos: VideoModel[]
2019-08-20 10:22:05 +02:00
static findOrCreateTags (tags: string[], transaction: Transaction): Promise<MTag[]> {
if (tags === null) return Promise.resolve([])
2018-05-16 09:28:18 +02:00
2020-12-08 14:30:29 +01:00
const tasks: Promise<MTag>[] = []
2017-12-12 17:53:50 +01:00
tags.forEach(tag => {
const query = {
where: {
name: tag
},
defaults: {
name: tag
},
transaction
2016-12-29 18:02:03 +01:00
}
2019-08-20 10:22:05 +02:00
const promise = TagModel.findOrCreate<MTag>(query)
2017-12-12 17:53:50 +01:00
.then(([ tagInstance ]) => tagInstance)
tasks.push(promise)
})
2017-12-12 17:53:50 +01:00
return Promise.all(tasks)
}
2018-08-30 14:58:00 +02:00
// threshold corresponds to how many video the field should have to be returned
2020-12-08 14:30:29 +01:00
static getRandomSamples (threshold: number, count: number): Promise<string[]> {
2018-08-30 14:58:00 +02:00
const query = 'SELECT tag.name FROM tag ' +
'INNER JOIN "videoTag" ON "videoTag"."tagId" = tag.id ' +
'INNER JOIN video ON video.id = "videoTag"."videoId" ' +
'WHERE video.privacy = $videoPrivacy AND video.state = $videoState ' +
'GROUP BY tag.name HAVING COUNT(tag.name) >= $threshold ' +
'ORDER BY random() ' +
'LIMIT $count'
const options = {
bind: { threshold, count, videoPrivacy: VideoPrivacy.PUBLIC, videoState: VideoState.PUBLISHED },
2019-04-18 11:28:17 +02:00
type: QueryTypes.SELECT as QueryTypes.SELECT
2018-08-30 14:58:00 +02:00
}
2019-04-23 09:50:57 +02:00
return TagModel.sequelize.query<{ name: string }>(query, options)
2018-08-30 14:58:00 +02:00
.then(data => data.map(d => d.name))
}
2016-12-29 18:02:03 +01:00
}