PeerTube/server/models/abuse/abuse.ts

586 lines
15 KiB
TypeScript
Raw Normal View History

2020-05-06 10:39:07 -05:00
import * as Bluebird from 'bluebird'
2020-07-01 09:05:30 -05:00
import { invert } from 'lodash'
import { literal, Op, WhereOptions } from 'sequelize'
import {
2020-05-06 10:39:07 -05:00
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey,
2020-07-01 09:05:30 -05:00
HasOne,
2020-05-06 10:39:07 -05:00
Is,
Model,
Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
2020-07-01 09:05:30 -05:00
import { isAbuseModerationCommentValid, isAbuseReasonValid, isAbuseStateValid } from '@server/helpers/custom-validators/abuses'
import {
2020-07-01 09:05:30 -05:00
Abuse,
2020-07-07 03:57:04 -05:00
AbuseFilter,
2020-07-01 09:05:30 -05:00
AbuseObject,
AbusePredefinedReasons,
abusePredefinedReasonsMap,
AbusePredefinedReasonsString,
AbuseState,
AbuseVideoIs,
2020-07-07 03:57:04 -05:00
VideoAbuse,
VideoCommentAbuse
2020-07-01 09:05:30 -05:00
} from '@shared/models'
2020-07-07 03:57:04 -05:00
import { ABUSE_STATES, CONSTRAINTS_FIELDS } from '../../initializers/constants'
2020-07-01 09:05:30 -05:00
import { MAbuse, MAbuseAP, MAbuseFormattable, MUserAccountId } from '../../types/models'
2020-07-07 07:34:16 -05:00
import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account'
2020-05-06 10:39:07 -05:00
import { buildBlockedAccountSQL, getSort, searchAttribute, throwIfNotValid } from '../utils'
2020-07-01 09:05:30 -05:00
import { ThumbnailModel } from '../video/thumbnail'
import { VideoModel } from '../video/video'
import { VideoBlacklistModel } from '../video/video-blacklist'
2020-07-07 07:34:16 -05:00
import { ScopeNames as VideoChannelScopeNames, SummaryOptions as ChannelSummaryOptions, VideoChannelModel } from '../video/video-channel'
import { VideoCommentModel } from '../video/video-comment'
2020-07-01 09:05:30 -05:00
import { VideoAbuseModel } from './video-abuse'
import { VideoCommentAbuseModel } from './video-comment-abuse'
2017-12-12 10:53:50 -06:00
export enum ScopeNames {
FOR_API = 'FOR_API'
}
@Scopes(() => ({
[ScopeNames.FOR_API]: (options: {
// search
search?: string
searchReporter?: string
searchReportee?: string
2020-07-01 09:05:30 -05:00
// video releated
searchVideo?: string
searchVideoChannel?: string
2020-07-01 09:05:30 -05:00
videoIs?: AbuseVideoIs
2020-05-06 03:31:52 -05:00
// filters
id?: number
predefinedReasonId?: number
2020-07-01 09:05:30 -05:00
filter?: AbuseFilter
2020-05-06 10:39:07 -05:00
2020-07-01 09:05:30 -05:00
state?: AbuseState
2020-05-06 03:31:52 -05:00
// accountIds
serverAccountId: number
userAccountId: number
}) => {
2020-07-07 03:57:04 -05:00
const whereAnd: WhereOptions[] = []
2020-07-01 09:05:30 -05:00
2020-07-07 03:57:04 -05:00
whereAnd.push({
reporterAccountId: {
[Op.notIn]: literal('(' + buildBlockedAccountSQL([ options.serverAccountId, options.userAccountId ]) + ')')
}
2020-07-07 03:57:04 -05:00
})
if (options.search) {
2020-07-01 09:05:30 -05:00
const escapedSearch = AbuseModel.sequelize.escape('%' + options.search + '%')
2020-07-07 03:57:04 -05:00
whereAnd.push({
[Op.or]: [
{
[Op.and]: [
2020-07-01 09:05:30 -05:00
{ '$VideoAbuse.videoId$': { [Op.not]: null } },
searchAttribute(options.search, '$VideoAbuse.Video.name$')
]
},
{
[Op.and]: [
2020-07-01 09:05:30 -05:00
{ '$VideoAbuse.videoId$': { [Op.not]: null } },
searchAttribute(options.search, '$VideoAbuse.Video.VideoChannel.name$')
]
},
{
[Op.and]: [
2020-07-01 09:05:30 -05:00
{ '$VideoAbuse.deletedVideo$': { [Op.not]: null } },
literal(`"VideoAbuse"."deletedVideo"->>'name' ILIKE ${escapedSearch}`)
]
},
{
[Op.and]: [
2020-07-01 09:05:30 -05:00
{ '$VideoAbuse.deletedVideo$': { [Op.not]: null } },
literal(`"VideoAbuse"."deletedVideo"->'channel'->>'displayName' ILIKE ${escapedSearch}`)
]
},
2020-07-01 09:05:30 -05:00
searchAttribute(options.search, '$ReporterAccount.name$'),
searchAttribute(options.search, '$FlaggedAccount.name$')
]
})
}
2020-07-07 03:57:04 -05:00
if (options.id) whereAnd.push({ id: options.id })
if (options.state) whereAnd.push({ state: options.state })
2020-05-06 10:39:07 -05:00
if (options.videoIs === 'deleted') {
2020-07-07 03:57:04 -05:00
whereAnd.push({
2020-07-01 09:05:30 -05:00
'$VideoAbuse.deletedVideo$': {
2020-05-06 10:39:07 -05:00
[Op.not]: null
}
})
}
if (options.predefinedReasonId) {
2020-07-07 03:57:04 -05:00
whereAnd.push({
predefinedReasons: {
[Op.contains]: [ options.predefinedReasonId ]
}
})
}
2020-07-07 03:57:04 -05:00
if (options.filter === 'account') {
whereAnd.push({
videoId: null,
commentId: null
})
}
const onlyBlacklisted = options.videoIs === 'blacklisted'
const videoRequired = !!(onlyBlacklisted || options.searchVideo || options.searchVideoChannel)
return {
attributes: {
include: [
[
2020-04-20 08:15:10 -05:00
// we don't care about this count for deleted videos, so there are not included
literal(
'(' +
'SELECT count(*) ' +
'FROM "videoAbuse" ' +
2020-07-07 07:34:16 -05:00
'WHERE "videoId" = "VideoAbuse"."videoId" AND "videoId" IS NOT NULL' +
')'
),
'countReportsForVideo'
],
[
2020-04-20 08:15:10 -05: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-07 07:34:16 -05:00
'WHERE t.id = "VideoAbuse".id AND t.id IS NOT NULL' +
')'
),
'nthReportForVideo'
],
[
literal(
'(' +
2020-07-07 07:34:16 -05:00
'SELECT count("abuse"."id") ' +
'FROM "abuse" ' +
'WHERE "abuse"."reporterAccountId" = "AbuseModel"."reporterAccountId"' +
2020-04-20 08:15:10 -05:00
')'
),
2020-07-07 07:34:16 -05:00
'countReportsForReporter'
2020-04-20 08:15:10 -05:00
],
[
literal(
'(' +
2020-07-07 07:34:16 -05:00
'SELECT count("abuse"."id") ' +
'FROM "abuse" ' +
'WHERE "abuse"."flaggedAccountId" = "AbuseModel"."flaggedAccountId"' +
')'
),
2020-07-07 07:34:16 -05:00
'countReportsForReportee'
]
]
},
include: [
{
2020-07-01 09:05:30 -05:00
model: AccountModel.scope(AccountScopeNames.SUMMARY),
as: 'ReporterAccount',
2020-07-07 07:34:16 -05:00
required: !!options.searchReporter,
where: searchAttribute(options.searchReporter, 'name')
},
{
2020-07-07 07:34:16 -05:00
model: AccountModel.scope({
method: [
AccountScopeNames.SUMMARY,
{ actorRequired: false } as AccountSummaryOptions
]
}),
2020-07-01 09:05:30 -05:00
as: 'FlaggedAccount',
2020-07-07 07:34:16 -05:00
required: !!options.searchReportee,
2020-07-01 09:05:30 -05:00
where: searchAttribute(options.searchReportee, 'name')
},
2020-07-07 03:57:04 -05:00
{
model: VideoCommentAbuseModel.unscoped(),
required: options.filter === 'comment',
include: [
{
model: VideoCommentModel.unscoped(),
required: false,
include: [
{
model: VideoModel.unscoped(),
2020-07-07 07:34:16 -05:00
attributes: [ 'name', 'id', 'uuid' ]
2020-07-07 03:57:04 -05:00
}
]
}
]
},
2020-07-01 09:05:30 -05:00
{
2020-07-07 07:34:16 -05:00
model: VideoAbuseModel.unscoped(),
2020-07-01 09:05:30 -05:00
required: options.filter === 'video' || !!options.videoIs || videoRequired,
include: [
{
2020-07-07 07:34:16 -05:00
attributes: [ 'id', 'uuid', 'name', 'nsfw' ],
model: VideoModel.unscoped(),
2020-07-01 09:05:30 -05:00
required: videoRequired,
where: searchAttribute(options.searchVideo, 'name'),
include: [
{
2020-07-07 07:34:16 -05:00
attributes: [ 'filename', 'fileUrl' ],
2020-07-01 09:05:30 -05:00
model: ThumbnailModel
},
{
2020-07-07 07:34:16 -05:00
model: VideoChannelModel.scope({
method: [
VideoChannelScopeNames.SUMMARY,
{ withAccount: false, actorRequired: false } as ChannelSummaryOptions
]
}),
2020-07-01 09:05:30 -05:00
where: searchAttribute(options.searchVideoChannel, 'name'),
2020-07-07 07:34:16 -05:00
required: !!options.searchVideoChannel
2020-07-01 09:05:30 -05:00
},
{
attributes: [ 'id', 'reason', 'unfederated' ],
model: VideoBlacklistModel,
required: onlyBlacklisted
}
]
}
]
}
],
2020-07-07 03:57:04 -05:00
where: {
[Op.and]: whereAnd
}
}
}
}))
2017-12-12 10:53:50 -06:00
@Table({
2020-07-01 09:05:30 -05:00
tableName: 'abuse',
2017-12-12 10:53:50 -06:00
indexes: [
2017-01-04 13:59:23 -06:00
{
2020-07-01 09:05:30 -05:00
fields: [ 'reporterAccountId' ]
2017-01-04 13:59:23 -06:00
},
{
2020-07-01 09:05:30 -05:00
fields: [ 'flaggedAccountId' ]
2017-01-04 13:59:23 -06:00
}
2017-05-22 13:58:25 -05:00
]
2017-12-12 10:53:50 -06:00
})
2020-07-01 09:05:30 -05:00
export class AbuseModel extends Model<AbuseModel> {
2017-05-22 13:58:25 -05:00
2017-12-12 10:53:50 -06:00
@AllowNull(false)
@Default(null)
2020-07-07 07:34:16 -05:00
@Is('AbuseReason', value => throwIfNotValid(value, isAbuseReasonValid, 'reason'))
2020-07-01 09:05:30 -05:00
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ABUSES.REASON.max))
2017-12-12 10:53:50 -06:00
reason: string
2017-11-16 10:04:19 -06:00
@AllowNull(false)
@Default(null)
2020-07-07 07:34:16 -05:00
@Is('AbuseState', value => throwIfNotValid(value, isAbuseStateValid, 'state'))
@Column
2020-07-01 09:05:30 -05:00
state: AbuseState
@AllowNull(true)
@Default(null)
2020-07-07 07:34:16 -05:00
@Is('AbuseModerationComment', value => throwIfNotValid(value, isAbuseModerationCommentValid, 'moderationComment', true))
2020-07-01 09:05:30 -05:00
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ABUSES.MODERATION_COMMENT.max))
moderationComment: string
@AllowNull(true)
@Default(null)
@Column(DataType.ARRAY(DataType.INTEGER))
2020-07-01 09:05:30 -05:00
predefinedReasons: AbusePredefinedReasons[]
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-07-01 09:05:30 -05:00
name: 'reporterAccountId',
allowNull: true
2017-01-04 13:59:23 -06:00
},
2020-07-01 09:05:30 -05:00
as: 'ReporterAccount',
onDelete: 'set null'
2017-01-04 13:59:23 -06:00
})
2020-07-01 09:05:30 -05:00
ReporterAccount: AccountModel
2017-12-12 10:53:50 -06:00
2020-07-01 09:05:30 -05:00
@ForeignKey(() => AccountModel)
2017-12-12 10:53:50 -06:00
@Column
2020-07-01 09:05:30 -05:00
flaggedAccountId: number
2017-01-04 13:59:23 -06:00
2020-07-01 09:05:30 -05:00
@BelongsTo(() => AccountModel, {
2017-01-04 13:59:23 -06:00
foreignKey: {
2020-07-01 09:05:30 -05:00
name: 'flaggedAccountId',
allowNull: true
2017-01-04 13:59:23 -06:00
},
2020-07-01 09:05:30 -05:00
as: 'FlaggedAccount',
onDelete: 'set null'
2017-01-04 13:59:23 -06:00
})
2020-07-01 09:05:30 -05:00
FlaggedAccount: AccountModel
@HasOne(() => VideoCommentAbuseModel, {
foreignKey: {
name: 'abuseId',
allowNull: false
},
onDelete: 'cascade'
})
VideoCommentAbuse: VideoCommentAbuseModel
2017-12-12 10:53:50 -06:00
2020-07-01 09:05:30 -05:00
@HasOne(() => VideoAbuseModel, {
foreignKey: {
name: 'abuseId',
allowNull: false
},
onDelete: 'cascade'
})
VideoAbuse: VideoAbuseModel
2020-07-07 03:57:04 -05:00
// FIXME: deprecated in 2.3. Remove these validators
2020-07-01 09:05:30 -05:00
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 09:05:30 -05:00
include: [
{
model: VideoAbuseModel,
required: true,
where: videoWhere
}
],
where: {
2020-07-01 09:05:30 -05:00
id
}
}
2020-07-01 09:05:30 -05:00
return AbuseModel.findOne(query)
}
2020-07-07 03:57:04 -05:00
static loadById (id: number): Bluebird<MAbuse> {
const query = {
where: {
id
}
}
return AbuseModel.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-05-06 10:39:07 -05:00
2020-07-01 09:05:30 -05:00
filter?: AbuseFilter
2019-08-29 07:31:04 -05:00
serverAccountId: number
user?: MUserAccountId
2020-05-06 10:39:07 -05:00
id?: number
2020-07-01 09:05:30 -05:00
predefinedReason?: AbusePredefinedReasonsString
state?: AbuseState
videoIs?: AbuseVideoIs
2020-05-06 10:39:07 -05:00
search?: string
searchReporter?: string
searchReportee?: string
searchVideo?: string
searchVideoChannel?: string
2019-08-29 07:31:04 -05:00
}) {
2020-05-06 10:39:07 -05:00
const {
start,
count,
sort,
search,
user,
serverAccountId,
state,
videoIs,
predefinedReason,
2020-05-06 10:39:07 -05:00
searchReportee,
searchVideo,
2020-07-01 09:05:30 -05:00
filter,
2020-05-06 10:39:07 -05:00
searchVideoChannel,
searchReporter,
id
} = parameters
2019-08-29 07:31:04 -05:00
const userAccountId = user ? user.Account.id : undefined
2020-07-01 09:05:30 -05:00
const predefinedReasonId = predefinedReason ? abusePredefinedReasonsMap[predefinedReason] : undefined
2019-08-29 07:31:04 -05:00
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-07-01 09:05:30 -05:00
col: 'AbuseModel.id',
distinct: true
2017-12-12 10:53:50 -06:00
}
2017-01-04 13:59:23 -06:00
const filters = {
2020-05-06 10:39:07 -05:00
id,
2020-07-01 09:05:30 -05:00
filter,
predefinedReasonId,
2020-05-06 10:39:07 -05:00
search,
state,
videoIs,
searchReportee,
searchVideo,
searchVideoChannel,
searchReporter,
serverAccountId,
userAccountId
}
2020-07-01 09:05:30 -05:00
return AbuseModel
.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
}
2020-07-01 09:05:30 -05:00
toFormattedJSON (this: MAbuseFormattable): Abuse {
const predefinedReasons = AbuseModel.getPredefinedReasonsStrings(this.predefinedReasons)
2020-07-07 07:34:16 -05:00
const countReportsForVideo = this.get('countReportsForVideo') as number
const nthReportForVideo = this.get('nthReportForVideo') as number
2020-07-07 07:34:16 -05:00
const countReportsForReporter = this.get('countReportsForReporter') as number
const countReportsForReportee = this.get('countReportsForReportee') as number
2020-07-01 09:05:30 -05:00
let video: VideoAbuse
2020-07-07 03:57:04 -05:00
let comment: VideoCommentAbuse
2020-07-01 09:05:30 -05:00
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(),
2020-07-07 07:34:16 -05:00
channel: abuseModel.Video?.VideoChannel.toFormattedJSON() || abuseModel.deletedVideo?.channel,
countReports: countReportsForVideo,
nthReport: nthReportForVideo
2020-07-01 09:05:30 -05:00
}
}
2020-07-07 03:57:04 -05:00
if (this.VideoCommentAbuse) {
const abuseModel = this.VideoCommentAbuse
const entity = abuseModel.VideoComment || abuseModel.deletedComment
comment = {
id: entity.id,
text: entity.text,
deleted: !abuseModel.VideoComment,
video: {
id: entity.Video.id,
name: entity.Video.name,
uuid: entity.Video.uuid
}
}
}
2017-12-12 10:53:50 -06:00
return {
id: this.id,
reason: this.reason,
predefinedReasons,
2020-07-01 09:05:30 -05:00
2020-07-07 07:34:16 -05:00
reporterAccount: this.ReporterAccount
? this.ReporterAccount.toFormattedJSON()
: null,
flaggedAccount: this.FlaggedAccount
? this.FlaggedAccount.toFormattedJSON()
: null,
2020-07-01 09:05:30 -05:00
state: {
id: this.state,
2020-07-01 09:05:30 -05:00
label: AbuseModel.getStateLabel(this.state)
},
2020-07-01 09:05:30 -05:00
moderationComment: this.moderationComment,
2020-07-01 09:05:30 -05:00
video,
2020-07-07 03:57:04 -05:00
comment,
2020-07-01 09:05:30 -05:00
createdAt: this.createdAt,
updatedAt: this.updatedAt,
2020-07-07 07:34:16 -05:00
countReportsForReporter: (countReportsForReporter || 0),
countReportsForReportee: (countReportsForReportee || 0),
2020-07-01 09:05:30 -05:00
// FIXME: deprecated in 2.3, remove this
startAt: null,
2020-07-07 07:34:16 -05:00
endAt: null,
count: countReportsForVideo || 0,
nth: nthReportForVideo || 0
2017-12-12 10:53:50 -06:00
}
}
2020-07-01 09:05:30 -05: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 09:05:30 -05:00
const startAt = this.VideoAbuse?.startAt
const endAt = this.VideoAbuse?.endAt
2017-12-12 10:53:50 -06:00
return {
type: 'Flag' as 'Flag',
content: this.reason,
2020-07-01 09:05:30 -05:00
object,
tag: predefinedReasons.map(r => ({
type: 'Hashtag' as 'Hashtag',
name: r
})),
startAt,
endAt
2017-12-12 10:53:50 -06:00
}
}
private static getStateLabel (id: number) {
2020-07-01 09:05:30 -05:00
return ABUSE_STATES[id] || 'Unknown'
}
2020-07-01 09:05:30 -05:00
private static getPredefinedReasonsStrings (predefinedReasons: AbusePredefinedReasons[]): AbusePredefinedReasonsString[] {
return (predefinedReasons || [])
2020-07-01 09:05:30 -05:00
.filter(r => r in AbusePredefinedReasons)
.map(r => invert(abusePredefinedReasonsMap)[r] as AbusePredefinedReasonsString)
}
2017-01-04 13:59:23 -06:00
}