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

117 lines
2.5 KiB
TypeScript
Raw Normal View History

2017-12-12 10:53:50 -06:00
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'
2017-06-16 02:45:46 -05:00
import { CONFIG } from '../../initializers'
2017-12-12 10:53:50 -06:00
import { AccountModel } from '../account/account'
import { ServerModel } from '../server/server'
import { getSort, throwIfNotValid } from '../utils'
import { VideoModel } from './video'
@Table({
tableName: 'videoAbuse',
indexes: [
2017-01-04 13:59:23 -06:00
{
2017-12-12 10:53:50 -06:00
fields: [ 'videoId' ]
2017-01-04 13:59:23 -06:00
},
{
2017-12-12 10:53:50 -06:00
fields: [ 'reporterAccountId' ]
2017-01-04 13:59:23 -06:00
}
2017-05-22 13:58:25 -05:00
]
2017-12-12 10:53:50 -06:00
})
export class VideoAbuseModel extends Model<VideoAbuseModel> {
2017-05-22 13:58:25 -05:00
2017-12-12 10:53:50 -06:00
@AllowNull(false)
@Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
@Column
reason: string
2017-11-16 10:04:19 -06:00
2017-12-12 10:53:50 -06:00
@CreatedAt
createdAt: Date
2017-11-16 10:04:19 -06:00
2017-12-12 10:53:50 -06:00
@UpdatedAt
updatedAt: Date
2017-05-22 13:58:25 -05:00
2017-12-12 10:53:50 -06:00
@ForeignKey(() => AccountModel)
@Column
reporterAccountId: number
2017-01-04 13:59:23 -06:00
2017-12-12 10:53:50 -06:00
@BelongsTo(() => AccountModel, {
2017-01-04 13:59:23 -06:00
foreignKey: {
2017-11-27 02:47:21 -06:00
allowNull: false
2017-01-04 13:59:23 -06:00
},
2017-12-12 10:53:50 -06:00
onDelete: 'cascade'
2017-01-04 13:59:23 -06:00
})
2017-12-12 10:53:50 -06:00
Account: AccountModel
@ForeignKey(() => VideoModel)
@Column
videoId: number
2017-01-04 13:59:23 -06:00
2017-12-12 10:53:50 -06:00
@BelongsTo(() => VideoModel, {
2017-01-04 13:59:23 -06:00
foreignKey: {
allowNull: false
},
2017-12-12 10:53:50 -06:00
onDelete: 'cascade'
2017-01-04 13:59:23 -06:00
})
2017-12-12 10:53:50 -06:00
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
}
]
}
2017-01-04 13:59:23 -06:00
2017-12-12 10:53:50 -06:00
return VideoAbuseModel.findAndCountAll(query)
.then(({ rows, count }) => {
return { total: count, data: rows }
})
2017-01-04 13:59:23 -06:00
}
2017-12-12 10:53:50 -06:00
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
}
}
2017-01-04 13:59:23 -06:00
}