]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-moderation/abuse.service.ts
Migrate client to eslint
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / abuse.service.ts
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'
8 import {
9 AbuseCreate,
10 AbuseFilter,
11 AbuseMessage,
12 AbusePredefinedReasonsString,
13 AbuseState,
14 AbuseUpdate,
15 AdminAbuse,
16 ResultList,
17 UserAbuse
18 } from '@shared/models'
19 import { environment } from '../../../environments/environment'
20
21 @Injectable()
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'
25
26 constructor (
27 private authHttp: HttpClient,
28 private restService: RestService,
29 private restExtractor: RestExtractor
30 ) { }
31
32 getAdminAbuses (options: {
33 pagination: RestPagination
34 sort: SortMeta
35 search?: string
36 }): Observable<ResultList<AdminAbuse>> {
37 const { pagination, sort, search } = options
38 const url = AbuseService.BASE_ABUSE_URL
39
40 let params = new HttpParams()
41 params = this.restService.addRestGetParams(params, pagination, sort)
42
43 if (search) {
44 params = this.buildParamsFromSearch(search, params)
45 }
46
47 return this.authHttp.get<ResultList<AdminAbuse>>(url, { params })
48 .pipe(
49 catchError(res => this.restExtractor.handleError(res))
50 )
51 }
52
53 getUserAbuses (options: {
54 pagination: RestPagination
55 sort: SortMeta
56 search?: string
57 }): Observable<ResultList<UserAbuse>> {
58 const { pagination, sort, search } = options
59 const url = AbuseService.BASE_MY_ABUSE_URL
60
61 let params = new HttpParams()
62 params = this.restService.addRestGetParams(params, pagination, sort)
63
64 if (search) {
65 params = this.buildParamsFromSearch(search, params)
66 }
67
68 return this.authHttp.get<ResultList<UserAbuse>>(url, { params })
69 .pipe(
70 catchError(res => this.restExtractor.handleError(res))
71 )
72 }
73
74 reportVideo (parameters: AbuseCreate) {
75 const url = AbuseService.BASE_ABUSE_URL
76
77 const body = omit(parameters, [ 'id' ])
78
79 return this.authHttp.post(url, body)
80 .pipe(
81 map(this.restExtractor.extractDataBool),
82 catchError(res => this.restExtractor.handleError(res))
83 )
84 }
85
86 updateAbuse (abuse: AdminAbuse, abuseUpdate: AbuseUpdate) {
87 const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id
88
89 return this.authHttp.put(url, abuseUpdate)
90 .pipe(
91 map(this.restExtractor.extractDataBool),
92 catchError(res => this.restExtractor.handleError(res))
93 )
94 }
95
96 removeAbuse (abuse: AdminAbuse) {
97 const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id
98
99 return this.authHttp.delete(url)
100 .pipe(
101 map(this.restExtractor.extractDataBool),
102 catchError(res => this.restExtractor.handleError(res))
103 )
104 }
105
106 addAbuseMessage (abuse: UserAbuse, message: string) {
107 const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id + '/messages'
108
109 return this.authHttp.post(url, { message })
110 .pipe(
111 map(this.restExtractor.extractDataBool),
112 catchError(res => this.restExtractor.handleError(res))
113 )
114 }
115
116 listAbuseMessages (abuse: UserAbuse) {
117 const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id + '/messages'
118
119 return this.authHttp.get<ResultList<AbuseMessage>>(url)
120 .pipe(
121 catchError(res => this.restExtractor.handleError(res))
122 )
123 }
124
125 deleteAbuseMessage (abuse: UserAbuse, abuseMessage: AbuseMessage) {
126 const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id + '/messages/' + abuseMessage.id
127
128 return this.authHttp.delete(url)
129 .pipe(
130 map(this.restExtractor.extractDataBool),
131 catchError(res => this.restExtractor.handleError(res))
132 )
133 }
134
135 getPrefefinedReasons (type: AbuseFilter) {
136 let reasons: { id: AbusePredefinedReasonsString, label: string, description?: string, help?: string }[] = [
137 {
138 id: 'violentOrRepulsive',
139 label: $localize`Violent or repulsive`,
140 help: $localize`Contains offensive, violent, or coarse language or iconography.`
141 },
142 {
143 id: 'hatefulOrAbusive',
144 label: $localize`Hateful or abusive`,
145 help: $localize`Contains abusive, racist or sexist language or iconography.`
146 },
147 {
148 id: 'spamOrMisleading',
149 label: $localize`Spam, ad or false news`,
150 // eslint-disable-next-line max-len
151 help: $localize`Contains marketing, spam, purposefully deceitful news, or otherwise misleading thumbnail/text/tags. Please provide reputable sources to report hoaxes.`
152 },
153 {
154 id: 'privacy',
155 label: $localize`Privacy breach or doxxing`,
156 // eslint-disable-next-line max-len
157 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).`
158 },
159 {
160 id: 'rights',
161 label: $localize`Copyright`,
162 help: $localize`Infringes your copyright wrt. the regional laws with which the server must comply.`
163 },
164 {
165 id: 'serverRules',
166 label: $localize`Breaks server rules`,
167 // eslint-disable-next-line max-len
168 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 }
170 ]
171
172 if (type === 'video') {
173 reasons = reasons.concat([
174 {
175 id: 'thumbnails',
176 label: $localize`Thumbnails`,
177 help: $localize`The above can only be seen in thumbnails.`
178 },
179 {
180 id: 'captions',
181 label: $localize`Captions`,
182 help: $localize`The above can only be seen in captions (please describe which).`
183 }
184 ])
185 }
186
187 return reasons
188 }
189
190 private buildParamsFromSearch (search: string, params: HttpParams) {
191 const filters = this.restService.parseQueryStringFilter(search, {
192 id: { prefix: '#' },
193 state: {
194 prefix: 'state:',
195 handler: v => {
196 if (v === 'accepted') return AbuseState.ACCEPTED
197 if (v === 'pending') return AbuseState.PENDING
198 if (v === 'rejected') return AbuseState.REJECTED
199
200 return undefined
201 }
202 },
203 videoIs: {
204 prefix: 'videoIs:',
205 handler: v => {
206 if (v === 'deleted') return v
207 if (v === 'blacklisted') return v
208
209 return undefined
210 }
211 },
212 searchReporter: { prefix: 'reporter:' },
213 searchReportee: { prefix: 'reportee:' },
214 predefinedReason: { prefix: 'tag:' }
215 })
216
217 return this.restService.addObjectParams(params, filters)
218 }
219 }