From 26b7305a232e547709f433a6edf700bf495935d8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 13 Aug 2018 16:57:13 +0200 Subject: Add blacklist reason field --- server/models/video/video-blacklist.ts | 59 +++++++++++++++++++++++++++------- server/models/video/video.ts | 30 ++++++++++++----- 2 files changed, 69 insertions(+), 20 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index 26167174a..1b8a338cb 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts @@ -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 { + @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 { }) 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 { 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 + } } } } diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 39fe21007..f3a900bc9 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -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 { }) 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 { }) } - 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 = { offset: start, limit: count, @@ -777,6 +787,10 @@ export class VideoModel extends Model { { model: ScheduleVideoUpdateModel, required: false + }, + { + model: VideoBlacklistModel, + required: false } ] } @@ -788,12 +802,6 @@ export class VideoModel extends Model { }) } - 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 { 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 { privacy: this.ScheduleVideoUpdate.privacy || undefined } } + + if (options.additionalAttributes.blacklistInfo === true) { + videoObject.blacklisted = !!this.VideoBlacklist + videoObject.blacklistedReason = this.VideoBlacklist ? this.VideoBlacklist.reason : null + } } return videoObject -- cgit v1.2.3