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