]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/abuse/abuse-query-builder.ts
Fix player translations
[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
C
50 if (options.serverAccountId || options.userAccountId) {
51 whereAnd.push('"abuse"."reporterAccountId" NOT IN (' + buildBlockedAccountSQL([ options.serverAccountId, options.userAccountId ]) + ')')
52 }
53
54 if (options.reporterAccountId) {
55 whereAnd.push('"abuse"."reporterAccountId" = :reporterAccountId')
56 replacements.reporterAccountId = options.reporterAccountId
57 }
811cef14
C
58
59 if (options.search) {
60 const searchWhereOr = [
61 '"video"."name" ILIKE :search',
62 '"videoChannel"."name" ILIKE :search',
63 `"videoAbuse"."deletedVideo"->>'name' ILIKE :search`,
64 `"videoAbuse"."deletedVideo"->'channel'->>'displayName' ILIKE :search`,
65 '"reporterAccount"."name" ILIKE :search',
66 '"flaggedAccount"."name" ILIKE :search'
67 ]
68
69 replacements.search = `%${options.search}%`
70 whereAnd.push('(' + searchWhereOr.join(' OR ') + ')')
71 }
72
73 if (options.searchVideo) {
74 whereAnd.push('"video"."name" ILIKE :searchVideo')
75 replacements.searchVideo = `%${options.searchVideo}%`
76 }
77
78 if (options.searchVideoChannel) {
79 whereAnd.push('"videoChannel"."name" ILIKE :searchVideoChannel')
80 replacements.searchVideoChannel = `%${options.searchVideoChannel}%`
81 }
82
83 if (options.id) {
84 whereAnd.push('"abuse"."id" = :id')
85 replacements.id = options.id
86 }
87
88 if (options.state) {
89 whereAnd.push('"abuse"."state" = :state')
90 replacements.state = options.state
91 }
92
93 if (options.videoIs === 'deleted') {
94 whereAnd.push('"videoAbuse"."deletedVideo" IS NOT NULL')
95 } else if (options.videoIs === 'blacklisted') {
96 whereAnd.push('"videoBlacklist"."id" IS NOT NULL')
97 }
98
99 if (options.predefinedReasonId) {
100 whereAnd.push(':predefinedReasonId = ANY("abuse"."predefinedReasons")')
101 replacements.predefinedReasonId = options.predefinedReasonId
102 }
103
104 if (options.filter === 'video') {
105 whereAnd.push('"videoAbuse"."id" IS NOT NULL')
106 } else if (options.filter === 'comment') {
107 whereAnd.push('"commentAbuse"."id" IS NOT NULL')
108 } else if (options.filter === 'account') {
109 whereAnd.push('"videoAbuse"."id" IS NULL AND "commentAbuse"."id" IS NULL')
110 }
111
112 if (options.searchReporter) {
113 whereAnd.push('"reporterAccount"."name" ILIKE :searchReporter')
114 replacements.searchReporter = `%${options.searchReporter}%`
115 }
116
117 if (options.searchReportee) {
118 whereAnd.push('"flaggedAccount"."name" ILIKE :searchReportee')
119 replacements.searchReportee = `%${options.searchReportee}%`
120 }
121
122 const prefix = type === 'count'
123 ? 'SELECT COUNT("abuse"."id") AS "total"'
124 : 'SELECT "abuse"."id" '
125
126 let suffix = ''
127 if (type !== 'count') {
128
129 if (options.sort) {
130 const order = buildAbuseOrder(options.sort)
131 suffix += `${order} `
132 }
133
134 if (exists(options.count)) {
135 const count = parseInt(options.count + '', 10)
136 suffix += `LIMIT ${count} `
137 }
138
139 if (exists(options.start)) {
140 const start = parseInt(options.start + '', 10)
141 suffix += `OFFSET ${start} `
142 }
143 }
144
145 const where = whereAnd.length !== 0
146 ? `WHERE ${whereAnd.join(' AND ')}`
147 : ''
148
149 return {
150 query: `${prefix} FROM "abuse" ${joins.join(' ')} ${where} ${suffix}`,
151 replacements
152 }
153}
154
155function buildAbuseOrder (value: string) {
156 const { direction, field } = buildDirectionAndField(value)
157
158 return `ORDER BY "abuse"."${field}" ${direction}`
159}
160
161export {
162 buildAbuseListQuery
163}