]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/abuse/abuse.ts
Upgrade sequelize to v6
[github/Chocobozzz/PeerTube.git] / server / models / abuse / abuse.ts
index 7002502d547436880ecda35abc2d7d97299d3897..de249d21144454a6a81bd8062f9ea627eb525f20 100644 (file)
@@ -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,29 +16,29 @@ import {
   UpdatedAt
 } from 'sequelize-typescript'
 import { isAbuseModerationCommentValid, isAbuseReasonValid, isAbuseStateValid } from '@server/helpers/custom-validators/abuses'
+import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse'
 import {
   AbuseFilter,
   AbuseObject,
   AbusePredefinedReasons,
-  abusePredefinedReasonsMap,
   AbusePredefinedReasonsString,
   AbuseState,
   AbuseVideoIs,
-  AdminVideoAbuse,
   AdminAbuse,
+  AdminVideoAbuse,
   AdminVideoCommentAbuse,
   UserAbuse,
   UserVideoAbuse
 } from '@shared/models'
 import { ABUSE_STATES, CONSTRAINTS_FIELDS } from '../../initializers/constants'
-import { MAbuse, MAbuseAdminFormattable, MAbuseAP, MUserAccountId, MAbuseUserFormattable } 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 +187,7 @@ export enum ScopeNames {
     }
   ]
 })
-export class AbuseModel extends Model<AbuseModel> {
+export class AbuseModel extends Model {
 
   @AllowNull(false)
   @Default(null)
@@ -265,33 +264,63 @@ export class AbuseModel extends Model<AbuseModel> {
   })
   VideoAbuse: VideoAbuseModel
 
-  // FIXME: deprecated in 2.3. Remove these validators
-  static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MAbuse> {
-    const videoWhere: WhereOptions = {}
-
-    if (videoId) videoWhere.videoId = videoId
-    if (uuid) videoWhere.deletedVideo = { uuid }
-
+  static loadByIdWithReporter (id: number): Promise<MAbuseReporter> {
     const query = {
+      where: {
+        id
+      },
       include: [
         {
-          model: VideoAbuseModel,
-          required: true,
-          where: videoWhere
+          model: AccountModel,
+          as: 'ReporterAccount'
         }
-      ],
-      where: {
-        id
-      }
+      ]
     }
+
     return AbuseModel.findOne(query)
   }
 
-  static loadById (id: number): Bluebird<MAbuse> {
+  static loadFull (id: number): Promise<MAbuseFull> {
     const query = {
       where: {
         id
-      }
+      },
+      include: [
+        {
+          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
+                }
+              ]
+            }
+          ]
+        }
+      ]
     }
 
     return AbuseModel.findOne(query)
@@ -445,7 +474,7 @@ export class AbuseModel extends Model<AbuseModel> {
       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
     }
   }
 
@@ -466,8 +495,6 @@ export class AbuseModel extends Model<AbuseModel> {
         label: AbuseModel.getStateLabel(this.state)
       },
 
-      moderationComment: this.moderationComment,
-
       countMessages,
 
       createdAt: this.createdAt,
@@ -500,18 +527,14 @@ export class AbuseModel extends Model<AbuseModel> {
       video,
       comment,
 
+      moderationComment: this.moderationComment,
+
       reporterAccount: this.ReporterAccount
         ? this.ReporterAccount.toFormattedJSON()
         : 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)
     })
   }
 
@@ -519,7 +542,7 @@ export class AbuseModel extends Model<AbuseModel> {
     const countMessages = this.get('countMessages') as number
 
     const video = this.buildBaseVideoAbuse()
-    const comment: AdminVideoCommentAbuse = this.buildBaseVideoCommentAbuse()
+    const comment = this.buildBaseVideoCommentAbuse()
     const abuse = this.buildBaseAbuse(countMessages || 0)
 
     return Object.assign(abuse, {
@@ -590,8 +613,10 @@ export class AbuseModel extends Model<AbuseModel> {
   }
 
   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)
   }
 }