]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/abuse/abuse-query-builder.ts
Fix abuse crash on deleted reporter account
[github/Chocobozzz/PeerTube.git] / server / models / abuse / abuse-query-builder.ts
CommitLineData
811cef14
C
1
2import { exists } from '@server/helpers/custom-validators/misc'
3import { AbuseFilter, AbuseState, AbuseVideoIs } from '@shared/models'
4import { buildBlockedAccountSQL, buildDirectionAndField } from '../utils'
5
6export type BuildAbusesQueryOptions = {
7 start: number
8 count: number
9 sort: string
10
11 // search
12 search?: string
13 searchReporter?: string
14 searchReportee?: string
15
16 // video releated
17 searchVideo?: string
18 searchVideoChannel?: string
19 videoIs?: AbuseVideoIs
20
21 // filters
22 id?: number
23 predefinedReasonId?: number
24 filter?: AbuseFilter
25
26 state?: AbuseState
27
28 // accountIds
edbc9325
C
29 serverAccountId?: number
30 userAccountId?: number
31
32 reporterAccountId?: number
811cef14
C
33}
34
35function buildAbuseListQuery (options: BuildAbusesQueryOptions, type: 'count' | 'id') {
36 const whereAnd: string[] = []
37 const replacements: any = {}
38
39 const joins = [
40 'LEFT JOIN "videoAbuse" ON "videoAbuse"."abuseId" = "abuse"."id"',
41 'LEFT JOIN "video" ON "videoAbuse"."videoId" = "video"."id"',
42 'LEFT JOIN "videoBlacklist" ON "videoBlacklist"."videoId" = "video"."id"',
43 'LEFT JOIN "videoChannel" ON "video"."channelId" = "videoChannel"."id"',
44 'LEFT JOIN "account" "reporterAccount" ON "reporterAccount"."id" = "abuse"."reporterAccountId"',
60554112 45 'LEFT JOIN "account" "flaggedAccount" ON "flaggedAccount"."id" = "abuse"."flaggedAccountId"',
811cef14
C
46 'LEFT JOIN "commentAbuse" ON "commentAbuse"."abuseId" = "abuse"."id"',
47 'LEFT JOIN "videoComment" ON "commentAbuse"."videoCommentId" = "videoComment"."id"'
48 ]
49
edbc9325 50 if (options.serverAccountId || options.userAccountId) {
82b9a100
C
51 whereAnd.push(
52 '"abuse"."reporterAccountId" IS NULL OR ' +
53 '"abuse"."reporterAccountId" NOT IN (' + buildBlockedAccountSQL([ options.serverAccountId, options.userAccountId ]) + ')'
54 )
edbc9325
C
55 }
56
57 if (options.reporterAccountId) {
58 whereAnd.push('"abuse"."reporterAccountId" = :reporterAccountId')
59 replacements.reporterAccountId = options.reporterAccountId
60 }
811cef14
C
61
62 if (options.search) {
63 const searchWhereOr = [
64 '"video"."name" ILIKE :search',
65 '"videoChannel"."name" ILIKE :search',
66 `"videoAbuse"."deletedVideo"->>'name' ILIKE :search`,
67 `"videoAbuse"."deletedVideo"->'channel'->>'displayName' ILIKE :search`,
68 '"reporterAccount"."name" ILIKE :search',
69 '"flaggedAccount"."name" ILIKE :search'
70 ]
71
72 replacements.search = `%${options.search}%`
73 whereAnd.push('(' + searchWhereOr.join(' OR ') + ')')
74 }
75
76 if (options.searchVideo) {
77 whereAnd.push('"video"."name" ILIKE :searchVideo')
78 replacements.searchVideo = `%${options.searchVideo}%`
79 }
80
81 if (options.searchVideoChannel) {
82 whereAnd.push('"videoChannel"."name" ILIKE :searchVideoChannel')
83 replacements.searchVideoChannel = `%${options.searchVideoChannel}%`
84 }
85
86 if (options.id) {
87 whereAnd.push('"abuse"."id" = :id')
88 replacements.id = options.id
89 }
90
91 if (options.state) {
92 whereAnd.push('"abuse"."state" = :state')
93 replacements.state = options.state
94 }
95
96 if (options.videoIs === 'deleted') {
97 whereAnd.push('"videoAbuse"."deletedVideo" IS NOT NULL')
98 } else if (options.videoIs === 'blacklisted') {
99 whereAnd.push('"videoBlacklist"."id" IS NOT NULL')
100 }
101
102 if (options.predefinedReasonId) {
103 whereAnd.push(':predefinedReasonId = ANY("abuse"."predefinedReasons")')
104 replacements.predefinedReasonId = options.predefinedReasonId
105 }
106
107 if (options.filter === 'video') {
108 whereAnd.push('"videoAbuse"."id" IS NOT NULL')
109 } else if (options.filter === 'comment') {
110 whereAnd.push('"commentAbuse"."id" IS NOT NULL')
111 } else if (options.filter === 'account') {
112 whereAnd.push('"videoAbuse"."id" IS NULL AND "commentAbuse"."id" IS NULL')
113 }
114
115 if (options.searchReporter) {
116 whereAnd.push('"reporterAccount"."name" ILIKE :searchReporter')
117 replacements.searchReporter = `%${options.searchReporter}%`
118 }
119
120 if (options.searchReportee) {
121 whereAnd.push('"flaggedAccount"."name" ILIKE :searchReportee')
122 replacements.searchReportee = `%${options.searchReportee}%`
123 }
124
125 const prefix = type === 'count'
126 ? 'SELECT COUNT("abuse"."id") AS "total"'
127 : 'SELECT "abuse"."id" '
128
129 let suffix = ''
130 if (type !== 'count') {
131
132 if (options.sort) {
133 const order = buildAbuseOrder(options.sort)
134 suffix += `${order} `
135 }
136
137 if (exists(options.count)) {
138 const count = parseInt(options.count + '', 10)
139 suffix += `LIMIT ${count} `
140 }
141
142 if (exists(options.start)) {
143 const start = parseInt(options.start + '', 10)
144 suffix += `OFFSET ${start} `
145 }
146 }
147
148 const where = whereAnd.length !== 0
149 ? `WHERE ${whereAnd.join(' AND ')}`
150 : ''
151
152 return {
153 query: `${prefix} FROM "abuse" ${joins.join(' ')} ${where} ${suffix}`,
154 replacements
155 }
156}
157
158function buildAbuseOrder (value: string) {
159 const { direction, field } = buildDirectionAndField(value)
160
161 return `ORDER BY "abuse"."${field}" ${direction}`
162}
163
164export {
165 buildAbuseListQuery
166}