1
0
Fork 0
peertube/server/models/abuse/abuse.ts

516 lines
13 KiB
TypeScript
Raw Normal View History

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