PeerTube/server/models/abuse/abuse.ts

539 lines
16 KiB
TypeScript
Raw Normal View History

2020-05-06 17:39:07 +02:00
import * as Bluebird from 'bluebird'
2020-07-01 16:05:30 +02:00
import { invert } from 'lodash'
import { literal, Op, WhereOptions } from 'sequelize'
import {
2020-05-06 17:39:07 +02:00
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey,
2020-07-01 16:05:30 +02:00
HasOne,
2020-05-06 17:39:07 +02:00
Is,
Model,
Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
2020-07-01 16:05:30 +02:00
import { isAbuseModerationCommentValid, isAbuseReasonValid, isAbuseStateValid } from '@server/helpers/custom-validators/abuses'
import {
2020-07-01 16:05:30 +02:00
Abuse,
AbuseObject,
AbusePredefinedReasons,
abusePredefinedReasonsMap,
AbusePredefinedReasonsString,
AbuseState,
AbuseVideoIs,
VideoAbuse
} from '@shared/models'
import { AbuseFilter } from '@shared/models/moderation/abuse/abuse-filter'
import { CONSTRAINTS_FIELDS, ABUSE_STATES } from '../../initializers/constants'
import { MAbuse, MAbuseAP, MAbuseFormattable, MUserAccountId } from '../../types/models'
import { AccountModel, ScopeNames as AccountScopeNames } from '../account/account'
2020-05-06 17:39:07 +02:00
import { buildBlockedAccountSQL, getSort, searchAttribute, throwIfNotValid } from '../utils'
2020-07-01 16:05:30 +02:00
import { ThumbnailModel } from '../video/thumbnail'
import { VideoModel } from '../video/video'
import { VideoBlacklistModel } from '../video/video-blacklist'
import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
import { VideoAbuseModel } from './video-abuse'
import { VideoCommentAbuseModel } from './video-comment-abuse'
2017-12-12 17:53:50 +01:00
export enum ScopeNames {
FOR_API = 'FOR_API'
}
@Scopes(() => ({
[ScopeNames.FOR_API]: (options: {
// search
search?: string
searchReporter?: string
searchReportee?: string
2020-07-01 16:05:30 +02:00
// video releated
searchVideo?: string
searchVideoChannel?: string
2020-07-01 16:05:30 +02:00
videoIs?: AbuseVideoIs
2020-05-06 10:31:52 +02:00
// filters
id?: number
predefinedReasonId?: number
2020-07-01 16:05:30 +02:00
filter?: AbuseFilter
2020-05-06 17:39:07 +02:00
2020-07-01 16:05:30 +02:00
state?: AbuseState
2020-05-06 10:31:52 +02:00
// accountIds
serverAccountId: number
userAccountId: number
}) => {
2020-07-01 16:05:30 +02:00
const onlyBlacklisted = options.videoIs === 'blacklisted'
const videoRequired = !!(onlyBlacklisted || options.searchVideo || options.searchVideoChannel)
2020-05-06 17:39:07 +02:00
const where = {
reporterAccountId: {
[Op.notIn]: literal('(' + buildBlockedAccountSQL([ options.serverAccountId, options.userAccountId ]) + ')')
}
}
if (options.search) {
2020-07-01 16:05:30 +02:00
const escapedSearch = AbuseModel.sequelize.escape('%' + options.search + '%')
2020-05-06 17:39:07 +02:00
Object.assign(where, {
[Op.or]: [
{
[Op.and]: [
2020-07-01 16:05:30 +02:00
{ '$VideoAbuse.videoId$': { [Op.not]: null } },
searchAttribute(options.search, '$VideoAbuse.Video.name$')
]
},
{
[Op.and]: [
2020-07-01 16:05:30 +02:00
{ '$VideoAbuse.videoId$': { [Op.not]: null } },
searchAttribute(options.search, '$VideoAbuse.Video.VideoChannel.name$')
]
},
{
[Op.and]: [
2020-07-01 16:05:30 +02:00
{ '$VideoAbuse.deletedVideo$': { [Op.not]: null } },
literal(`"VideoAbuse"."deletedVideo"->>'name' ILIKE ${escapedSearch}`)
]
},
{
[Op.and]: [
2020-07-01 16:05:30 +02:00
{ '$VideoAbuse.deletedVideo$': { [Op.not]: null } },
literal(`"VideoAbuse"."deletedVideo"->'channel'->>'displayName' ILIKE ${escapedSearch}`)
]
},
2020-07-01 16:05:30 +02:00
searchAttribute(options.search, '$ReporterAccount.name$'),
searchAttribute(options.search, '$FlaggedAccount.name$')
]
})
}
2020-05-06 17:39:07 +02:00
if (options.id) Object.assign(where, { id: options.id })
if (options.state) Object.assign(where, { state: options.state })
2020-05-06 17:39:07 +02:00
if (options.videoIs === 'deleted') {
Object.assign(where, {
2020-07-01 16:05:30 +02:00
'$VideoAbuse.deletedVideo$': {
2020-05-06 17:39:07 +02:00
[Op.not]: null
}
})
}
if (options.predefinedReasonId) {
Object.assign(where, {
predefinedReasons: {
[Op.contains]: [ options.predefinedReasonId ]
}
})
}
return {
attributes: {
include: [
[
2020-04-20 15:15:10 +02:00
// we don't care about this count for deleted videos, so there are not included
literal(
'(' +
'SELECT count(*) ' +
'FROM "videoAbuse" ' +
2020-07-01 16:05:30 +02:00
'WHERE "videoId" = "VideoAbuse"."videoId" ' +
')'
),
'countReportsForVideo'
],
[
2020-04-20 15:15:10 +02:00
// we don't care about this count for deleted videos, so there are not included
literal(
'(' +
'SELECT t.nth ' +
'FROM ( ' +
'SELECT id, ' +
'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
'FROM "videoAbuse" ' +
') t ' +
2020-07-01 16:05:30 +02:00
'WHERE t.id = "VideoAbuse".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" ' +
2020-07-01 16:05:30 +02:00
'WHERE "account"."id" = "AbuseModel"."reporterAccountId" ' +
')'
),
2020-04-20 15:15:10 +02:00
'countReportsForReporter__video'
],
[
literal(
'(' +
'SELECT count(DISTINCT "videoAbuse"."id") ' +
'FROM "videoAbuse" ' +
2020-07-01 16:05:30 +02:00
`WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "AbuseModel"."reporterAccountId" ` +
2020-04-20 15:15:10 +02:00
')'
),
'countReportsForReporter__deletedVideo'
],
[
literal(
'(' +
'SELECT count(DISTINCT "videoAbuse"."id") ' +
'FROM "videoAbuse" ' +
'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
2020-04-20 15:15:10 +02:00
'INNER JOIN "account" ON ' +
2020-07-01 16:05:30 +02:00
'"videoChannel"."accountId" = "VideoAbuse->Video->VideoChannel"."accountId" ' +
`OR "videoChannel"."accountId" = CAST("VideoAbuse"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
2020-04-20 15:15:10 +02:00
')'
),
'countReportsForReportee__video'
],
[
literal(
'(' +
'SELECT count(DISTINCT "videoAbuse"."id") ' +
'FROM "videoAbuse" ' +
2020-07-01 16:05:30 +02:00
`WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "VideoAbuse->Video->VideoChannel"."accountId" ` +
`OR CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = ` +
2020-07-01 16:05:30 +02:00
`CAST("VideoAbuse"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
')'
),
2020-04-20 15:15:10 +02:00
'countReportsForReportee__deletedVideo'
]
]
},
include: [
{
2020-07-01 16:05:30 +02:00
model: AccountModel.scope(AccountScopeNames.SUMMARY),
as: 'ReporterAccount',
required: true,
where: searchAttribute(options.searchReporter, 'name')
},
{
2020-07-01 16:05:30 +02:00
model: AccountModel.scope(AccountScopeNames.SUMMARY),
as: 'FlaggedAccount',
required: true,
where: searchAttribute(options.searchReportee, 'name')
},
{
model: VideoAbuseModel,
required: options.filter === 'video' || !!options.videoIs || videoRequired,
include: [
{
2020-07-01 16:05:30 +02:00
model: VideoModel,
required: videoRequired,
where: searchAttribute(options.searchVideo, 'name'),
include: [
{
2020-07-01 16:05:30 +02:00
model: ThumbnailModel
},
{
model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: false } as SummaryOptions ] }),
where: searchAttribute(options.searchVideoChannel, 'name'),
required: true,
include: [
{
model: AccountModel.scope(AccountScopeNames.SUMMARY),
required: true,
where: searchAttribute(options.searchReportee, 'name')
}
]
},
{
attributes: [ 'id', 'reason', 'unfederated' ],
model: VideoBlacklistModel,
required: onlyBlacklisted
}
]
}
]
}
],
where
}
}
}))
2017-12-12 17:53:50 +01:00
@Table({
2020-07-01 16:05:30 +02:00
tableName: 'abuse',
2017-12-12 17:53:50 +01:00
indexes: [
2017-01-04 20:59:23 +01:00
{
2020-07-01 16:05:30 +02:00
fields: [ 'reporterAccountId' ]
2017-01-04 20:59:23 +01:00
},
{
2020-07-01 16:05:30 +02:00
fields: [ 'flaggedAccountId' ]
2017-01-04 20:59:23 +01:00
}
2017-05-22 20:58:25 +02:00
]
2017-12-12 17:53:50 +01:00
})
2020-07-01 16:05:30 +02:00
export class AbuseModel extends Model<AbuseModel> {
2017-05-22 20:58:25 +02:00
2017-12-12 17:53:50 +01:00
@AllowNull(false)
@Default(null)
2020-07-01 16:05:30 +02:00
@Is('VideoAbuseReason', value => throwIfNotValid(value, isAbuseReasonValid, 'reason'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ABUSES.REASON.max))
2017-12-12 17:53:50 +01:00
reason: string
2017-11-16 17:04:19 +01:00
@AllowNull(false)
@Default(null)
2020-07-01 16:05:30 +02:00
@Is('VideoAbuseState', value => throwIfNotValid(value, isAbuseStateValid, 'state'))
@Column
2020-07-01 16:05:30 +02:00
state: AbuseState
@AllowNull(true)
@Default(null)
2020-07-01 16:05:30 +02:00
@Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isAbuseModerationCommentValid, 'moderationComment', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ABUSES.MODERATION_COMMENT.max))
moderationComment: string
@AllowNull(true)
@Default(null)
@Column(DataType.ARRAY(DataType.INTEGER))
2020-07-01 16:05:30 +02:00
predefinedReasons: AbusePredefinedReasons[]
2017-12-12 17:53:50 +01:00
@CreatedAt
createdAt: Date
2017-11-16 17:04:19 +01:00
2017-12-12 17:53:50 +01:00
@UpdatedAt
updatedAt: Date
2017-05-22 20:58:25 +02:00
2017-12-12 17:53:50 +01:00
@ForeignKey(() => AccountModel)
@Column
reporterAccountId: number
2017-01-04 20:59:23 +01:00
2017-12-12 17:53:50 +01:00
@BelongsTo(() => AccountModel, {
2017-01-04 20:59:23 +01:00
foreignKey: {
2020-07-01 16:05:30 +02:00
name: 'reporterAccountId',
allowNull: true
2017-01-04 20:59:23 +01:00
},
2020-07-01 16:05:30 +02:00
as: 'ReporterAccount',
onDelete: 'set null'
2017-01-04 20:59:23 +01:00
})
2020-07-01 16:05:30 +02:00
ReporterAccount: AccountModel
2017-12-12 17:53:50 +01:00
2020-07-01 16:05:30 +02:00
@ForeignKey(() => AccountModel)
2017-12-12 17:53:50 +01:00
@Column
2020-07-01 16:05:30 +02:00
flaggedAccountId: number
2017-01-04 20:59:23 +01:00
2020-07-01 16:05:30 +02:00
@BelongsTo(() => AccountModel, {
2017-01-04 20:59:23 +01:00
foreignKey: {
2020-07-01 16:05:30 +02:00
name: 'flaggedAccountId',
allowNull: true
2017-01-04 20:59:23 +01:00
},
2020-07-01 16:05:30 +02:00
as: 'FlaggedAccount',
onDelete: 'set null'
2017-01-04 20:59:23 +01:00
})
2020-07-01 16:05:30 +02:00
FlaggedAccount: AccountModel
@HasOne(() => VideoCommentAbuseModel, {
foreignKey: {
name: 'abuseId',
allowNull: false
},
onDelete: 'cascade'
})
VideoCommentAbuse: VideoCommentAbuseModel
2017-12-12 17:53:50 +01:00
2020-07-01 16:05:30 +02:00
@HasOne(() => VideoAbuseModel, {
foreignKey: {
name: 'abuseId',
allowNull: false
},
onDelete: 'cascade'
})
VideoAbuse: VideoAbuseModel
static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MAbuse> {
const videoWhere: WhereOptions = {}
if (videoId) videoWhere.videoId = videoId
if (uuid) videoWhere.deletedVideo = { uuid }
const query = {
2020-07-01 16:05:30 +02:00
include: [
{
model: VideoAbuseModel,
required: true,
where: videoWhere
}
],
where: {
2020-07-01 16:05:30 +02:00
id
}
}
2020-07-01 16:05:30 +02:00
return AbuseModel.findOne(query)
}
2019-08-29 14:31:04 +02:00
static listForApi (parameters: {
2020-01-31 16:56:52 +01:00
start: number
count: number
sort: string
2020-05-06 17:39:07 +02:00
2020-07-01 16:05:30 +02:00
filter?: AbuseFilter
2019-08-29 14:31:04 +02:00
serverAccountId: number
user?: MUserAccountId
2020-05-06 17:39:07 +02:00
id?: number
2020-07-01 16:05:30 +02:00
predefinedReason?: AbusePredefinedReasonsString
state?: AbuseState
videoIs?: AbuseVideoIs
2020-05-06 17:39:07 +02:00
search?: string
searchReporter?: string
searchReportee?: string
searchVideo?: string
searchVideoChannel?: string
2019-08-29 14:31:04 +02:00
}) {
2020-05-06 17:39:07 +02:00
const {
start,
count,
sort,
search,
user,
serverAccountId,
state,
videoIs,
predefinedReason,
2020-05-06 17:39:07 +02:00
searchReportee,
searchVideo,
2020-07-01 16:05:30 +02:00
filter,
2020-05-06 17:39:07 +02:00
searchVideoChannel,
searchReporter,
id
} = parameters
2019-08-29 14:31:04 +02:00
const userAccountId = user ? user.Account.id : undefined
2020-07-01 16:05:30 +02:00
const predefinedReasonId = predefinedReason ? abusePredefinedReasonsMap[predefinedReason] : undefined
2019-08-29 14:31:04 +02:00
2017-12-12 17:53:50 +01:00
const query = {
offset: start,
limit: count,
2018-02-19 09:41:03 +01:00
order: getSort(sort),
2020-07-01 16:05:30 +02:00
col: 'AbuseModel.id',
distinct: true
2017-12-12 17:53:50 +01:00
}
2017-01-04 20:59:23 +01:00
const filters = {
2020-05-06 17:39:07 +02:00
id,
2020-07-01 16:05:30 +02:00
filter,
predefinedReasonId,
2020-05-06 17:39:07 +02:00
search,
state,
videoIs,
searchReportee,
searchVideo,
searchVideoChannel,
searchReporter,
serverAccountId,
userAccountId
}
2020-07-01 16:05:30 +02:00
return AbuseModel
.scope([
{ method: [ ScopeNames.FOR_API, filters ] }
])
.findAndCountAll(query)
2017-12-12 17:53:50 +01:00
.then(({ rows, count }) => {
return { total: count, data: rows }
})
2017-01-04 20:59:23 +01:00
}
2020-07-01 16:05:30 +02:00
toFormattedJSON (this: MAbuseFormattable): Abuse {
const predefinedReasons = AbuseModel.getPredefinedReasonsStrings(this.predefinedReasons)
const countReportsForVideo = this.get('countReportsForVideo') as number
const nthReportForVideo = this.get('nthReportForVideo') as number
2020-04-20 15:15:10 +02:00
const countReportsForReporterVideo = this.get('countReportsForReporter__video') as number
const countReportsForReporterDeletedVideo = this.get('countReportsForReporter__deletedVideo') as number
const countReportsForReporteeVideo = this.get('countReportsForReportee__video') as number
const countReportsForReporteeDeletedVideo = this.get('countReportsForReportee__deletedVideo') as number
2020-07-01 16:05:30 +02:00
let video: VideoAbuse
if (this.VideoAbuse) {
const abuseModel = this.VideoAbuse
const entity = abuseModel.Video || abuseModel.deletedVideo
video = {
id: entity.id,
uuid: entity.uuid,
name: entity.name,
nsfw: entity.nsfw,
startAt: abuseModel.startAt,
endAt: abuseModel.endAt,
deleted: !abuseModel.Video,
blacklisted: abuseModel.Video?.isBlacklisted() || false,
thumbnailPath: abuseModel.Video?.getMiniatureStaticPath(),
channel: abuseModel.Video?.VideoChannel.toFormattedJSON() || abuseModel.deletedVideo?.channel
}
}
2017-12-12 17:53:50 +01:00
return {
id: this.id,
reason: this.reason,
predefinedReasons,
2020-07-01 16:05:30 +02:00
reporterAccount: this.ReporterAccount.toFormattedJSON(),
state: {
id: this.state,
2020-07-01 16:05:30 +02:00
label: AbuseModel.getStateLabel(this.state)
},
2020-07-01 16:05:30 +02:00
moderationComment: this.moderationComment,
2020-07-01 16:05:30 +02:00
video,
comment: null,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
count: countReportsForVideo || 0,
nth: nthReportForVideo || 0,
2020-04-20 15:15:10 +02:00
countReportsForReporter: (countReportsForReporterVideo || 0) + (countReportsForReporterDeletedVideo || 0),
2020-07-01 16:05:30 +02:00
countReportsForReportee: (countReportsForReporteeVideo || 0) + (countReportsForReporteeDeletedVideo || 0),
// FIXME: deprecated in 2.3, remove this
startAt: null,
endAt: null
2017-12-12 17:53:50 +01:00
}
}
2020-07-01 16:05:30 +02:00
toActivityPubObject (this: MAbuseAP): AbuseObject {
const predefinedReasons = AbuseModel.getPredefinedReasonsStrings(this.predefinedReasons)
const object = this.VideoAbuse?.Video?.url || this.VideoCommentAbuse?.VideoComment?.url || this.FlaggedAccount.Actor.url
2020-07-01 16:05:30 +02:00
const startAt = this.VideoAbuse?.startAt
const endAt = this.VideoAbuse?.endAt
2017-12-12 17:53:50 +01:00
return {
type: 'Flag' as 'Flag',
content: this.reason,
2020-07-01 16:05:30 +02:00
object,
tag: predefinedReasons.map(r => ({
type: 'Hashtag' as 'Hashtag',
name: r
})),
startAt,
endAt
2017-12-12 17:53:50 +01:00
}
}
private static getStateLabel (id: number) {
2020-07-01 16:05:30 +02:00
return ABUSE_STATES[id] || 'Unknown'
}
2020-07-01 16:05:30 +02:00
private static getPredefinedReasonsStrings (predefinedReasons: AbusePredefinedReasons[]): AbusePredefinedReasonsString[] {
return (predefinedReasons || [])
2020-07-01 16:05:30 +02:00
.filter(r => r in AbusePredefinedReasons)
.map(r => invert(abusePredefinedReasonsMap)[r] as AbusePredefinedReasonsString)
}
2017-01-04 20:59:23 +01:00
}