Add blacklist reason field

This commit is contained in:
Chocobozzz
2018-08-13 16:57:13 +02:00
parent efc9e8450a
commit 26b7305a23
35 changed files with 689 additions and 163 deletions

View File

@@ -1,7 +1,23 @@
import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
import {
AfterCreate,
AfterDestroy,
AllowNull,
BelongsTo,
Column,
CreatedAt, DataType,
ForeignKey,
Is,
Model,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { SortType } from '../../helpers/utils'
import { getSortOnModel } from '../utils'
import { getSortOnModel, throwIfNotValid } from '../utils'
import { VideoModel } from './video'
import { isVideoBlacklistReasonValid } from '../../helpers/custom-validators/video-blacklist'
import { Emailer } from '../../lib/emailer'
import { BlacklistedVideo } from '../../../shared/models/videos'
import { CONSTRAINTS_FIELDS } from '../../initializers'
@Table({
tableName: 'videoBlacklist',
@@ -14,6 +30,11 @@ import { VideoModel } from './video'
})
export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
@AllowNull(true)
@Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
reason: string
@CreatedAt
createdAt: Date
@@ -32,6 +53,16 @@ export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
})
Video: VideoModel
@AfterCreate
static sendBlacklistEmailNotification (instance: VideoBlacklistModel) {
return Emailer.Instance.addVideoBlacklistReportJob(instance.videoId, instance.reason)
}
@AfterDestroy
static sendUnblacklistEmailNotification (instance: VideoBlacklistModel) {
return Emailer.Instance.addVideoUnblacklistReportJob(instance.videoId)
}
static listForApi (start: number, count: number, sort: SortType) {
const query = {
offset: start,
@@ -59,22 +90,26 @@ export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
return VideoBlacklistModel.findOne(query)
}
toFormattedJSON () {
toFormattedJSON (): BlacklistedVideo {
const video = this.Video
return {
id: this.id,
videoId: this.videoId,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
name: video.name,
uuid: video.uuid,
description: video.description,
duration: video.duration,
views: video.views,
likes: video.likes,
dislikes: video.dislikes,
nsfw: video.nsfw
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
}
}
}
}

View File

@@ -93,6 +93,7 @@ import { VideoShareModel } from './video-share'
import { VideoTagModel } from './video-tag'
import { ScheduleVideoUpdateModel } from './schedule-video-update'
import { VideoCaptionModel } from './video-caption'
import { VideoBlacklistModel } from './video-blacklist'
// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
const indexes: Sequelize.DefineIndexesOptions[] = [
@@ -581,6 +582,15 @@ export class VideoModel extends Model<VideoModel> {
})
ScheduleVideoUpdate: ScheduleVideoUpdateModel
@HasOne(() => VideoBlacklistModel, {
foreignKey: {
name: 'videoId',
allowNull: false
},
onDelete: 'cascade'
})
VideoBlacklist: VideoBlacklistModel
@HasMany(() => VideoCaptionModel, {
foreignKey: {
name: 'videoId',
@@ -755,7 +765,7 @@ export class VideoModel extends Model<VideoModel> {
})
}
static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, hideNSFW: boolean, withFiles = false) {
static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) {
const query: IFindOptions<VideoModel> = {
offset: start,
limit: count,
@@ -777,6 +787,10 @@ export class VideoModel extends Model<VideoModel> {
{
model: ScheduleVideoUpdateModel,
required: false
},
{
model: VideoBlacklistModel,
required: false
}
]
}
@@ -788,12 +802,6 @@ export class VideoModel extends Model<VideoModel> {
})
}
if (hideNSFW === true) {
query.where = {
nsfw: false
}
}
return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
return {
data: rows,
@@ -1177,7 +1185,8 @@ export class VideoModel extends Model<VideoModel> {
additionalAttributes: {
state?: boolean,
waitTranscoding?: boolean,
scheduledUpdate?: boolean
scheduledUpdate?: boolean,
blacklistInfo?: boolean
}
}): Video {
const formattedAccount = this.VideoChannel.Account.toFormattedJSON()
@@ -1254,6 +1263,11 @@ export class VideoModel extends Model<VideoModel> {
privacy: this.ScheduleVideoUpdate.privacy || undefined
}
}
if (options.additionalAttributes.blacklistInfo === true) {
videoObject.blacklisted = !!this.VideoBlacklist
videoObject.blacklistedReason = this.VideoBlacklist ? this.VideoBlacklist.reason : null
}
}
return videoObject