PeerTube/server/models/video/video-abuse.ts

128 lines
2.9 KiB
TypeScript
Raw Normal View History

2017-05-22 20:58:25 +02:00
import * as Sequelize from 'sequelize'
2017-06-16 09:45:46 +02:00
import { CONFIG } from '../../initializers'
import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../../helpers'
2017-01-04 20:59:23 +01:00
2017-06-16 09:45:46 +02:00
import { addMethodsToModel, getSort } from '../utils'
2017-05-22 20:58:25 +02:00
import {
VideoAbuseInstance,
VideoAbuseAttributes,
VideoAbuseMethods
} from './video-abuse-interface'
let VideoAbuse: Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes>
2017-06-17 11:28:11 +02:00
let toFormatedJSON: VideoAbuseMethods.ToFormatedJSON
2017-05-22 20:58:25 +02:00
let listForApi: VideoAbuseMethods.ListForApi
2017-06-11 17:35:32 +02:00
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
VideoAbuse = sequelize.define<VideoAbuseInstance, VideoAbuseAttributes>('VideoAbuse',
2017-01-04 20:59:23 +01:00
{
reporterUsername: {
type: DataTypes.STRING,
allowNull: false,
validate: {
2017-07-11 17:04:57 +02:00
reporterUsernameValid: value => {
2017-05-15 22:22:03 +02:00
const res = isVideoAbuseReporterUsernameValid(value)
2017-01-04 20:59:23 +01:00
if (res === false) throw new Error('Video abuse reporter username is not valid.')
}
}
},
reason: {
type: DataTypes.STRING,
allowNull: false,
validate: {
2017-07-11 17:04:57 +02:00
reasonValid: value => {
2017-05-15 22:22:03 +02:00
const res = isVideoAbuseReasonValid(value)
2017-01-04 20:59:23 +01:00
if (res === false) throw new Error('Video abuse reason is not valid.')
}
}
}
},
{
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'reporterPodId' ]
}
2017-05-22 20:58:25 +02:00
]
2017-01-04 20:59:23 +01:00
}
)
2017-05-22 20:58:25 +02:00
const classMethods = [
associate,
listForApi
]
const instanceMethods = [
toFormatedJSON
]
addMethodsToModel(VideoAbuse, classMethods, instanceMethods)
2017-01-04 20:59:23 +01:00
return VideoAbuse
}
2017-05-22 20:58:25 +02:00
// ------------------------------ METHODS ------------------------------
2017-06-17 11:28:11 +02:00
toFormatedJSON = function (this: VideoAbuseInstance) {
2017-05-22 20:58:25 +02:00
let reporterPodHost
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
}
// ------------------------------ STATICS ------------------------------
2017-01-04 20:59:23 +01:00
function associate (models) {
2017-05-22 20:58:25 +02:00
VideoAbuse.belongsTo(models.Pod, {
2017-01-04 20:59:23 +01:00
foreignKey: {
name: 'reporterPodId',
allowNull: true
},
onDelete: 'CASCADE'
2017-01-04 20:59:23 +01:00
})
2017-05-22 20:58:25 +02:00
VideoAbuse.belongsTo(models.Video, {
2017-01-04 20:59:23 +01:00
foreignKey: {
name: 'videoId',
allowNull: false
},
onDelete: 'CASCADE'
2017-01-04 20:59:23 +01:00
})
}
listForApi = function (start: number, count: number, sort: string) {
2017-01-04 20:59:23 +01:00
const query = {
offset: start,
limit: count,
2017-05-15 22:22:03 +02:00
order: [ getSort(sort) ],
2017-01-04 20:59:23 +01:00
include: [
{
2017-05-22 20:58:25 +02:00
model: VideoAbuse['sequelize'].models.Pod,
2017-01-04 20:59:23 +01:00
required: false
}
]
}
return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => {
return { total: count, data: rows }
2017-01-04 20:59:23 +01:00
})
}