X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fabuse%2Fabuse.ts;h=f85f48e86b0d04e707de206ccebd0dc0147046b4;hb=fba911e2c89708a166636e3a93fcd8fcbc3de7e1;hp=3353e9e41c310e42524ef4e40a9d562b343a73e3;hpb=94148c9028829b5576a5dcbfba2c7fb9cf6443d3;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/abuse/abuse.ts b/server/models/abuse/abuse.ts index 3353e9e41..f85f48e86 100644 --- a/server/models/abuse/abuse.ts +++ b/server/models/abuse/abuse.ts @@ -1,6 +1,5 @@ -import * as Bluebird from 'bluebird' import { invert } from 'lodash' -import { literal, Op, QueryTypes, WhereOptions } from 'sequelize' +import { literal, Op, QueryTypes } from 'sequelize' import { AllowNull, BelongsTo, @@ -17,11 +16,11 @@ import { UpdatedAt } from 'sequelize-typescript' import { isAbuseModerationCommentValid, isAbuseReasonValid, isAbuseStateValid } from '@server/helpers/custom-validators/abuses' +import { abusePredefinedReasonsMap } from '@shared/core-utils' import { AbuseFilter, AbuseObject, AbusePredefinedReasons, - abusePredefinedReasonsMap, AbusePredefinedReasonsString, AbuseState, AbuseVideoIs, @@ -31,15 +30,16 @@ import { UserAbuse, UserVideoAbuse } from '@shared/models' +import { AttributesOnly } from '@shared/typescript-utils' import { ABUSE_STATES, CONSTRAINTS_FIELDS } from '../../initializers/constants' -import { MAbuse, MAbuseAdminFormattable, MAbuseAP, MAbuseReporter, MAbuseUserFormattable, MUserAccountId } from '../../types/models' +import { MAbuseAdminFormattable, MAbuseAP, MAbuseFull, MAbuseReporter, MAbuseUserFormattable, MUserAccountId } from '../../types/models' import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account' import { getSort, throwIfNotValid } from '../utils' import { ThumbnailModel } from '../video/thumbnail' -import { VideoModel } from '../video/video' +import { ScopeNames as VideoScopeNames, VideoModel } from '../video/video' import { VideoBlacklistModel } from '../video/video-blacklist' import { ScopeNames as VideoChannelScopeNames, SummaryOptions as ChannelSummaryOptions, VideoChannelModel } from '../video/video-channel' -import { VideoCommentModel } from '../video/video-comment' +import { ScopeNames as CommentScopeNames, VideoCommentModel } from '../video/video-comment' import { buildAbuseListQuery, BuildAbusesQueryOptions } from './abuse-query-builder' import { VideoAbuseModel } from './video-abuse' import { VideoCommentAbuseModel } from './video-comment-abuse' @@ -188,7 +188,7 @@ export enum ScopeNames { } ] }) -export class AbuseModel extends Model { +export class AbuseModel extends Model>> { @AllowNull(false) @Default(null) @@ -265,41 +265,61 @@ export class AbuseModel extends Model { }) VideoAbuse: VideoAbuseModel - // FIXME: deprecated in 2.3. Remove these validators - static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird { - const videoWhere: WhereOptions = {} - - if (videoId) videoWhere.videoId = videoId - if (uuid) videoWhere.deletedVideo = { uuid } - + static loadByIdWithReporter (id: number): Promise { const query = { + where: { + id + }, include: [ - { - model: VideoAbuseModel, - required: true, - where: videoWhere - }, { model: AccountModel, as: 'ReporterAccount' } - ], - where: { - id - } + ] } + return AbuseModel.findOne(query) } - static loadByIdWithReporter (id: number): Bluebird { + static loadFull (id: number): Promise { const query = { where: { id }, include: [ { - model: AccountModel, + model: AccountModel.scope(AccountScopeNames.SUMMARY), + required: false, as: 'ReporterAccount' + }, + { + model: AccountModel.scope(AccountScopeNames.SUMMARY), + as: 'FlaggedAccount' + }, + { + model: VideoAbuseModel, + required: false, + include: [ + { + model: VideoModel.scope([ VideoScopeNames.WITH_ACCOUNT_DETAILS ]) + } + ] + }, + { + model: VideoCommentAbuseModel, + required: false, + include: [ + { + model: VideoCommentModel.scope([ + CommentScopeNames.WITH_ACCOUNT + ]), + include: [ + { + model: VideoModel + } + ] + } + ] } ] } @@ -415,10 +435,10 @@ export class AbuseModel extends Model { } buildBaseVideoCommentAbuse (this: MAbuseUserFormattable) { - if (!this.VideoCommentAbuse) return null + // Associated video comment could have been destroyed if the video has been deleted + if (!this.VideoCommentAbuse || !this.VideoCommentAbuse.VideoComment) return null - const abuseModel = this.VideoCommentAbuse - const entity = abuseModel.VideoComment + const entity = this.VideoCommentAbuse.VideoComment return { id: entity.id, @@ -455,7 +475,7 @@ export class AbuseModel extends Model { blacklisted: abuseModel.Video?.isBlacklisted() || false, thumbnailPath: abuseModel.Video?.getMiniatureStaticPath(), - channel: abuseModel.Video?.VideoChannel.toFormattedJSON() || abuseModel.deletedVideo?.channel, + channel: abuseModel.Video?.VideoChannel.toFormattedJSON() || abuseModel.deletedVideo?.channel } } @@ -515,13 +535,7 @@ export class AbuseModel extends Model { : null, countReportsForReporter: (countReportsForReporter || 0), - countReportsForReportee: (countReportsForReportee || 0), - - // FIXME: deprecated in 2.3, remove this - startAt: null, - endAt: null, - count: countReportsForVideo || 0, - nth: nthReportForVideo || 0 + countReportsForReportee: (countReportsForReportee || 0) }) } @@ -549,6 +563,7 @@ export class AbuseModel extends Model { return { type: 'Flag' as 'Flag', content: this.reason, + mediaType: 'text/markdown', object, tag: predefinedReasons.map(r => ({ type: 'Hashtag' as 'Hashtag', @@ -600,8 +615,10 @@ export class AbuseModel extends Model { } private static getPredefinedReasonsStrings (predefinedReasons: AbusePredefinedReasons[]): AbusePredefinedReasonsString[] { + const invertedPredefinedReasons = invert(abusePredefinedReasonsMap) + return (predefinedReasons || []) - .filter(r => r in AbusePredefinedReasons) - .map(r => invert(abusePredefinedReasonsMap)[r] as AbusePredefinedReasonsString) + .map(r => invertedPredefinedReasons[r] as AbusePredefinedReasonsString) + .filter(v => !!v) } }