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

408 lines
12 KiB
TypeScript
Raw Normal View History

import {
AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt, Scopes
} 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'
import {
isVideoAbuseModerationCommentValid,
isVideoAbuseReasonValid,
isVideoAbuseStateValid
} from '../../helpers/custom-validators/video-abuses'
2017-12-12 11:53:50 -05:00
import { AccountModel } from '../account/account'
import { buildBlockedAccountSQL, getSort, throwIfNotValid, searchAttribute, parseQueryStringFilter } from '../utils'
2017-12-12 11:53:50 -05:00
import { VideoModel } from './video'
import { VideoAbuseState, VideoDetails } from '../../../shared'
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'
2020-04-20 09:15:10 -04:00
import { literal, Op } from 'sequelize'
import { ThumbnailModel } from './thumbnail'
import { VideoBlacklistModel } from './video-blacklist'
import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
2017-12-12 11:53:50 -05:00
export enum ScopeNames {
FOR_API = 'FOR_API'
}
@Scopes(() => ({
[ScopeNames.FOR_API]: (options: {
// search
search?: string
searchReporter?: string
searchReportee?: string
searchVideo?: string
searchVideoChannel?: string
// filters
id?: number
state?: VideoAbuseState
is?: any
// accountIds
serverAccountId: number
userAccountId: number
}) => {
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 } },
searchAttribute(options.search, '$Video.name$')
]
},
{
[Op.and]: [
{ videoId: { [Op.not]: null } },
searchAttribute(options.search, '$Video.VideoChannel.name$')
]
},
{
[Op.and]: [
{ deletedVideo: { [Op.not]: null } },
{ deletedVideo: searchAttribute(options.search, 'name') }
]
},
{
[Op.and]: [
{ deletedVideo: { [Op.not]: null } },
{ deletedVideo: { channel: searchAttribute(options.search, 'displayName') } }
]
},
searchAttribute(options.search, '$Account.name$')
]
})
}
if (options.id) {
where = Object.assign(where, {
id: options.id
})
}
if (options.state) {
where = Object.assign(where, {
state: options.state
})
}
if (options.is) {
where = Object.assign(where, {
...options.is
})
}
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" ' +
'WHERE "videoId" = "VideoAbuseModel"."videoId" ' +
')'
),
'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 ' +
'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" ' +
')'
),
2020-04-20 09:15:10 -04:00
'countReportsForReporter__video'
],
[
literal(
'(' +
'SELECT count(DISTINCT "videoAbuse"."id") ' +
'FROM "videoAbuse" ' +
`WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "VideoAbuseModel"."reporterAccountId" ` +
')'
),
'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 09:15:10 -04:00
'INNER JOIN "account" ON ' +
'"videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' +
`OR "videoChannel"."accountId" = CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
')'
),
'countReportsForReportee__video'
],
[
literal(
'(' +
'SELECT count(DISTINCT "videoAbuse"."id") ' +
'FROM "videoAbuse" ' +
`WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "Video->VideoChannel"."accountId" ` +
`OR CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = ` +
`CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
')'
),
2020-04-20 09:15:10 -04:00
'countReportsForReportee__deletedVideo'
]
]
},
include: [
{
model: AccountModel,
required: true,
where: searchAttribute(options.searchReporter, 'name')
},
{
model: VideoModel,
required: false,
where: searchAttribute(options.searchVideo, 'name'),
include: [
{
model: ThumbnailModel
},
{
model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
where: searchAttribute(options.searchVideoChannel, 'name'),
include: [
{
model: AccountModel,
where: searchAttribute(options.searchReportee, 'name')
}
]
},
{
attributes: [ 'id', 'reason', 'unfederated' ],
model: VideoBlacklistModel
}
]
}
],
where
}
}
}))
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)
@Default(null)
2017-12-12 11:53:50 -05:00
@Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
@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
@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))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
moderationComment: string
@AllowNull(true)
@Default(null)
@Column(DataType.JSONB)
deletedVideo: VideoDetails
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: {
allowNull: true
2017-01-04 14:59:23 -05: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: {
allowNull: true
2017-01-04 14:59:23 -05:00
},
onDelete: 'set null'
2017-01-04 14:59:23 -05:00
})
2017-12-12 11:53:50 -05:00
Video: VideoModel
static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
const videoAttributes = {}
if (videoId) videoAttributes['videoId'] = videoId
if (uuid) videoAttributes['deletedVideo'] = { uuid }
const query = {
where: {
id,
...videoAttributes
}
}
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
search?: string
2019-08-29 08:31:04 -04:00
serverAccountId: number
user?: MUserAccountId
}) {
const { start, count, sort, search, user, serverAccountId } = parameters
2019-08-29 08:31:04 -04:00
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),
col: 'VideoAbuseModel.id',
distinct: true
2017-12-12 11:53:50 -05:00
}
2017-01-04 14:59:23 -05:00
const filters = {
...parseQueryStringFilter(search, {
id: {
prefix: '#',
handler: v => v
},
state: {
prefix: 'state:',
handler: v => {
if (v === "accepted") return VideoAbuseState.ACCEPTED
if (v === "pending") return VideoAbuseState.PENDING
if (v === "rejected") return VideoAbuseState.REJECTED
return undefined
}
},
is: {
prefix: 'is:',
handler: v => {
if (v === "deleted") return { deletedVideo: { [Op.not]: null } }
return undefined
}
},
searchReporter: {
prefix: 'reporter:',
handler: v => v
},
searchReportee: {
prefix: 'reportee:',
handler: v => v
}
}),
serverAccountId,
userAccountId
}
return VideoAbuseModel
.scope({ method: [ ScopeNames.FOR_API, filters ] })
.findAndCountAll(query)
2017-12-12 11:53:50 -05:00
.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 {
const countReportsForVideo = this.get('countReportsForVideo') as number
const nthReportForVideo = this.get('nthReportForVideo') as number
2020-04-20 09:15:10 -04: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
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(),
state: {
id: this.state,
label: VideoAbuseModel.getStateLabel(this.state)
},
moderationComment: this.moderationComment,
2018-03-12 06:29:46 -04:00
video: {
id: video.id,
uuid: video.uuid,
name: video.name,
nsfw: video.nsfw,
deleted: !this.Video,
blacklisted: this.Video && this.Video.isBlacklisted(),
thumbnailPath: this.Video?.getMiniatureStaticPath(),
channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel
2018-03-12 06:29:46 -04:00
},
createdAt: this.createdAt,
updatedAt: this.updatedAt,
count: countReportsForVideo || 0,
nth: nthReportForVideo || 0,
2020-04-20 09:15:10 -04:00
countReportsForReporter: (countReportsForReporterVideo || 0) + (countReportsForReporterDeletedVideo || 0),
countReportsForReportee: (countReportsForReporteeVideo || 0) + (countReportsForReporteeDeletedVideo || 0)
2017-12-12 11:53:50 -05:00
}
}
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
}
}
private static getStateLabel (id: number) {
return VIDEO_ABUSE_STATES[id] || 'Unknown'
}
2017-01-04 14:59:23 -05:00
}