1 import { omit } from 'lodash-es'
2 import { SortMeta } from 'primeng/api'
3 import { Observable } from 'rxjs'
4 import { catchError, map } from 'rxjs/operators'
5 import { HttpClient, HttpParams } from '@angular/common/http'
6 import { Injectable } from '@angular/core'
7 import { RestExtractor, RestPagination, RestService } from '@app/core'
12 AbusePredefinedReasonsString,
18 } from '@shared/models'
19 import { environment } from '../../../environments/environment'
22 export class AbuseService {
23 private static BASE_ABUSE_URL = environment.apiUrl + '/api/v1/abuses'
24 private static BASE_MY_ABUSE_URL = environment.apiUrl + '/api/v1/users/me/abuses'
27 private authHttp: HttpClient,
28 private restService: RestService,
29 private restExtractor: RestExtractor
32 getAdminAbuses (options: {
33 pagination: RestPagination,
36 }): Observable<ResultList<AdminAbuse>> {
37 const { pagination, sort, search } = options
38 const url = AbuseService.BASE_ABUSE_URL
40 let params = new HttpParams()
41 params = this.restService.addRestGetParams(params, pagination, sort)
44 params = this.buildParamsFromSearch(search, params)
47 return this.authHttp.get<ResultList<AdminAbuse>>(url, { params })
49 catchError(res => this.restExtractor.handleError(res))
53 getUserAbuses (options: {
54 pagination: RestPagination,
57 }): Observable<ResultList<UserAbuse>> {
58 const { pagination, sort, search } = options
59 const url = AbuseService.BASE_MY_ABUSE_URL
61 let params = new HttpParams()
62 params = this.restService.addRestGetParams(params, pagination, sort)
65 params = this.buildParamsFromSearch(search, params)
68 return this.authHttp.get<ResultList<UserAbuse>>(url, { params })
70 catchError(res => this.restExtractor.handleError(res))
74 reportVideo (parameters: AbuseCreate) {
75 const url = AbuseService.BASE_ABUSE_URL
77 const body = omit(parameters, ['id'])
79 return this.authHttp.post(url, body)
81 map(this.restExtractor.extractDataBool),
82 catchError(res => this.restExtractor.handleError(res))
86 updateAbuse (abuse: AdminAbuse, abuseUpdate: AbuseUpdate) {
87 const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id
89 return this.authHttp.put(url, abuseUpdate)
91 map(this.restExtractor.extractDataBool),
92 catchError(res => this.restExtractor.handleError(res))
96 removeAbuse (abuse: AdminAbuse) {
97 const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id
99 return this.authHttp.delete(url)
101 map(this.restExtractor.extractDataBool),
102 catchError(res => this.restExtractor.handleError(res))
106 addAbuseMessage (abuse: UserAbuse, message: string) {
107 const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id + '/messages'
109 return this.authHttp.post(url, { message })
111 map(this.restExtractor.extractDataBool),
112 catchError(res => this.restExtractor.handleError(res))
116 listAbuseMessages (abuse: UserAbuse) {
117 const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id + '/messages'
119 return this.authHttp.get<ResultList<AbuseMessage>>(url)
121 catchError(res => this.restExtractor.handleError(res))
125 deleteAbuseMessage (abuse: UserAbuse, abuseMessage: AbuseMessage) {
126 const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id + '/messages/' + abuseMessage.id
128 return this.authHttp.delete(url)
130 map(this.restExtractor.extractDataBool),
131 catchError(res => this.restExtractor.handleError(res))
135 getPrefefinedReasons (type: AbuseFilter) {
136 let reasons: { id: AbusePredefinedReasonsString, label: string, description?: string, help?: string }[] = [
138 id: 'violentOrRepulsive',
139 label: $localize`Violent or repulsive`,
140 help: $localize`Contains offensive, violent, or coarse language or iconography.`
143 id: 'hatefulOrAbusive',
144 label: $localize`Hateful or abusive`,
145 help: $localize`Contains abusive, racist or sexist language or iconography.`
148 id: 'spamOrMisleading',
149 label: $localize`Spam, ad or false news`,
150 help: $localize`Contains marketing, spam, purposefully deceitful news, or otherwise misleading thumbnail/text/tags. Please provide reputable sources to report hoaxes.`
154 label: $localize`Privacy breach or doxxing`,
155 help: $localize`Contains personal information that could be used to track, identify, contact or impersonate someone (e.g. name, address, phone number, email, or credit card details).`
159 label: $localize`Copyright`,
160 help: $localize`Infringes your copyright wrt. the regional laws with which the server must comply.`
164 label: $localize`Breaks server rules`,
165 description: $localize`Anything not included in the above that breaks the terms of service, code of conduct, or general rules in place on the server.`
169 if (type === 'video') {
170 reasons = reasons.concat([
173 label: $localize`Thumbnails`,
174 help: $localize`The above can only be seen in thumbnails.`
178 label: $localize`Captions`,
179 help: $localize`The above can only be seen in captions (please describe which).`
187 private buildParamsFromSearch (search: string, params: HttpParams) {
188 const filters = this.restService.parseQueryStringFilter(search, {
193 if (v === 'accepted') return AbuseState.ACCEPTED
194 if (v === 'pending') return AbuseState.PENDING
195 if (v === 'rejected') return AbuseState.REJECTED
203 if (v === 'deleted') return v
204 if (v === 'blacklisted') return v
209 searchReporter: { prefix: 'reporter:' },
210 searchReportee: { prefix: 'reportee:' },
211 predefinedReason: { prefix: 'tag:' }
214 return this.restService.addObjectParams(params, filters)