2019-04-18 05:28:17 -04:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
2019-09-05 04:19:35 -04:00
|
|
|
import { getBlacklistSort, SortType, throwIfNotValid } from '../utils'
|
2019-08-15 05:53:26 -04:00
|
|
|
import { VideoModel } from './video'
|
2019-07-31 09:57:32 -04:00
|
|
|
import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
|
2019-04-02 05:26:47 -04:00
|
|
|
import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist'
|
|
|
|
import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos'
|
2019-04-11 08:26:41 -04:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
|
2019-09-05 04:30:51 -04:00
|
|
|
import { FindOptions } from 'sequelize'
|
2019-05-21 06:09:58 -04:00
|
|
|
import { ThumbnailModel } from './thumbnail'
|
2019-08-15 05:53:26 -04:00
|
|
|
import * as Bluebird from 'bluebird'
|
2019-08-20 13:05:31 -04:00
|
|
|
import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/typings/models'
|
2017-05-22 14:58:25 -04:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@Table({
|
|
|
|
tableName: 'videoBlacklist',
|
|
|
|
indexes: [
|
2017-04-26 15:22:10 -04:00
|
|
|
{
|
2017-12-12 11:53:50 -05:00
|
|
|
fields: [ 'videoId' ],
|
|
|
|
unique: true
|
2017-04-26 15:22:10 -04:00
|
|
|
}
|
2017-05-22 14:58:25 -04:00
|
|
|
]
|
2017-12-12 11:53:50 -05:00
|
|
|
})
|
|
|
|
export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
|
2017-04-26 15:22:10 -04:00
|
|
|
|
2018-08-13 10:57:13 -04:00
|
|
|
@AllowNull(true)
|
2019-04-18 05:28:17 -04:00
|
|
|
@Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true))
|
2018-08-13 10:57:13 -04:00
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
|
|
|
|
reason: string
|
|
|
|
|
2019-01-10 09:39:51 -05:00
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
unfederated: boolean
|
|
|
|
|
2019-04-02 05:26:47 -04:00
|
|
|
@AllowNull(false)
|
|
|
|
@Default(null)
|
|
|
|
@Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
|
|
|
|
@Column
|
|
|
|
type: VideoBlacklistType
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
2017-04-26 15:22:10 -04:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
2017-04-26 15:22:10 -04:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
2017-04-26 15:22:10 -04:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@BelongsTo(() => VideoModel, {
|
2017-07-11 10:01:56 -04:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
2017-12-12 11:53:50 -05:00
|
|
|
onDelete: 'cascade'
|
2017-04-26 15:22:10 -04:00
|
|
|
})
|
2017-12-12 11:53:50 -05:00
|
|
|
Video: VideoModel
|
|
|
|
|
2019-04-02 05:26:47 -04:00
|
|
|
static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) {
|
2019-05-21 06:09:58 -04:00
|
|
|
function buildBaseQuery (): FindOptions {
|
|
|
|
return {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2019-09-04 10:23:37 -04:00
|
|
|
order: getBlacklistSort(sort.sortModel, sort.sortValue)
|
2019-05-21 06:09:58 -04:00
|
|
|
}
|
2017-12-12 11:53:50 -05:00
|
|
|
}
|
2017-04-26 15:22:10 -04:00
|
|
|
|
2019-05-21 06:09:58 -04:00
|
|
|
const countQuery = buildBaseQuery()
|
|
|
|
|
|
|
|
const findQuery = buildBaseQuery()
|
|
|
|
findQuery.include = [
|
|
|
|
{
|
|
|
|
model: VideoModel,
|
|
|
|
required: true,
|
|
|
|
include: [
|
|
|
|
{
|
2019-07-31 09:57:32 -04:00
|
|
|
model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
|
2019-05-21 06:09:58 -04:00
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: ThumbnailModel,
|
|
|
|
attributes: [ 'type', 'filename' ],
|
|
|
|
required: false
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-04-02 05:26:47 -04:00
|
|
|
if (type) {
|
2019-05-21 06:09:58 -04:00
|
|
|
countQuery.where = { type }
|
|
|
|
findQuery.where = { type }
|
2019-04-02 05:26:47 -04:00
|
|
|
}
|
|
|
|
|
2019-05-21 06:09:58 -04:00
|
|
|
return Promise.all([
|
|
|
|
VideoBlacklistModel.count(countQuery),
|
|
|
|
VideoBlacklistModel.findAll(findQuery)
|
|
|
|
]).then(([ count, rows ]) => {
|
|
|
|
return {
|
|
|
|
data: rows,
|
|
|
|
total: count
|
|
|
|
}
|
|
|
|
})
|
2017-04-26 15:22:10 -04:00
|
|
|
}
|
|
|
|
|
2019-08-15 05:53:26 -04:00
|
|
|
static loadByVideoId (id: number): Bluebird<MVideoBlacklist> {
|
2017-12-12 11:53:50 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoId: id
|
|
|
|
}
|
2017-07-05 07:26:25 -04:00
|
|
|
}
|
2017-04-26 15:22:10 -04:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
return VideoBlacklistModel.findOne(query)
|
2017-04-26 15:22:10 -04:00
|
|
|
}
|
|
|
|
|
2019-08-20 13:05:31 -04:00
|
|
|
toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist {
|
2017-12-12 11:53:50 -05:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt,
|
2018-08-13 10:57:13 -04:00
|
|
|
reason: this.reason,
|
2019-01-10 09:39:51 -05:00
|
|
|
unfederated: this.unfederated,
|
2019-04-02 05:26:47 -04:00
|
|
|
type: this.type,
|
2018-08-13 10:57:13 -04:00
|
|
|
|
2019-04-02 05:26:47 -04:00
|
|
|
video: this.Video.toFormattedJSON()
|
2017-12-12 11:53:50 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-26 15:22:10 -04:00
|
|
|
}
|