X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-abuse.ts;h=1ac7919b31e2c70f8d06c08a6ffd881e205565c8;hb=3caf77d3b11f2dbc12e52d665183d36604c1dab9;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..1ac7919b3 100644 --- a/server/models/video/video-abuse.ts +++ b/server/models/video/video-abuse.ts @@ -1,142 +1,139 @@ -import * as Sequelize from 'sequelize' - -import { CONFIG } from '../../initializers' -import { isVideoAbuseReasonValid } from '../../helpers' - -import { addMethodsToModel, getSort } from '../utils' +import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { VideoAbuseObject } from '../../../shared/models/activitypub/objects' +import { VideoAbuse } from '../../../shared/models/videos' import { - VideoAbuseInstance, - VideoAbuseAttributes, + isVideoAbuseModerationCommentValid, + isVideoAbuseReasonValid, + isVideoAbuseStateValid +} from '../../helpers/custom-validators/video-abuses' +import { AccountModel } from '../account/account' +import { getSort, throwIfNotValid } from '../utils' +import { VideoModel } from './video' +import { VideoAbuseState } from '../../../shared' +import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants' + +@Table({ + tableName: 'videoAbuse', + indexes: [ + { + fields: [ 'videoId' ] + }, + { + fields: [ 'reporterAccountId' ] + } + ] +}) +export class VideoAbuseModel extends Model { + + @AllowNull(false) + @Default(null) + @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason')) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max)) + reason: string + + @AllowNull(false) + @Default(null) + @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state')) + @Column + state: VideoAbuseState + + @AllowNull(true) + @Default(null) + @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true)) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max)) + moderationComment: string + + @CreatedAt + createdAt: Date + + @UpdatedAt + updatedAt: Date + + @ForeignKey(() => AccountModel) + @Column + reporterAccountId: number + + @BelongsTo(() => AccountModel, { + foreignKey: { + allowNull: false + }, + onDelete: 'cascade' + }) + Account: AccountModel - VideoAbuseMethods -} from './video-abuse-interface' -import { VideoAbuseObject } from '../../../shared/models/activitypub/objects/video-abuse-object' + @ForeignKey(() => VideoModel) + @Column + videoId: number -let VideoAbuse: Sequelize.Model -let toFormattedJSON: VideoAbuseMethods.ToFormattedJSON -let listForApi: VideoAbuseMethods.ListForApi -let toActivityPubObject: VideoAbuseMethods.ToActivityPubObject + @BelongsTo(() => VideoModel, { + foreignKey: { + allowNull: false + }, + onDelete: 'cascade' + }) + Video: VideoModel -export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { - VideoAbuse = sequelize.define('VideoAbuse', - { - 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.') - } - } + static loadByIdAndVideoId (id: number, videoId: number) { + const query = { + where: { + id, + videoId } - }, - { - indexes: [ + } + return VideoAbuseModel.findOne(query) + } + + static listForApi (start: number, count: number, sort: string) { + const query = { + offset: start, + limit: count, + order: getSort(sort), + include: [ { - fields: [ 'videoId' ] + model: AccountModel, + required: true }, { - fields: [ 'reporterAccountId' ] + model: VideoModel, + required: true } ] } - ) - - const classMethods = [ - associate, - - listForApi - ] - const instanceMethods = [ - toFormattedJSON, - toActivityPubObject - ] - addMethodsToModel(VideoAbuse, classMethods, instanceMethods) - return VideoAbuse -} - -// ------------------------------ METHODS ------------------------------ - -toFormattedJSON = function (this: VideoAbuseInstance) { - let reporterServerHost - - if (this.Account.Server) { - reporterServerHost = this.Account.Server.host - } else { - // It means it's our video - reporterServerHost = CONFIG.WEBSERVER.HOST + return VideoAbuseModel.findAndCountAll(query) + .then(({ rows, count }) => { + return { total: count, data: rows } + }) } - 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 + toFormattedJSON (): VideoAbuse { + return { + id: this.id, + reason: this.reason, + reporterAccount: this.Account.toFormattedJSON(), + state: { + id: this.state, + label: VideoAbuseModel.getStateLabel(this.state) + }, + moderationComment: this.moderationComment, + video: { + id: this.Video.id, + uuid: this.Video.uuid, + name: 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 + toActivityPubObject (): VideoAbuseObject { + return { + type: 'Flag' as 'Flag', + content: this.reason, + object: this.Video.url + } } - return videoAbuseObject -} - -// ------------------------------ STATICS ------------------------------ - -function associate (models) { - VideoAbuse.belongsTo(models.Account, { - foreignKey: { - name: 'reporterAccountId', - allowNull: true - }, - onDelete: 'CASCADE' - }) - - VideoAbuse.belongsTo(models.Video, { - foreignKey: { - name: 'videoId', - allowNull: false - }, - onDelete: 'CASCADE' - }) -} - -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 - } - ] - }, - { - model: VideoAbuse['sequelize'].models.Video, - required: true - } - ] + private static getStateLabel (id: number) { + return VIDEO_ABUSE_STATES[id] || 'Unknown' } - - return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => { - return { total: count, data: rows } - }) }