2019-01-14 09:48:38 -06:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoAbuseObject } from '../../../shared/models/activitypub/objects'
|
2018-03-12 05:29:46 -05:00
|
|
|
import { VideoAbuse } from '../../../shared/models/videos'
|
2018-08-10 09:54:01 -05:00
|
|
|
import {
|
|
|
|
isVideoAbuseModerationCommentValid,
|
|
|
|
isVideoAbuseReasonValid,
|
|
|
|
isVideoAbuseStateValid
|
|
|
|
} from '../../helpers/custom-validators/video-abuses'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { AccountModel } from '../account/account'
|
|
|
|
import { getSort, throwIfNotValid } from '../utils'
|
|
|
|
import { VideoModel } from './video'
|
2018-08-10 09:54:01 -05:00
|
|
|
import { VideoAbuseState } from '../../../shared'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants'
|
2017-12-12 10:53:50 -06:00
|
|
|
|
|
|
|
@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)
|
2019-01-14 09:48:38 -06:00
|
|
|
@Default(null)
|
2017-12-12 10:53:50 -06:00
|
|
|
@Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
|
2019-01-14 09:48:38 -06:00
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
|
2017-12-12 10:53:50 -06:00
|
|
|
reason: string
|
2017-11-16 10:04:19 -06:00
|
|
|
|
2018-08-10 09:54:01 -05:00
|
|
|
@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'))
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
|
|
|
|
moderationComment: string
|
|
|
|
|
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
|
|
|
|
|
2018-08-10 09:54:01 -05:00
|
|
|
static loadByIdAndVideoId (id: number, videoId: number) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
videoId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return VideoAbuseModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static listForApi (start: number, count: number, sort: string) {
|
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2018-02-19 02:41:03 -06:00
|
|
|
order: getSort(sort),
|
2017-12-12 10:53:50 -06:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: AccountModel,
|
2017-12-14 10:38:41 -06:00
|
|
|
required: true
|
2017-12-12 10:53:50 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-12 05:29:46 -05:00
|
|
|
toFormattedJSON (): VideoAbuse {
|
2017-12-12 10:53:50 -06:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
reason: this.reason,
|
2018-03-12 05:29:46 -05:00
|
|
|
reporterAccount: this.Account.toFormattedJSON(),
|
2018-08-10 09:54:01 -05:00
|
|
|
state: {
|
|
|
|
id: this.state,
|
|
|
|
label: VideoAbuseModel.getStateLabel(this.state)
|
|
|
|
},
|
|
|
|
moderationComment: this.moderationComment,
|
2018-03-12 05:29:46 -05:00
|
|
|
video: {
|
|
|
|
id: this.Video.id,
|
|
|
|
uuid: this.Video.uuid,
|
|
|
|
name: this.Video.name
|
|
|
|
},
|
2017-12-12 10:53:50 -06:00
|
|
|
createdAt: this.createdAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toActivityPubObject (): VideoAbuseObject {
|
|
|
|
return {
|
|
|
|
type: 'Flag' as 'Flag',
|
|
|
|
content: this.reason,
|
|
|
|
object: this.Video.url
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 09:54:01 -05:00
|
|
|
|
|
|
|
private static getStateLabel (id: number) {
|
|
|
|
return VIDEO_ABUSE_STATES[id] || 'Unknown'
|
|
|
|
}
|
2017-01-04 13:59:23 -06:00
|
|
|
}
|