X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-abuse.ts;h=39f0c2cb2c6ae605e733d8d70b8762b4f110f0c9;hb=eacb25c4366bcc8fba20f98f93f004fabc6d5578;hp=e8f4f9a67e53c96eb6d3977263f2dccb5494f87e;hpb=21e0727a84734cb0c81c1c9bb22a49b13e46fe5f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-abuse.ts b/server/models/video/video-abuse.ts index e8f4f9a67..39f0c2cb2 100644 --- a/server/models/video/video-abuse.ts +++ b/server/models/video/video-abuse.ts @@ -1,142 +1,108 @@ -import * as Sequelize from 'sequelize' - -import { CONFIG } from '../../initializers' -import { isVideoAbuseReasonValid } from '../../helpers' - -import { addMethodsToModel, getSort } from '../utils' -import { - VideoAbuseInstance, - VideoAbuseAttributes, - - VideoAbuseMethods -} from './video-abuse-interface' -import { VideoAbuseObject } from '../../../shared/models/activitypub/objects/video-abuse-object' - -let VideoAbuse: Sequelize.Model -let toFormattedJSON: VideoAbuseMethods.ToFormattedJSON -let listForApi: VideoAbuseMethods.ListForApi -let toActivityPubObject: VideoAbuseMethods.ToActivityPubObject - -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: [ { - reason: { - type: DataTypes.STRING, - allowNull: false, - validate: { - reasonValid: value => { - const res = isVideoAbuseReasonValid(value) - if (res === false) throw new Error('Video abuse reason is not valid.') - } - } - } + fields: [ 'videoId' ] }, { - indexes: [ - { - fields: [ 'videoId' ] - }, - { - fields: [ 'reporterAccountId' ] - } - ] + fields: [ 'reporterAccountId' ] } - ) - - const classMethods = [ - associate, - - listForApi - ] - const instanceMethods = [ - toFormattedJSON, - toActivityPubObject ] - addMethodsToModel(VideoAbuse, classMethods, instanceMethods) +}) +export class VideoAbuseModel extends Model { - return VideoAbuse -} - -// ------------------------------ METHODS ------------------------------ + @AllowNull(false) + @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason')) + @Column + reason: string -toFormattedJSON = function (this: VideoAbuseInstance) { - let reporterServerHost + @CreatedAt + createdAt: Date - if (this.Account.Server) { - reporterServerHost = this.Account.Server.host - } else { - // It means it's our video - reporterServerHost = CONFIG.WEBSERVER.HOST - } + @UpdatedAt + updatedAt: Date - const json = { - id: this.id, - reason: this.reason, - reporterUsername: this.Account.name, - reporterServerHost, - videoId: this.Video.id, - videoUUID: this.Video.uuid, - videoName: this.Video.name, - createdAt: this.createdAt - } - - return json -} - -toActivityPubObject = function (this: VideoAbuseInstance) { - const videoAbuseObject: VideoAbuseObject = { - type: 'Flag' as 'Flag', - content: this.reason, - object: this.Video.url - } + @ForeignKey(() => AccountModel) + @Column + reporterAccountId: number - return videoAbuseObject -} - -// ------------------------------ STATICS ------------------------------ - -function associate (models) { - VideoAbuse.belongsTo(models.Account, { + @BelongsTo(() => AccountModel, { foreignKey: { - name: 'reporterAccountId', - allowNull: true + allowNull: false }, - onDelete: 'CASCADE' + onDelete: 'cascade' }) + Account: AccountModel + + @ForeignKey(() => VideoModel) + @Column + videoId: number - VideoAbuse.belongsTo(models.Video, { + @BelongsTo(() => VideoModel, { foreignKey: { - name: 'videoId', allowNull: false }, - onDelete: 'CASCADE' + 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.Account, - required: true, - include: [ - { - model: VideoAbuse['sequelize'].models.Server, - required: false - } - ] + @AfterCreate + static sendEmailNotification (instance: VideoAbuseModel) { + return Emailer.Instance.addVideoAbuseReportJob(instance.videoId) + } + + 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 }, - { - model: VideoAbuse['sequelize'].models.Video, - required: true - } - ] + createdAt: this.createdAt + } } - return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => { - return { total: count, data: rows } - }) + toActivityPubObject (): VideoAbuseObject { + return { + type: 'Flag' as 'Flag', + content: this.reason, + object: this.Video.url + } + } }