parseQueryStringFilter cleanup

This commit is contained in:
Chocobozzz 2020-05-06 10:31:52 +02:00
parent d056b01956
commit fc8aabd0bf
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 16 additions and 20 deletions

View File

@ -231,15 +231,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes)
? [].concat.apply([], q.split('"').map((v, i) => i % 2 ? v : v.split(' '))).filter(Boolean) // split by space unless using double quotes ? [].concat.apply([], q.split('"').map((v, i) => i % 2 ? v : v.split(' '))).filter(Boolean) // split by space unless using double quotes
: [] : []
// TODO: when Typescript supports Object.fromEntries, replace with the Object method const objectMap = (obj, fn) => Object.fromEntries(
function fromEntries<T> (entries: [keyof T, T[keyof T]][]): T {
return entries.reduce(
(acc, [ key, value ]) => ({ ...acc, [key]: value }),
{} as T
)
}
const objectMap = (obj, fn) => fromEntries(
Object.entries(obj).map( Object.entries(obj).map(
([ k, v ], i) => [ k, fn(v, k, i) ] ([ k, v ], i) => [ k, fn(v, k, i) ]
) )
@ -248,7 +240,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes)
return { return {
// search is the querystring minus defined filters // search is the querystring minus defined filters
search: tokens.filter(e => !Object.values(prefixes).some(p => { search: tokens.filter(e => !Object.values(prefixes).some(p => {
if (typeof p === "string") { if (typeof p === 'string') {
return e.startsWith(p) return e.startsWith(p)
} else { } else {
return e.startsWith(p.prefix) return e.startsWith(p.prefix)
@ -256,7 +248,7 @@ function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes)
})).join(' '), })).join(' '),
// filters defined in prefixes are added under their own name // filters defined in prefixes are added under their own name
...objectMap(prefixes, p => { ...objectMap(prefixes, p => {
if (typeof p === "string") { if (typeof p === 'string') {
return tokens.filter(e => e.startsWith(p)).map(e => e.slice(p.length)) // we keep the matched item, and remove its prefix return tokens.filter(e => e.startsWith(p)).map(e => e.slice(p.length)) // we keep the matched item, and remove its prefix
} else { } else {
const _tokens = tokens.filter(e => e.startsWith(p.prefix)).map(e => e.slice(p.prefix.length)).map(p.handler) const _tokens = tokens.filter(e => e.startsWith(p.prefix)).map(e => e.slice(p.prefix.length)).map(p.handler)

View File

@ -32,10 +32,12 @@ export enum ScopeNames {
searchReportee?: string searchReportee?: string
searchVideo?: string searchVideo?: string
searchVideoChannel?: string searchVideoChannel?: string
// filters // filters
id?: number id?: number
state?: VideoAbuseState state?: VideoAbuseState
is?: any is?: 'deleted' | 'blacklisted'
// accountIds // accountIds
serverAccountId: number serverAccountId: number
userAccountId: number userAccountId: number
@ -91,11 +93,11 @@ export enum ScopeNames {
} }
let onlyBlacklisted = false let onlyBlacklisted = false
if (options.is === "deleted") { if (options.is === 'deleted') {
where = Object.assign(where, { where = Object.assign(where, {
deletedVideo: { [Op.not]: null } deletedVideo: { [Op.not]: null }
}) })
} else if (options.is === "blacklisted") { } else if (options.is === 'blacklisted') {
onlyBlacklisted = true onlyBlacklisted = true
} }
@ -323,17 +325,17 @@ export class VideoAbuseModel extends Model<VideoAbuseModel> {
state: { state: {
prefix: 'state:', prefix: 'state:',
handler: v => { handler: v => {
if (v === "accepted") return VideoAbuseState.ACCEPTED if (v === 'accepted') return VideoAbuseState.ACCEPTED
if (v === "pending") return VideoAbuseState.PENDING if (v === 'pending') return VideoAbuseState.PENDING
if (v === "rejected") return VideoAbuseState.REJECTED if (v === 'rejected') return VideoAbuseState.REJECTED
return undefined return undefined
} }
}, },
is: { is: {
prefix: 'is:', prefix: 'is:',
handler: v => { handler: v => {
if (v === "deleted") return v if (v === 'deleted') return v
if (v === "blacklisted") return v if (v === 'blacklisted') return v
return undefined return undefined
} }
}, },

View File

@ -12,7 +12,9 @@
"dom", "dom",
"es2015", "es2015",
"es2016", "es2016",
"es2017" "es2017",
"es2018",
"es2019"
], ],
"typeRoots": [ "typeRoots": [
"node_modules/sitemap/node_modules/@types", "node_modules/sitemap/node_modules/@types",