2018-08-13 09:57:13 -05:00
|
|
|
import {
|
|
|
|
AfterCreate,
|
|
|
|
AfterDestroy,
|
|
|
|
AllowNull,
|
|
|
|
BelongsTo,
|
|
|
|
Column,
|
2018-08-14 08:28:30 -05:00
|
|
|
CreatedAt,
|
|
|
|
DataType,
|
2018-08-13 09:57:13 -05:00
|
|
|
ForeignKey,
|
|
|
|
Is,
|
|
|
|
Model,
|
|
|
|
Table,
|
|
|
|
UpdatedAt
|
|
|
|
} from 'sequelize-typescript'
|
2018-08-14 08:28:30 -05:00
|
|
|
import { getSortOnModel, SortType, throwIfNotValid } from '../utils'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoModel } from './video'
|
2018-08-13 09:57:13 -05:00
|
|
|
import { isVideoBlacklistReasonValid } from '../../helpers/custom-validators/video-blacklist'
|
|
|
|
import { Emailer } from '../../lib/emailer'
|
2018-08-14 02:08:47 -05:00
|
|
|
import { VideoBlacklist } from '../../../shared/models/videos'
|
2018-08-13 09:57:13 -05:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../initializers'
|
2017-05-22 13:58:25 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@Table({
|
|
|
|
tableName: 'videoBlacklist',
|
|
|
|
indexes: [
|
2017-04-26 14:22:10 -05:00
|
|
|
{
|
2017-12-12 10:53:50 -06:00
|
|
|
fields: [ 'videoId' ],
|
|
|
|
unique: true
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|
2017-05-22 13:58:25 -05:00
|
|
|
]
|
2017-12-12 10:53:50 -06:00
|
|
|
})
|
|
|
|
export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
|
2017-04-26 14:22:10 -05:00
|
|
|
|
2018-08-13 09:57:13 -05:00
|
|
|
@AllowNull(true)
|
|
|
|
@Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason'))
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
|
|
|
|
reason: string
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
2017-04-26 14:22:10 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
2017-04-26 14:22:10 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
2017-04-26 14:22:10 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@BelongsTo(() => VideoModel, {
|
2017-07-11 09:01:56 -05:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
2017-12-12 10:53:50 -06:00
|
|
|
onDelete: 'cascade'
|
2017-04-26 14:22:10 -05:00
|
|
|
})
|
2017-12-12 10:53:50 -06:00
|
|
|
Video: VideoModel
|
|
|
|
|
|
|
|
static listForApi (start: number, count: number, sort: SortType) {
|
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2018-02-19 02:41:03 -06:00
|
|
|
order: getSortOnModel(sort.sortModel, sort.sortValue),
|
2018-08-14 02:08:47 -05:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: VideoModel,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
2017-12-12 10:53:50 -06:00
|
|
|
}
|
2017-04-26 14:22:10 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
return VideoBlacklistModel.findAndCountAll(query)
|
|
|
|
.then(({ rows, count }) => {
|
|
|
|
return {
|
|
|
|
data: rows,
|
|
|
|
total: count
|
|
|
|
}
|
|
|
|
})
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static loadByVideoId (id: number) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoId: id
|
|
|
|
}
|
2017-07-05 06:26:25 -05:00
|
|
|
}
|
2017-04-26 14:22:10 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
return VideoBlacklistModel.findOne(query)
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|
|
|
|
|
2018-08-14 02:08:47 -05:00
|
|
|
toFormattedJSON (): VideoBlacklist {
|
2017-12-12 10:53:50 -06:00
|
|
|
const video = this.Video
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt,
|
2018-08-13 09:57:13 -05:00
|
|
|
reason: this.reason,
|
|
|
|
|
|
|
|
video: {
|
|
|
|
id: video.id,
|
|
|
|
name: video.name,
|
|
|
|
uuid: video.uuid,
|
|
|
|
description: video.description,
|
|
|
|
duration: video.duration,
|
|
|
|
views: video.views,
|
|
|
|
likes: video.likes,
|
|
|
|
dislikes: video.dislikes,
|
|
|
|
nsfw: video.nsfw
|
|
|
|
}
|
2017-12-12 10:53:50 -06:00
|
|
|
}
|
|
|
|
}
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|