2020-04-17 03:47:22 -05:00
|
|
|
import {
|
2020-04-18 03:00:19 -05:00
|
|
|
AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt, Scopes
|
2020-04-17 03:47:22 -05:00
|
|
|
} 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'
|
2020-04-19 07:11:40 -05:00
|
|
|
import { buildBlockedAccountSQL, getSort, throwIfNotValid, searchAttribute } from '../utils'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoModel } from './video'
|
2020-04-18 15:57:20 -05:00
|
|
|
import { VideoAbuseState, VideoDetails } from '../../../shared'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants'
|
2019-08-29 07:31:04 -05:00
|
|
|
import { MUserAccountId, MVideoAbuse, MVideoAbuseFormattable, MVideoAbuseVideo } from '../../typings/models'
|
2019-08-15 04:53:26 -05:00
|
|
|
import * as Bluebird from 'bluebird'
|
2020-04-20 04:20:12 -05:00
|
|
|
import { literal, Op, Sequelize } from 'sequelize'
|
2020-04-17 03:47:22 -05:00
|
|
|
import { ThumbnailModel } from './thumbnail'
|
|
|
|
import { VideoBlacklistModel } from './video-blacklist'
|
2020-04-19 07:11:40 -05:00
|
|
|
import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
|
2017-12-12 10:53:50 -06:00
|
|
|
|
2020-04-18 03:00:19 -05:00
|
|
|
export enum ScopeNames {
|
|
|
|
FOR_API = 'FOR_API'
|
|
|
|
}
|
|
|
|
|
|
|
|
@Scopes(() => ({
|
|
|
|
[ScopeNames.FOR_API]: (options: {
|
|
|
|
search?: string
|
|
|
|
searchReporter?: string
|
|
|
|
searchVideo?: string
|
|
|
|
searchVideoChannel?: string
|
|
|
|
serverAccountId: number
|
2020-04-20 04:20:12 -05:00
|
|
|
userAccountId: number
|
2020-04-18 03:00:19 -05:00
|
|
|
}) => {
|
|
|
|
let where = {
|
|
|
|
reporterAccountId: {
|
|
|
|
[Op.notIn]: literal('(' + buildBlockedAccountSQL(options.serverAccountId, options.userAccountId) + ')')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.search) {
|
|
|
|
where = Object.assign(where, {
|
|
|
|
[Op.or]: [
|
|
|
|
{
|
|
|
|
[Op.and]: [
|
|
|
|
{ videoId: { [Op.not]: null } },
|
2020-04-20 04:20:12 -05:00
|
|
|
searchAttribute(options.search, '$Video.name$')
|
2020-04-18 03:00:19 -05:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[Op.and]: [
|
|
|
|
{ videoId: { [Op.not]: null } },
|
2020-04-20 04:20:12 -05:00
|
|
|
searchAttribute(options.search, '$Video.VideoChannel.name$')
|
2020-04-18 03:00:19 -05:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[Op.and]: [
|
|
|
|
{ deletedVideo: { [Op.not]: null } },
|
2020-04-20 04:20:12 -05:00
|
|
|
{ deletedVideo: searchAttribute(options.search, 'name') }
|
2020-04-18 03:00:19 -05:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[Op.and]: [
|
|
|
|
{ deletedVideo: { [Op.not]: null } },
|
2020-04-20 04:20:12 -05:00
|
|
|
{ deletedVideo: { channel: searchAttribute(options.search, 'displayName') } }
|
2020-04-18 03:00:19 -05:00
|
|
|
]
|
|
|
|
},
|
2020-04-20 04:20:12 -05:00
|
|
|
searchAttribute(options.search, '$Account.name$')
|
2020-04-18 03:00:19 -05:00
|
|
|
]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-04-18 15:57:20 -05:00
|
|
|
attributes: {
|
|
|
|
include: [
|
|
|
|
[
|
|
|
|
literal(
|
|
|
|
'(' +
|
2020-04-20 04:20:12 -05:00
|
|
|
'SELECT count(*) ' +
|
|
|
|
'FROM "videoAbuse" ' +
|
|
|
|
'WHERE "videoId" = "VideoAbuseModel"."videoId" ' +
|
2020-04-18 15:57:20 -05:00
|
|
|
')'
|
|
|
|
),
|
|
|
|
'countReportsForVideo'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
literal(
|
|
|
|
'(' +
|
|
|
|
'SELECT t.nth ' +
|
|
|
|
'FROM ( ' +
|
|
|
|
'SELECT id, ' +
|
|
|
|
'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
|
|
|
|
'FROM "videoAbuse" ' +
|
|
|
|
') t ' +
|
|
|
|
'WHERE t.id = "VideoAbuseModel".id ' +
|
|
|
|
')'
|
|
|
|
),
|
|
|
|
'nthReportForVideo'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
literal(
|
|
|
|
'(' +
|
|
|
|
'SELECT count("videoAbuse"."id") ' +
|
|
|
|
'FROM "videoAbuse" ' +
|
|
|
|
'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
|
|
|
|
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
|
|
|
|
'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
|
|
|
|
'WHERE "account"."id" = "VideoAbuseModel"."reporterAccountId" ' +
|
|
|
|
')'
|
|
|
|
),
|
|
|
|
'countReportsForReporter'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
literal(
|
|
|
|
'(' +
|
2020-04-20 04:20:12 -05:00
|
|
|
'SELECT count(DISTINCT "videoAbuse"."id") ' +
|
2020-04-18 15:57:20 -05:00
|
|
|
'FROM "videoAbuse" ' +
|
|
|
|
'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
|
|
|
|
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
|
2020-04-20 04:20:12 -05:00
|
|
|
'INNER JOIN "account" ON "videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' +
|
2020-04-18 15:57:20 -05:00
|
|
|
')'
|
|
|
|
),
|
|
|
|
'countReportsForReportee'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
},
|
2020-04-17 03:47:22 -05:00
|
|
|
include: [
|
|
|
|
{
|
2020-04-18 03:00:19 -05:00
|
|
|
model: AccountModel,
|
|
|
|
required: true,
|
2020-04-20 04:20:12 -05:00
|
|
|
where: searchAttribute(options.searchReporter, 'name')
|
2020-04-17 03:47:22 -05:00
|
|
|
},
|
|
|
|
{
|
2020-04-18 03:00:19 -05:00
|
|
|
model: VideoModel,
|
|
|
|
required: false,
|
2020-04-20 04:20:12 -05:00
|
|
|
where: searchAttribute(options.searchVideo, 'name'),
|
2020-04-17 03:47:22 -05:00
|
|
|
include: [
|
|
|
|
{
|
2020-04-18 03:00:19 -05:00
|
|
|
model: ThumbnailModel
|
|
|
|
},
|
|
|
|
{
|
2020-04-19 07:11:40 -05:00
|
|
|
model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
|
2020-04-20 04:20:12 -05:00
|
|
|
where: searchAttribute(options.searchVideoChannel, 'name')
|
2020-04-18 03:00:19 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
attributes: [ 'id', 'reason', 'unfederated' ],
|
|
|
|
model: VideoBlacklistModel
|
2020-04-17 03:47:22 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2020-04-18 03:00:19 -05:00
|
|
|
],
|
|
|
|
where
|
2020-04-17 03:47:22 -05:00
|
|
|
}
|
2020-04-18 03:00:19 -05:00
|
|
|
}
|
2020-04-17 03:47:22 -05:00
|
|
|
}))
|
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)
|
2019-04-18 04:28:17 -05:00
|
|
|
@Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
|
2018-08-10 09:54:01 -05:00
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
|
|
|
|
moderationComment: string
|
|
|
|
|
2020-04-16 07:22:27 -05:00
|
|
|
@AllowNull(true)
|
|
|
|
@Default(null)
|
|
|
|
@Column(DataType.JSONB)
|
2020-04-18 15:57:20 -05:00
|
|
|
deletedVideo: VideoDetails
|
2020-04-16 07:22:27 -05: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: {
|
2020-04-16 07:22:27 -05:00
|
|
|
allowNull: true
|
2017-01-04 13:59:23 -06:00
|
|
|
},
|
2020-04-16 07:22:27 -05:00
|
|
|
onDelete: 'set null'
|
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: {
|
2020-04-16 07:22:27 -05:00
|
|
|
allowNull: true
|
2017-01-04 13:59:23 -06:00
|
|
|
},
|
2020-04-16 07:22:27 -05:00
|
|
|
onDelete: 'set null'
|
2017-01-04 13:59:23 -06:00
|
|
|
})
|
2017-12-12 10:53:50 -06:00
|
|
|
Video: VideoModel
|
|
|
|
|
2020-04-16 07:22:27 -05: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 09:54:01 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
id,
|
2020-04-16 07:22:27 -05:00
|
|
|
...videoAttributes
|
2018-08-10 09:54:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return VideoAbuseModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
2019-08-29 07:31:04 -05:00
|
|
|
static listForApi (parameters: {
|
2020-01-31 09:56:52 -06:00
|
|
|
start: number
|
|
|
|
count: number
|
|
|
|
sort: string
|
2020-04-18 03:00:19 -05:00
|
|
|
search?: string
|
2019-08-29 07:31:04 -05:00
|
|
|
serverAccountId: number
|
|
|
|
user?: MUserAccountId
|
|
|
|
}) {
|
2020-04-18 03:00:19 -05:00
|
|
|
const { start, count, sort, search, user, serverAccountId } = parameters
|
2019-08-29 07:31:04 -05:00
|
|
|
const userAccountId = user ? user.Account.id : undefined
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2018-02-19 02:41:03 -06:00
|
|
|
order: getSort(sort),
|
2020-04-17 03:47:22 -05:00
|
|
|
col: 'VideoAbuseModel.id',
|
|
|
|
distinct: true
|
2017-12-12 10:53:50 -06:00
|
|
|
}
|
2017-01-04 13:59:23 -06:00
|
|
|
|
2020-04-18 03:00:19 -05:00
|
|
|
const filters = {
|
|
|
|
search,
|
|
|
|
serverAccountId,
|
|
|
|
userAccountId
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoAbuseModel
|
|
|
|
.scope({ method: [ ScopeNames.FOR_API, filters ] })
|
|
|
|
.findAndCountAll(query)
|
2017-12-12 10:53:50 -06:00
|
|
|
.then(({ rows, count }) => {
|
|
|
|
return { total: count, data: rows }
|
|
|
|
})
|
2017-01-04 13:59:23 -06:00
|
|
|
}
|
|
|
|
|
2019-08-20 12:05:31 -05:00
|
|
|
toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
|
2020-04-18 15:57:20 -05:00
|
|
|
const countReportsForVideo = this.get('countReportsForVideo') as number
|
|
|
|
const nthReportForVideo = this.get('nthReportForVideo') as number
|
|
|
|
const countReportsForReporter = this.get('countReportsForReporter') as number
|
|
|
|
const countReportsForReportee = this.get('countReportsForReportee') as number
|
|
|
|
|
2020-04-16 07:22:27 -05:00
|
|
|
const video = this.Video
|
|
|
|
? this.Video
|
|
|
|
: this.deletedVideo
|
|
|
|
|
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: {
|
2020-04-16 07:22:27 -05:00
|
|
|
id: video.id,
|
|
|
|
uuid: video.uuid,
|
|
|
|
name: video.name,
|
|
|
|
nsfw: video.nsfw,
|
2020-04-17 03:47:22 -05:00
|
|
|
deleted: !this.Video,
|
|
|
|
blacklisted: this.Video && this.Video.isBlacklisted(),
|
|
|
|
thumbnailPath: this.Video?.getMiniatureStaticPath(),
|
2020-04-18 15:57:20 -05:00
|
|
|
channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel
|
2018-03-12 05:29:46 -05:00
|
|
|
},
|
2020-04-18 15:57:20 -05:00
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt,
|
|
|
|
count: countReportsForVideo || 0,
|
|
|
|
nth: nthReportForVideo || 0,
|
|
|
|
countReportsForReporter: countReportsForReporter || 0,
|
|
|
|
countReportsForReportee: countReportsForReportee || 0
|
2017-12-12 10:53:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
|
2017-12-12 10:53:50 -06:00
|
|
|
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
|
|
|
}
|