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