X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-abuse.ts;h=cc7078ae7966541ecea1c3bb79a542af19fc178c;hb=34cbef8c6cc912143a421413bdd832c4adcc556a;hp=df92609c05731a945906eb9d1ce3c4af593a76fa;hpb=075f16caac5236cb04c98ae7b3a989766d764bb3;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-abuse.ts b/server/models/video/video-abuse.ts index df92609c0..cc7078ae7 100644 --- a/server/models/video/video-abuse.ts +++ b/server/models/video/video-abuse.ts @@ -1,127 +1,115 @@ -import * as Sequelize from 'sequelize' - +import { AfterCreate, AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { VideoAbuseObject } from '../../../shared/models/activitypub/objects' +import { isVideoAbuseReasonValid } from '../../helpers/custom-validators/videos' 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 { 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: 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: 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) - - return VideoAbuse -} +}) +export class VideoAbuseModel extends Model { -// ------------------------------ METHODS ------------------------------ + @AllowNull(false) + @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason')) + @Column + reason: string -toFormatedJSON = function (this: VideoAbuseInstance) { - let reporterPodHost + @CreatedAt + createdAt: Date - if (this.Pod) { - reporterPodHost = this.Pod.host - } else { - // It means it's our video - reporterPodHost = CONFIG.WEBSERVER.HOST - } - - const json = { - id: this.id, - reporterPodHost, - reason: this.reason, - reporterUsername: this.reporterUsername, - videoId: this.videoId, - createdAt: this.createdAt - } - - 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' + 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.Pod, - required: false - } - ] + @AfterCreate + static sendEmailNotification (instance: VideoAbuseModel) { + return Emailer.Instance.addVideoAbuseReport(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 () { + let reporterServerHost + + if (this.Account.Actor.Server) { + reporterServerHost = this.Account.Actor.Server.host + } else { + // It means it's our video + reporterServerHost = CONFIG.WEBSERVER.HOST + } + + return { + 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 + } + } + + toActivityPubObject (): VideoAbuseObject { + return { + type: 'Flag' as 'Flag', + content: this.reason, + object: this.Video.url + } + } }