X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-abuse.ts;h=39f0c2cb2c6ae605e733d8d70b8762b4f110f0c9;hb=eacb25c4366bcc8fba20f98f93f004fabc6d5578;hp=ab1a3ea7d393d6b3970e7e1b8a9ffdbee70209a4;hpb=6fcd19ba737f1f5614a56c6925adb882dea43b8d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-abuse.ts b/server/models/video/video-abuse.ts index ab1a3ea7d..39f0c2cb2 100644 --- a/server/models/video/video-abuse.ts +++ b/server/models/video/video-abuse.ts @@ -1,127 +1,108 @@ -import * as Sequelize from 'sequelize' - -import { CONFIG } from '../../initializers' -import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../../helpers' - -import { addMethodsToModel, getSort } from '../utils' -import { - VideoAbuseInstance, - VideoAbuseAttributes, - - VideoAbuseMethods -} from './video-abuse-interface' - -let VideoAbuse: Sequelize.Model -let toFormatedJSON: VideoAbuseMethods.ToFormatedJSON -let listForApi: VideoAbuseMethods.ListForApi - -export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { - VideoAbuse = sequelize.define('VideoAbuse', +import { AfterCreate, AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { VideoAbuseObject } from '../../../shared/models/activitypub/objects' +import { VideoAbuse } from '../../../shared/models/videos' +import { isVideoAbuseReasonValid } from '../../helpers/custom-validators/videos' +import { Emailer } from '../../lib/emailer' +import { AccountModel } from '../account/account' +import { getSort, throwIfNotValid } from '../utils' +import { VideoModel } from './video' + +@Table({ + tableName: 'videoAbuse', + indexes: [ { - reporterUsername: { - type: DataTypes.STRING, - allowNull: false, - validate: { - reporterUsernameValid: function (value) { - const res = isVideoAbuseReporterUsernameValid(value) - if (res === false) throw new Error('Video abuse reporter username is not valid.') - } - } - }, - reason: { - type: DataTypes.STRING, - allowNull: false, - validate: { - reasonValid: function (value) { - const res = isVideoAbuseReasonValid(value) - if (res === false) throw new Error('Video abuse reason is not valid.') - } - } - } + fields: [ 'videoId' ] }, { - indexes: [ - { - fields: [ 'videoId' ] - }, - { - fields: [ 'reporterPodId' ] - } - ] + fields: [ 'reporterAccountId' ] } - ) - - const classMethods = [ - associate, - - listForApi - ] - const instanceMethods = [ - toFormatedJSON ] - addMethodsToModel(VideoAbuse, classMethods, instanceMethods) +}) +export class VideoAbuseModel extends Model { - return VideoAbuse -} - -// ------------------------------ METHODS ------------------------------ - -toFormatedJSON = function (this: VideoAbuseInstance) { - let reporterPodHost - - if (this.Pod) { - reporterPodHost = this.Pod.host - } else { - // It means it's our video - reporterPodHost = CONFIG.WEBSERVER.HOST - } + @AllowNull(false) + @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason')) + @Column + reason: string - const json = { - id: this.id, - reporterPodHost, - reason: this.reason, - reporterUsername: this.reporterUsername, - videoId: this.videoId, - createdAt: this.createdAt - } + @CreatedAt + createdAt: Date - return json -} + @UpdatedAt + updatedAt: Date -// ------------------------------ STATICS ------------------------------ + @ForeignKey(() => AccountModel) + @Column + reporterAccountId: number -function associate (models) { - VideoAbuse.belongsTo(models.Pod, { + @BelongsTo(() => AccountModel, { foreignKey: { - name: 'reporterPodId', - allowNull: true + allowNull: false }, onDelete: 'cascade' }) + Account: AccountModel - VideoAbuse.belongsTo(models.Video, { + @ForeignKey(() => VideoModel) + @Column + videoId: number + + @BelongsTo(() => VideoModel, { foreignKey: { - name: 'videoId', allowNull: false }, onDelete: 'cascade' }) -} + Video: VideoModel -listForApi = function (start: number, count: number, sort: string) { - const query = { - offset: start, - limit: count, - order: [ getSort(sort) ], - include: [ - { - model: VideoAbuse['sequelize'].models.Pod, - required: false - } - ] + @AfterCreate + static sendEmailNotification (instance: VideoAbuseModel) { + return Emailer.Instance.addVideoAbuseReportJob(instance.videoId) } - return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => { - return { total: count, data: rows } - }) + static listForApi (start: number, count: number, sort: string) { + const query = { + offset: start, + limit: count, + order: getSort(sort), + include: [ + { + model: AccountModel, + required: true + }, + { + model: VideoModel, + required: true + } + ] + } + + return VideoAbuseModel.findAndCountAll(query) + .then(({ rows, count }) => { + return { total: count, data: rows } + }) + } + + toFormattedJSON (): VideoAbuse { + return { + id: this.id, + reason: this.reason, + reporterAccount: this.Account.toFormattedJSON(), + video: { + id: this.Video.id, + uuid: this.Video.uuid, + url: this.Video.url, + name: this.Video.name + }, + createdAt: this.createdAt + } + } + + toActivityPubObject (): VideoAbuseObject { + return { + type: 'Flag' as 'Flag', + content: this.reason, + object: this.Video.url + } + } }