From feb34f6b6b991046aab6a10df747b48fa4da07a7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 6 May 2020 17:39:07 +0200 Subject: Use video abuse filters on client side --- .../video-abuse-list.component.html | 4 +- client/src/app/shared/rest/rest.service.ts | 63 ++++++++++++++++++++-- .../app/shared/video-abuse/video-abuse.service.ts | 31 ++++++++++- 3 files changed, 90 insertions(+), 8 deletions(-) (limited to 'client/src/app') diff --git a/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.html b/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.html index ba05073cf..cffa7a40e 100644 --- a/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.html +++ b/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.html @@ -19,8 +19,8 @@ Unsolved reports Accepted reports Refused reports - Reports with blacklisted videos - Reports with deleted videos + Reports with blacklisted videos + Reports with deleted videos string | number + multiple?: boolean + } +} + +type ParseQueryStringFilterResult = { + [key: string]: string | number | (string | number)[] +} + @Injectable() export class RestService { @@ -53,4 +64,48 @@ export class RestService { return { start, count } } + + parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes): ParseQueryStringFilterResult { + if (!q) return {} + + // Tokenize the strings using spaces + const tokens = q.split(' ').filter(token => !!token) + + // Build prefix array + const prefixeStrings = Object.values(prefixes) + .map(p => p.prefix) + + // Search is the querystring minus defined filters + const searchTokens = tokens.filter(t => { + return prefixeStrings.every(prefixString => t.startsWith(prefixString) === false) + }) + + const additionalFilters: ParseQueryStringFilterResult = {} + + for (const prefixKey of Object.keys(prefixes)) { + const prefixObj = prefixes[prefixKey] + const prefix = prefixObj.prefix + + const matchedTokens = tokens.filter(t => t.startsWith(prefix)) + .map(t => t.slice(prefix.length)) // Keep the value filter + .map(t => { + if (prefixObj.handler) return prefixObj.handler(t) + + return t + }) + .filter(t => !!t) + + if (matchedTokens.length === 0) continue + + additionalFilters[prefixKey] = prefixObj.multiple === true + ? matchedTokens + : matchedTokens[0] + } + + return { + search: searchTokens.join(' '), + + ...additionalFilters + } + } } diff --git a/client/src/app/shared/video-abuse/video-abuse.service.ts b/client/src/app/shared/video-abuse/video-abuse.service.ts index 1ab6b5376..700a30239 100644 --- a/client/src/app/shared/video-abuse/video-abuse.service.ts +++ b/client/src/app/shared/video-abuse/video-abuse.service.ts @@ -3,7 +3,7 @@ import { HttpClient, HttpParams } from '@angular/common/http' import { Injectable } from '@angular/core' import { SortMeta } from 'primeng/api' import { Observable } from 'rxjs' -import { ResultList, VideoAbuse, VideoAbuseUpdate } from '../../../../../shared' +import { ResultList, VideoAbuse, VideoAbuseUpdate, VideoAbuseState } from '../../../../../shared' import { environment } from '../../../environments/environment' import { RestExtractor, RestPagination, RestService } from '../rest' @@ -28,7 +28,34 @@ export class VideoAbuseService { let params = new HttpParams() params = this.restService.addRestGetParams(params, pagination, sort) - if (search) params = params.append('search', search) + if (search) { + const filters = this.restService.parseQueryStringFilter(search, { + id: { prefix: '#' }, + state: { + prefix: 'state:', + handler: v => { + if (v === 'accepted') return VideoAbuseState.ACCEPTED + if (v === 'pending') return VideoAbuseState.PENDING + if (v === 'rejected') return VideoAbuseState.REJECTED + + return undefined + } + }, + videoIs: { + prefix: 'videoIs:', + handler: v => { + if (v === 'deleted') return v + if (v === 'blacklisted') return v + + return undefined + } + }, + searchReporter: { prefix: 'reporter:' }, + searchReportee: { prefix: 'reportee:' } + }) + + params = this.restService.addObjectParams(params, filters) + } return this.authHttp.get>(url, { params }) .pipe( -- cgit v1.2.3