1
0
Fork 0
peertube/server/models/video/video-abuse.ts

143 lines
3.3 KiB
TypeScript
Raw Normal View History

2017-05-22 18:58:25 +00:00
import * as Sequelize from 'sequelize'
2017-06-16 07:45:46 +00:00
import { CONFIG } from '../../initializers'
2017-11-15 14:12:23 +00:00
import { isVideoAbuseReasonValid } from '../../helpers'
2017-01-04 19:59:23 +00:00
2017-06-16 07:45:46 +00:00
import { addMethodsToModel, getSort } from '../utils'
2017-05-22 18:58:25 +00:00
import {
VideoAbuseInstance,
VideoAbuseAttributes,
VideoAbuseMethods
} from './video-abuse-interface'
2017-11-16 16:04:19 +00:00
import { VideoAbuseObject } from '../../../shared/models/activitypub/objects/video-abuse-object'
2017-05-22 18:58:25 +00:00
let VideoAbuse: Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes>
2017-08-25 09:45:31 +00:00
let toFormattedJSON: VideoAbuseMethods.ToFormattedJSON
2017-05-22 18:58:25 +00:00
let listForApi: VideoAbuseMethods.ListForApi
2017-11-16 16:04:19 +00:00
let toActivityPubObject: VideoAbuseMethods.ToActivityPubObject
2017-05-22 18:58:25 +00:00
2017-06-11 15:35:32 +00:00
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
VideoAbuse = sequelize.define<VideoAbuseInstance, VideoAbuseAttributes>('VideoAbuse',
2017-01-04 19:59:23 +00:00
{
reason: {
type: DataTypes.STRING,
allowNull: false,
validate: {
2017-07-11 15:04:57 +00:00
reasonValid: value => {
2017-05-15 20:22:03 +00:00
const res = isVideoAbuseReasonValid(value)
2017-01-04 19:59:23 +00:00
if (res === false) throw new Error('Video abuse reason is not valid.')
}
}
}
},
{
indexes: [
{
fields: [ 'videoId' ]
},
{
2017-11-15 14:12:23 +00:00
fields: [ 'reporterAccountId' ]
2017-01-04 19:59:23 +00:00
}
2017-05-22 18:58:25 +00:00
]
2017-01-04 19:59:23 +00:00
}
)
2017-05-22 18:58:25 +00:00
const classMethods = [
associate,
listForApi
]
const instanceMethods = [
2017-11-16 16:04:19 +00:00
toFormattedJSON,
toActivityPubObject
2017-05-22 18:58:25 +00:00
]
addMethodsToModel(VideoAbuse, classMethods, instanceMethods)
2017-01-04 19:59:23 +00:00
return VideoAbuse
}
2017-05-22 18:58:25 +00:00
// ------------------------------ METHODS ------------------------------
2017-08-25 09:45:31 +00:00
toFormattedJSON = function (this: VideoAbuseInstance) {
2017-11-15 10:00:25 +00:00
let reporterServerHost
2017-05-22 18:58:25 +00:00
2017-11-15 14:12:23 +00:00
if (this.Account.Server) {
reporterServerHost = this.Account.Server.host
2017-05-22 18:58:25 +00:00
} else {
// It means it's our video
2017-11-15 10:00:25 +00:00
reporterServerHost = CONFIG.WEBSERVER.HOST
2017-05-22 18:58:25 +00:00
}
const json = {
id: this.id,
reason: this.reason,
2017-11-15 14:12:23 +00:00
reporterUsername: this.Account.name,
reporterServerHost,
videoId: this.Video.id,
videoUUID: this.Video.uuid,
videoName: this.Video.name,
2017-05-22 18:58:25 +00:00
createdAt: this.createdAt
}
return json
}
2017-11-16 16:04:19 +00:00
toActivityPubObject = function (this: VideoAbuseInstance) {
const videoAbuseObject: VideoAbuseObject = {
type: 'Flag' as 'Flag',
content: this.reason,
object: this.Video.url
}
return videoAbuseObject
}
2017-05-22 18:58:25 +00:00
// ------------------------------ STATICS ------------------------------
2017-01-04 19:59:23 +00:00
function associate (models) {
2017-11-15 14:12:23 +00:00
VideoAbuse.belongsTo(models.Account, {
2017-01-04 19:59:23 +00:00
foreignKey: {
2017-11-15 14:12:23 +00:00
name: 'reporterAccountId',
2017-11-27 08:47:21 +00:00
allowNull: false
2017-01-04 19:59:23 +00:00
},
onDelete: 'CASCADE'
2017-01-04 19:59:23 +00:00
})
2017-05-22 18:58:25 +00:00
VideoAbuse.belongsTo(models.Video, {
2017-01-04 19:59:23 +00:00
foreignKey: {
name: 'videoId',
allowNull: false
},
onDelete: 'CASCADE'
2017-01-04 19:59:23 +00:00
})
}
listForApi = function (start: number, count: number, sort: string) {
2017-01-04 19:59:23 +00:00
const query = {
offset: start,
limit: count,
2017-05-15 20:22:03 +00:00
order: [ getSort(sort) ],
2017-01-04 19:59:23 +00:00
include: [
{
2017-11-15 14:12:23 +00:00
model: VideoAbuse['sequelize'].models.Account,
required: true,
include: [
{
model: VideoAbuse['sequelize'].models.Server,
required: false
}
]
},
{
model: VideoAbuse['sequelize'].models.Video,
required: true
2017-01-04 19:59:23 +00:00
}
]
}
return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => {
return { total: count, data: rows }
2017-01-04 19:59:23 +00:00
})
}