]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/abuse/abuse-query-builder.ts
Merge branch 'feature/SO035' into develop
[github/Chocobozzz/PeerTube.git] / server / models / abuse / abuse-query-builder.ts
index 5fddcf3c439d451b4b8267fc11d7ad600c4f75c6..74f4542e55259ff20a47e94bb1e080de3f094ced 100644 (file)
@@ -1,5 +1,6 @@
 
 import { exists } from '@server/helpers/custom-validators/misc'
+import { forceNumber } from '@shared/core-utils'
 import { AbuseFilter, AbuseState, AbuseVideoIs } from '@shared/models'
 import { buildBlockedAccountSQL, buildDirectionAndField } from '../utils'
 
@@ -13,7 +14,7 @@ export type BuildAbusesQueryOptions = {
   searchReporter?: string
   searchReportee?: string
 
-  // video releated
+  // video related
   searchVideo?: string
   searchVideoChannel?: string
   videoIs?: AbuseVideoIs
@@ -26,8 +27,10 @@ export type BuildAbusesQueryOptions = {
   state?: AbuseState
 
   // accountIds
-  serverAccountId: number
-  userAccountId: number
+  serverAccountId?: number
+  userAccountId?: number
+
+  reporterAccountId?: number
 }
 
 function buildAbuseListQuery (options: BuildAbusesQueryOptions, type: 'count' | 'id') {
@@ -40,12 +43,22 @@ function buildAbuseListQuery (options: BuildAbusesQueryOptions, type: 'count' |
     'LEFT JOIN "videoBlacklist" ON "videoBlacklist"."videoId" = "video"."id"',
     'LEFT JOIN "videoChannel" ON "video"."channelId" = "videoChannel"."id"',
     'LEFT JOIN "account" "reporterAccount" ON "reporterAccount"."id" = "abuse"."reporterAccountId"',
-    'LEFT JOIN "account" "flaggedAccount" ON "flaggedAccount"."id" = "abuse"."reporterAccountId"',
+    'LEFT JOIN "account" "flaggedAccount" ON "flaggedAccount"."id" = "abuse"."flaggedAccountId"',
     'LEFT JOIN "commentAbuse" ON "commentAbuse"."abuseId" = "abuse"."id"',
     'LEFT JOIN "videoComment" ON "commentAbuse"."videoCommentId" = "videoComment"."id"'
   ]
 
-  whereAnd.push('"abuse"."reporterAccountId" NOT IN (' + buildBlockedAccountSQL([ options.serverAccountId, options.userAccountId ]) + ')')
+  if (options.serverAccountId || options.userAccountId) {
+    whereAnd.push(
+      '"abuse"."reporterAccountId" IS NULL OR ' +
+      '"abuse"."reporterAccountId" NOT IN (' + buildBlockedAccountSQL([ options.serverAccountId, options.userAccountId ]) + ')'
+    )
+  }
+
+  if (options.reporterAccountId) {
+    whereAnd.push('"abuse"."reporterAccountId" = :reporterAccountId')
+    replacements.reporterAccountId = options.reporterAccountId
+  }
 
   if (options.search) {
     const searchWhereOr = [
@@ -123,12 +136,12 @@ function buildAbuseListQuery (options: BuildAbusesQueryOptions, type: 'count' |
     }
 
     if (exists(options.count)) {
-      const count = parseInt(options.count + '', 10)
+      const count = forceNumber(options.count)
       suffix += `LIMIT ${count} `
     }
 
     if (exists(options.start)) {
-      const start = parseInt(options.start + '', 10)
+      const start = forceNumber(options.start)
       suffix += `OFFSET ${start} `
     }
   }