]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { omit } from 'lodash-es' |
2 | import { SortMeta } from 'primeng/api' | |
3 | import { Observable } from 'rxjs' | |
db400f44 | 4 | import { catchError, map } from 'rxjs/operators' |
d592e0a9 | 5 | import { HttpClient, HttpParams } from '@angular/common/http' |
63c4db6d | 6 | import { Injectable } from '@angular/core' |
67ed6552 | 7 | import { RestExtractor, RestPagination, RestService } from '@app/core' |
d95d1559 | 8 | import { AbuseUpdate, ResultList, Abuse, AbuseCreate, AbuseState } from '@shared/models' |
63c4db6d | 9 | import { environment } from '../../../environments/environment' |
11ac88de C |
10 | |
11 | @Injectable() | |
d95d1559 C |
12 | export class AbuseService { |
13 | private static BASE_ABUSE_URL = environment.apiUrl + '/api/v1/abuses' | |
11ac88de | 14 | |
df98563e | 15 | constructor ( |
d592e0a9 C |
16 | private authHttp: HttpClient, |
17 | private restService: RestService, | |
11ac88de C |
18 | private restExtractor: RestExtractor |
19 | ) {} | |
20 | ||
d95d1559 | 21 | getAbuses (options: { |
844db39e RK |
22 | pagination: RestPagination, |
23 | sort: SortMeta, | |
24 | search?: string | |
d95d1559 | 25 | }): Observable<ResultList<Abuse>> { |
844db39e | 26 | const { pagination, sort, search } = options |
d95d1559 | 27 | const url = AbuseService.BASE_ABUSE_URL + 'abuse' |
d592e0a9 C |
28 | |
29 | let params = new HttpParams() | |
30 | params = this.restService.addRestGetParams(params, pagination, sort) | |
31 | ||
feb34f6b C |
32 | if (search) { |
33 | const filters = this.restService.parseQueryStringFilter(search, { | |
34 | id: { prefix: '#' }, | |
35 | state: { | |
36 | prefix: 'state:', | |
37 | handler: v => { | |
d95d1559 C |
38 | if (v === 'accepted') return AbuseState.ACCEPTED |
39 | if (v === 'pending') return AbuseState.PENDING | |
40 | if (v === 'rejected') return AbuseState.REJECTED | |
feb34f6b C |
41 | |
42 | return undefined | |
43 | } | |
44 | }, | |
45 | videoIs: { | |
46 | prefix: 'videoIs:', | |
47 | handler: v => { | |
48 | if (v === 'deleted') return v | |
49 | if (v === 'blacklisted') return v | |
50 | ||
51 | return undefined | |
52 | } | |
53 | }, | |
54 | searchReporter: { prefix: 'reporter:' }, | |
1ebddadd RK |
55 | searchReportee: { prefix: 'reportee:' }, |
56 | predefinedReason: { prefix: 'tag:' } | |
feb34f6b C |
57 | }) |
58 | ||
59 | params = this.restService.addObjectParams(params, filters) | |
60 | } | |
844db39e | 61 | |
d95d1559 | 62 | return this.authHttp.get<ResultList<Abuse>>(url, { params }) |
db400f44 | 63 | .pipe( |
db400f44 C |
64 | catchError(res => this.restExtractor.handleError(res)) |
65 | ) | |
11ac88de C |
66 | } |
67 | ||
d95d1559 C |
68 | reportVideo (parameters: AbuseCreate) { |
69 | const url = AbuseService.BASE_ABUSE_URL | |
1ebddadd RK |
70 | |
71 | const body = omit(parameters, [ 'id' ]) | |
11ac88de C |
72 | |
73 | return this.authHttp.post(url, body) | |
db400f44 C |
74 | .pipe( |
75 | map(this.restExtractor.extractDataBool), | |
76 | catchError(res => this.restExtractor.handleError(res)) | |
77 | ) | |
11ac88de | 78 | } |
efc9e845 | 79 | |
d95d1559 C |
80 | updateAbuse (abuse: Abuse, abuseUpdate: AbuseUpdate) { |
81 | const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id | |
efc9e845 C |
82 | |
83 | return this.authHttp.put(url, abuseUpdate) | |
84 | .pipe( | |
85 | map(this.restExtractor.extractDataBool), | |
86 | catchError(res => this.restExtractor.handleError(res)) | |
87 | ) | |
88 | } | |
89 | ||
d95d1559 C |
90 | removeAbuse (abuse: Abuse) { |
91 | const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id | |
efc9e845 C |
92 | |
93 | return this.authHttp.delete(url) | |
94 | .pipe( | |
95 | map(this.restExtractor.extractDataBool), | |
96 | catchError(res => this.restExtractor.handleError(res)) | |
97 | ) | |
98 | }} |