2019-01-14 10:48:38 -05:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { VideoAbuseObject } from '../../../shared/models/activitypub/objects'
|
2018-03-12 06:29:46 -04:00
|
|
|
import { VideoAbuse } from '../../../shared/models/videos'
|
2018-08-10 10:54:01 -04:00
|
|
|
import {
|
|
|
|
isVideoAbuseModerationCommentValid,
|
|
|
|
isVideoAbuseReasonValid,
|
|
|
|
isVideoAbuseStateValid
|
|
|
|
} from '../../helpers/custom-validators/video-abuses'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { AccountModel } from '../account/account'
|
2019-08-29 08:31:04 -04:00
|
|
|
import { buildBlockedAccountSQL, getSort, throwIfNotValid } from '../utils'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { VideoModel } from './video'
|
2020-04-16 08:22:27 -04:00
|
|
|
import { VideoAbuseState, Video } from '../../../shared'
|
2019-04-11 08:26:41 -04:00
|
|
|
import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants'
|
2019-08-29 08:31:04 -04:00
|
|
|
import { MUserAccountId, MVideoAbuse, MVideoAbuseFormattable, MVideoAbuseVideo } from '../../typings/models'
|
2019-08-15 05:53:26 -04:00
|
|
|
import * as Bluebird from 'bluebird'
|
2019-08-29 08:31:04 -04:00
|
|
|
import { literal, Op } from 'sequelize'
|
2017-12-12 11:53:50 -05:00
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'videoAbuse',
|
|
|
|
indexes: [
|
2017-01-04 14:59:23 -05:00
|
|
|
{
|
2017-12-12 11:53:50 -05:00
|
|
|
fields: [ 'videoId' ]
|
2017-01-04 14:59:23 -05:00
|
|
|
},
|
|
|
|
{
|
2017-12-12 11:53:50 -05:00
|
|
|
fields: [ 'reporterAccountId' ]
|
2017-01-04 14:59:23 -05:00
|
|
|
}
|
2017-05-22 14:58:25 -04:00
|
|
|
]
|
2017-12-12 11:53:50 -05:00
|
|
|
})
|
|
|
|
export class VideoAbuseModel extends Model<VideoAbuseModel> {
|
2017-05-22 14:58:25 -04:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@AllowNull(false)
|
2019-01-14 10:48:38 -05:00
|
|
|
@Default(null)
|
2017-12-12 11:53:50 -05:00
|
|
|
@Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
|
2019-01-14 10:48:38 -05:00
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
|
2017-12-12 11:53:50 -05:00
|
|
|
reason: string
|
2017-11-16 11:04:19 -05:00
|
|
|
|
2018-08-10 10:54:01 -04:00
|
|
|
@AllowNull(false)
|
|
|
|
@Default(null)
|
|
|
|
@Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
|
|
|
|
@Column
|
|
|
|
state: VideoAbuseState
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Default(null)
|
2019-04-18 05:28:17 -04:00
|
|
|
@Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
|
2018-08-10 10:54:01 -04:00
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
|
|
|
|
moderationComment: string
|
|
|
|
|
2020-04-16 08:22:27 -04:00
|
|
|
@AllowNull(true)
|
|
|
|
@Default(null)
|
|
|
|
@Column(DataType.JSONB)
|
|
|
|
deletedVideo: Video
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
2017-11-16 11:04:19 -05:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
2017-05-22 14:58:25 -04:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@ForeignKey(() => AccountModel)
|
|
|
|
@Column
|
|
|
|
reporterAccountId: number
|
2017-01-04 14:59:23 -05:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@BelongsTo(() => AccountModel, {
|
2017-01-04 14:59:23 -05:00
|
|
|
foreignKey: {
|
2020-04-16 08:22:27 -04:00
|
|
|
allowNull: true
|
2017-01-04 14:59:23 -05:00
|
|
|
},
|
2020-04-16 08:22:27 -04:00
|
|
|
onDelete: 'set null'
|
2017-01-04 14:59:23 -05:00
|
|
|
})
|
2017-12-12 11:53:50 -05:00
|
|
|
Account: AccountModel
|
|
|
|
|
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
2017-01-04 14:59:23 -05:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@BelongsTo(() => VideoModel, {
|
2017-01-04 14:59:23 -05:00
|
|
|
foreignKey: {
|
2020-04-16 08:22:27 -04:00
|
|
|
allowNull: true
|
2017-01-04 14:59:23 -05:00
|
|
|
},
|
2020-04-16 08:22:27 -04:00
|
|
|
onDelete: 'set null'
|
2017-01-04 14:59:23 -05:00
|
|
|
})
|
2017-12-12 11:53:50 -05:00
|
|
|
Video: VideoModel
|
|
|
|
|
2020-04-16 08:22:27 -04:00
|
|
|
static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
|
|
|
|
const videoAttributes = {}
|
|
|
|
if (videoId) videoAttributes['videoId'] = videoId
|
|
|
|
if (uuid) videoAttributes['deletedVideo'] = { uuid }
|
|
|
|
|
2018-08-10 10:54:01 -04:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
id,
|
2020-04-16 08:22:27 -04:00
|
|
|
...videoAttributes
|
2018-08-10 10:54:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return VideoAbuseModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
2019-08-29 08:31:04 -04:00
|
|
|
static listForApi (parameters: {
|
2020-01-31 10:56:52 -05:00
|
|
|
start: number
|
|
|
|
count: number
|
|
|
|
sort: string
|
2019-08-29 08:31:04 -04:00
|
|
|
serverAccountId: number
|
|
|
|
user?: MUserAccountId
|
|
|
|
}) {
|
|
|
|
const { start, count, sort, user, serverAccountId } = parameters
|
|
|
|
const userAccountId = user ? user.Account.id : undefined
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2018-02-19 03:41:03 -05:00
|
|
|
order: getSort(sort),
|
2019-08-29 08:31:04 -04:00
|
|
|
where: {
|
|
|
|
reporterAccountId: {
|
|
|
|
[Op.notIn]: literal('(' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')')
|
|
|
|
}
|
|
|
|
},
|
2017-12-12 11:53:50 -05:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: AccountModel,
|
2017-12-14 11:38:41 -05:00
|
|
|
required: true
|
2017-12-12 11:53:50 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
model: VideoModel,
|
2020-04-16 08:22:27 -04:00
|
|
|
required: false
|
2017-12-12 11:53:50 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2017-01-04 14:59:23 -05:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
return VideoAbuseModel.findAndCountAll(query)
|
|
|
|
.then(({ rows, count }) => {
|
|
|
|
return { total: count, data: rows }
|
|
|
|
})
|
2017-01-04 14:59:23 -05:00
|
|
|
}
|
|
|
|
|
2019-08-20 13:05:31 -04:00
|
|
|
toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
|
2020-04-16 08:22:27 -04:00
|
|
|
const video = this.Video
|
|
|
|
? this.Video
|
|
|
|
: this.deletedVideo
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
reason: this.reason,
|
2018-03-12 06:29:46 -04:00
|
|
|
reporterAccount: this.Account.toFormattedJSON(),
|
2018-08-10 10:54:01 -04:00
|
|
|
state: {
|
|
|
|
id: this.state,
|
|
|
|
label: VideoAbuseModel.getStateLabel(this.state)
|
|
|
|
},
|
|
|
|
moderationComment: this.moderationComment,
|
2018-03-12 06:29:46 -04:00
|
|
|
video: {
|
2020-04-16 08:22:27 -04:00
|
|
|
id: video.id,
|
|
|
|
uuid: video.uuid,
|
|
|
|
name: video.name,
|
|
|
|
nsfw: video.nsfw,
|
|
|
|
deleted: !this.Video
|
2018-03-12 06:29:46 -04:00
|
|
|
},
|
2017-12-12 11:53:50 -05:00
|
|
|
createdAt: this.createdAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 05:53:26 -04:00
|
|
|
toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
|
2017-12-12 11:53:50 -05:00
|
|
|
return {
|
|
|
|
type: 'Flag' as 'Flag',
|
|
|
|
content: this.reason,
|
|
|
|
object: this.Video.url
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 10:54:01 -04:00
|
|
|
|
|
|
|
private static getStateLabel (id: number) {
|
|
|
|
return VIDEO_ABUSE_STATES[id] || 'Unknown'
|
|
|
|
}
|
2017-01-04 14:59:23 -05:00
|
|
|
}
|