diff options
author | Chocobozzz <me@florianbigard.com> | 2020-05-06 10:31:52 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-05-06 10:31:52 +0200 |
commit | fc8aabd0bf38441c0591f21b9b435b52e99ffc23 (patch) | |
tree | 0fd22fd08d9952cd19961d9e01d1237a05eb5bfa /server/models | |
parent | d056b019565539c5d12a302d1968581a186e5514 (diff) | |
download | PeerTube-fc8aabd0bf38441c0591f21b9b435b52e99ffc23.tar.gz PeerTube-fc8aabd0bf38441c0591f21b9b435b52e99ffc23.tar.zst PeerTube-fc8aabd0bf38441c0591f21b9b435b52e99ffc23.zip |
parseQueryStringFilter cleanup
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/utils.ts | 14 | ||||
-rw-r--r-- | server/models/video/video-abuse.ts | 18 |
2 files changed, 13 insertions, 19 deletions
diff --git a/server/models/utils.ts b/server/models/utils.ts index 956562e70..fe4596d31 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts | |||
@@ -231,15 +231,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes) | |||
231 | ? [].concat.apply([], q.split('"').map((v, i) => i % 2 ? v : v.split(' '))).filter(Boolean) // split by space unless using double quotes | 231 | ? [].concat.apply([], q.split('"').map((v, i) => i % 2 ? v : v.split(' '))).filter(Boolean) // split by space unless using double quotes |
232 | : [] | 232 | : [] |
233 | 233 | ||
234 | // TODO: when Typescript supports Object.fromEntries, replace with the Object method | 234 | const objectMap = (obj, fn) => Object.fromEntries( |
235 | function fromEntries<T> (entries: [keyof T, T[keyof T]][]): T { | ||
236 | return entries.reduce( | ||
237 | (acc, [ key, value ]) => ({ ...acc, [key]: value }), | ||
238 | {} as T | ||
239 | ) | ||
240 | } | ||
241 | |||
242 | const objectMap = (obj, fn) => fromEntries( | ||
243 | Object.entries(obj).map( | 235 | Object.entries(obj).map( |
244 | ([ k, v ], i) => [ k, fn(v, k, i) ] | 236 | ([ k, v ], i) => [ k, fn(v, k, i) ] |
245 | ) | 237 | ) |
@@ -248,7 +240,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes) | |||
248 | return { | 240 | return { |
249 | // search is the querystring minus defined filters | 241 | // search is the querystring minus defined filters |
250 | search: tokens.filter(e => !Object.values(prefixes).some(p => { | 242 | search: tokens.filter(e => !Object.values(prefixes).some(p => { |
251 | if (typeof p === "string") { | 243 | if (typeof p === 'string') { |
252 | return e.startsWith(p) | 244 | return e.startsWith(p) |
253 | } else { | 245 | } else { |
254 | return e.startsWith(p.prefix) | 246 | return e.startsWith(p.prefix) |
@@ -256,7 +248,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes) | |||
256 | })).join(' '), | 248 | })).join(' '), |
257 | // filters defined in prefixes are added under their own name | 249 | // filters defined in prefixes are added under their own name |
258 | ...objectMap(prefixes, p => { | 250 | ...objectMap(prefixes, p => { |
259 | if (typeof p === "string") { | 251 | if (typeof p === 'string') { |
260 | return tokens.filter(e => e.startsWith(p)).map(e => e.slice(p.length)) // we keep the matched item, and remove its prefix | 252 | return tokens.filter(e => e.startsWith(p)).map(e => e.slice(p.length)) // we keep the matched item, and remove its prefix |
261 | } else { | 253 | } else { |
262 | const _tokens = tokens.filter(e => e.startsWith(p.prefix)).map(e => e.slice(p.prefix.length)).map(p.handler) | 254 | const _tokens = tokens.filter(e => e.startsWith(p.prefix)).map(e => e.slice(p.prefix.length)).map(p.handler) |
diff --git a/server/models/video/video-abuse.ts b/server/models/video/video-abuse.ts index f6b546825..6cd2c0418 100644 --- a/server/models/video/video-abuse.ts +++ b/server/models/video/video-abuse.ts | |||
@@ -32,10 +32,12 @@ export enum ScopeNames { | |||
32 | searchReportee?: string | 32 | searchReportee?: string |
33 | searchVideo?: string | 33 | searchVideo?: string |
34 | searchVideoChannel?: string | 34 | searchVideoChannel?: string |
35 | |||
35 | // filters | 36 | // filters |
36 | id?: number | 37 | id?: number |
37 | state?: VideoAbuseState | 38 | state?: VideoAbuseState |
38 | is?: any | 39 | is?: 'deleted' | 'blacklisted' |
40 | |||
39 | // accountIds | 41 | // accountIds |
40 | serverAccountId: number | 42 | serverAccountId: number |
41 | userAccountId: number | 43 | userAccountId: number |
@@ -91,11 +93,11 @@ export enum ScopeNames { | |||
91 | } | 93 | } |
92 | 94 | ||
93 | let onlyBlacklisted = false | 95 | let onlyBlacklisted = false |
94 | if (options.is === "deleted") { | 96 | if (options.is === 'deleted') { |
95 | where = Object.assign(where, { | 97 | where = Object.assign(where, { |
96 | deletedVideo: { [Op.not]: null } | 98 | deletedVideo: { [Op.not]: null } |
97 | }) | 99 | }) |
98 | } else if (options.is === "blacklisted") { | 100 | } else if (options.is === 'blacklisted') { |
99 | onlyBlacklisted = true | 101 | onlyBlacklisted = true |
100 | } | 102 | } |
101 | 103 | ||
@@ -323,17 +325,17 @@ export class VideoAbuseModel extends Model<VideoAbuseModel> { | |||
323 | state: { | 325 | state: { |
324 | prefix: 'state:', | 326 | prefix: 'state:', |
325 | handler: v => { | 327 | handler: v => { |
326 | if (v === "accepted") return VideoAbuseState.ACCEPTED | 328 | if (v === 'accepted') return VideoAbuseState.ACCEPTED |
327 | if (v === "pending") return VideoAbuseState.PENDING | 329 | if (v === 'pending') return VideoAbuseState.PENDING |
328 | if (v === "rejected") return VideoAbuseState.REJECTED | 330 | if (v === 'rejected') return VideoAbuseState.REJECTED |
329 | return undefined | 331 | return undefined |
330 | } | 332 | } |
331 | }, | 333 | }, |
332 | is: { | 334 | is: { |
333 | prefix: 'is:', | 335 | prefix: 'is:', |
334 | handler: v => { | 336 | handler: v => { |
335 | if (v === "deleted") return v | 337 | if (v === 'deleted') return v |
336 | if (v === "blacklisted") return v | 338 | if (v === 'blacklisted') return v |
337 | return undefined | 339 | return undefined |
338 | } | 340 | } |
339 | }, | 341 | }, |