X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fabuse%2Fabuse.ts;h=3353e9e41c310e42524ef4e40a9d562b343a73e3;hb=94148c9028829b5576a5dcbfba2c7fb9cf6443d3;hp=dffd503b391ee648decdf1dc8d490160e653313a;hpb=310b5219b38427f0c2c7ba57225afdd8f3064380;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/abuse/abuse.ts b/server/models/abuse/abuse.ts index dffd503b3..3353e9e41 100644 --- a/server/models/abuse/abuse.ts +++ b/server/models/abuse/abuse.ts @@ -18,7 +18,6 @@ import { } from 'sequelize-typescript' import { isAbuseModerationCommentValid, isAbuseReasonValid, isAbuseStateValid } from '@server/helpers/custom-validators/abuses' import { - Abuse, AbuseFilter, AbuseObject, AbusePredefinedReasons, @@ -26,11 +25,14 @@ import { AbusePredefinedReasonsString, AbuseState, AbuseVideoIs, - VideoAbuse, - VideoCommentAbuse + AdminAbuse, + AdminVideoAbuse, + AdminVideoCommentAbuse, + UserAbuse, + UserVideoAbuse } from '@shared/models' import { ABUSE_STATES, CONSTRAINTS_FIELDS } from '../../initializers/constants' -import { MAbuse, MAbuseAP, MAbuseFormattable, MUserAccountId } from '../../types/models' +import { MAbuse, MAbuseAdminFormattable, MAbuseAP, 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' @@ -51,6 +53,16 @@ export enum ScopeNames { return { attributes: { include: [ + [ + literal( + '(' + + 'SELECT count(*) ' + + 'FROM "abuseMessage" ' + + 'WHERE "abuseId" = "AbuseModel"."id"' + + ')' + ), + 'countMessages' + ], [ // we don't care about this count for deleted videos, so there are not included literal( @@ -140,7 +152,7 @@ export enum ScopeNames { model: VideoModel.unscoped(), include: [ { - attributes: [ 'filename', 'fileUrl' ], + attributes: [ 'filename', 'fileUrl', 'type' ], model: ThumbnailModel }, { @@ -254,7 +266,7 @@ export class AbuseModel extends Model { VideoAbuse: VideoAbuseModel // FIXME: deprecated in 2.3. Remove these validators - static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird { + static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird { const videoWhere: WhereOptions = {} if (videoId) videoWhere.videoId = videoId @@ -266,6 +278,10 @@ export class AbuseModel extends Model { model: VideoAbuseModel, required: true, where: videoWhere + }, + { + model: AccountModel, + as: 'ReporterAccount' } ], where: { @@ -275,17 +291,23 @@ export class AbuseModel extends Model { return AbuseModel.findOne(query) } - static loadById (id: number): Bluebird { + static loadByIdWithReporter (id: number): Bluebird { const query = { where: { id - } + }, + include: [ + { + model: AccountModel, + as: 'ReporterAccount' + } + ] } return AbuseModel.findOne(query) } - static async listForApi (parameters: { + static async listForAdminApi (parameters: { start: number count: number sort: string @@ -353,69 +375,98 @@ export class AbuseModel extends Model { return { total, data } } - toFormattedJSON (this: MAbuseFormattable): Abuse { - const predefinedReasons = AbuseModel.getPredefinedReasonsStrings(this.predefinedReasons) + static async listForUserApi (parameters: { + user: MUserAccountId - const countReportsForVideo = this.get('countReportsForVideo') as number - const nthReportForVideo = this.get('nthReportForVideo') as number + start: number + count: number + sort: string - const countReportsForReporter = this.get('countReportsForReporter') as number - const countReportsForReportee = this.get('countReportsForReportee') as number + id?: number + search?: string + state?: AbuseState + }) { + const { + start, + count, + sort, + search, + user, + state, + id + } = parameters - let video: VideoAbuse = null - let comment: VideoCommentAbuse = null + const queryOptions: BuildAbusesQueryOptions = { + start, + count, + sort, + id, + search, + state, + reporterAccountId: user.Account.id + } - if (this.VideoAbuse) { - const abuseModel = this.VideoAbuse - const entity = abuseModel.Video || abuseModel.deletedVideo + const [ total, data ] = await Promise.all([ + AbuseModel.internalCountForApi(queryOptions), + AbuseModel.internalListForApi(queryOptions) + ]) - video = { - id: entity.id, - uuid: entity.uuid, - name: entity.name, - nsfw: entity.nsfw, + return { total, data } + } - startAt: abuseModel.startAt, - endAt: abuseModel.endAt, + buildBaseVideoCommentAbuse (this: MAbuseUserFormattable) { + if (!this.VideoCommentAbuse) return null - deleted: !abuseModel.Video, - blacklisted: abuseModel.Video?.isBlacklisted() || false, - thumbnailPath: abuseModel.Video?.getMiniatureStaticPath(), + const abuseModel = this.VideoCommentAbuse + const entity = abuseModel.VideoComment - channel: abuseModel.Video?.VideoChannel.toFormattedJSON() || abuseModel.deletedVideo?.channel, + return { + id: entity.id, + threadId: entity.getThreadId(), - countReports: countReportsForVideo, - nthReport: nthReportForVideo + text: entity.text ?? '', + + deleted: entity.isDeleted(), + + video: { + id: entity.Video.id, + name: entity.Video.name, + uuid: entity.Video.uuid } } + } - if (this.VideoCommentAbuse) { - const abuseModel = this.VideoCommentAbuse - const entity = abuseModel.VideoComment + buildBaseVideoAbuse (this: MAbuseUserFormattable): UserVideoAbuse { + if (!this.VideoAbuse) return null - comment = { - id: entity.id, - text: entity.text ?? '', + const abuseModel = this.VideoAbuse + const entity = abuseModel.Video || abuseModel.deletedVideo - deleted: entity.isDeleted(), + return { + id: entity.id, + uuid: entity.uuid, + name: entity.name, + nsfw: entity.nsfw, - video: { - id: entity.Video.id, - name: entity.Video.name, - uuid: entity.Video.uuid - } - } + 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, } + } + + buildBaseAbuse (this: MAbuseUserFormattable, countMessages: number): UserAbuse { + const predefinedReasons = AbuseModel.getPredefinedReasonsStrings(this.predefinedReasons) return { id: this.id, reason: this.reason, predefinedReasons, - reporterAccount: this.ReporterAccount - ? this.ReporterAccount.toFormattedJSON() - : null, - flaggedAccount: this.FlaggedAccount ? this.FlaggedAccount.toFormattedJSON() : null, @@ -425,13 +476,43 @@ export class AbuseModel extends Model { label: AbuseModel.getStateLabel(this.state) }, - moderationComment: this.moderationComment, + countMessages, + + createdAt: this.createdAt, + updatedAt: this.updatedAt + } + } + toFormattedAdminJSON (this: MAbuseAdminFormattable): AdminAbuse { + const countReportsForVideo = this.get('countReportsForVideo') as number + const nthReportForVideo = this.get('nthReportForVideo') as number + + const countReportsForReporter = this.get('countReportsForReporter') as number + const countReportsForReportee = this.get('countReportsForReportee') as number + + const countMessages = this.get('countMessages') as number + + const baseVideo = this.buildBaseVideoAbuse() + const video: AdminVideoAbuse = baseVideo + ? Object.assign(baseVideo, { + countReports: countReportsForVideo, + nthReport: nthReportForVideo + }) + : null + + const comment: AdminVideoCommentAbuse = this.buildBaseVideoCommentAbuse() + + const abuse = this.buildBaseAbuse(countMessages || 0) + + return Object.assign(abuse, { video, comment, - createdAt: this.createdAt, - updatedAt: this.updatedAt, + moderationComment: this.moderationComment, + + reporterAccount: this.ReporterAccount + ? this.ReporterAccount.toFormattedJSON() + : null, countReportsForReporter: (countReportsForReporter || 0), countReportsForReportee: (countReportsForReportee || 0), @@ -441,7 +522,20 @@ export class AbuseModel extends Model { endAt: null, count: countReportsForVideo || 0, nth: nthReportForVideo || 0 - } + }) + } + + toFormattedUserJSON (this: MAbuseUserFormattable): UserAbuse { + const countMessages = this.get('countMessages') as number + + const video = this.buildBaseVideoAbuse() + const comment = this.buildBaseVideoCommentAbuse() + const abuse = this.buildBaseAbuse(countMessages || 0) + + return Object.assign(abuse, { + video, + comment + }) } toActivityPubObject (this: MAbuseAP): AbuseObject {