From 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 12 Dec 2017 17:53:50 +0100 Subject: Move models to typescript-sequelize --- server/models/video/video-abuse.ts | 210 ++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 118 deletions(-) (limited to 'server/models/video/video-abuse.ts') diff --git a/server/models/video/video-abuse.ts b/server/models/video/video-abuse.ts index d09f5f7a1..d0ee969fb 100644 --- a/server/models/video/video-abuse.ts +++ b/server/models/video/video-abuse.ts @@ -1,142 +1,116 @@ -import * as Sequelize from 'sequelize' - +import { 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 { 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 { AccountModel } from '../account/account' +import { ServerModel } from '../server/server' +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) - - 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 - } - - 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 - } +}) +export class VideoAbuseModel extends Model { - return json -} + @AllowNull(false) + @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason')) + @Column + reason: string -toActivityPubObject = function (this: VideoAbuseInstance) { - const videoAbuseObject: VideoAbuseObject = { - type: 'Flag' as 'Flag', - content: this.reason, - object: this.Video.url - } + @CreatedAt + createdAt: Date - return videoAbuseObject -} + @UpdatedAt + updatedAt: Date -// ------------------------------ STATICS ------------------------------ + @ForeignKey(() => AccountModel) + @Column + reporterAccountId: number -function associate (models) { - VideoAbuse.belongsTo(models.Account, { + @BelongsTo(() => AccountModel, { foreignKey: { - name: 'reporterAccountId', 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 + + static listForApi (start: number, count: number, sort: string) { + const query = { + offset: start, + limit: count, + order: [ getSort(sort) ], + include: [ + { + model: AccountModel, + required: true, + include: [ + { + model: ServerModel, + required: false + } + ] + }, + { + model: VideoModel, + required: true + } + ] + } -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 - } - ] + return VideoAbuseModel.findAndCountAll(query) + .then(({ rows, count }) => { + return { total: count, data: rows } + }) } - return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => { - return { total: count, data: rows } - }) + toFormattedJSON () { + let reporterServerHost + + if (this.Account.Server) { + reporterServerHost = this.Account.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 + } + } } -- cgit v1.2.3