]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-moderation/abuse.service.ts
Update build steps for localization
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / abuse.service.ts
index 95ac169557abfdc6586a3cb9e29364544b44a84d..06b236d1e6a542c3de1e6d8957ec1eebdf41efbc 100644 (file)
@@ -5,13 +5,24 @@ import { catchError, map } from 'rxjs/operators'
 import { HttpClient, HttpParams } from '@angular/common/http'
 import { Injectable } from '@angular/core'
 import { RestExtractor, RestPagination, RestService } from '@app/core'
-import { Abuse, AbuseCreate, AbuseFilter, AbusePredefinedReasonsString, AbuseState, AbuseUpdate, ResultList } from '@shared/models'
-import { environment } from '../../../environments/environment'
 import { I18n } from '@ngx-translate/i18n-polyfill'
+import {
+  AbuseCreate,
+  AbuseFilter,
+  AbuseMessage,
+  AbusePredefinedReasonsString,
+  AbuseState,
+  AbuseUpdate,
+  AdminAbuse,
+  ResultList,
+  UserAbuse
+} from '@shared/models'
+import { environment } from '../../../environments/environment'
 
 @Injectable()
 export class AbuseService {
   private static BASE_ABUSE_URL = environment.apiUrl + '/api/v1/abuses'
+  private static BASE_MY_ABUSE_URL = environment.apiUrl + '/api/v1/users/me/abuses'
 
   constructor (
     private i18n: I18n,
@@ -20,11 +31,11 @@ export class AbuseService {
     private restExtractor: RestExtractor
   ) { }
 
-  getAbuses (options: {
+  getAdminAbuses (options: {
     pagination: RestPagination,
     sort: SortMeta,
     search?: string
-  }): Observable<ResultList<Abuse>> {
+  }): Observable<ResultList<AdminAbuse>> {
     const { pagination, sort, search } = options
     const url = AbuseService.BASE_ABUSE_URL
 
@@ -32,36 +43,31 @@ export class AbuseService {
     params = this.restService.addRestGetParams(params, pagination, sort)
 
     if (search) {
-      const filters = this.restService.parseQueryStringFilter(search, {
-        id: { prefix: '#' },
-        state: {
-          prefix: 'state:',
-          handler: v => {
-            if (v === 'accepted') return AbuseState.ACCEPTED
-            if (v === 'pending') return AbuseState.PENDING
-            if (v === 'rejected') return AbuseState.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:' },
-        predefinedReason: { prefix: 'tag:' }
-      })
+      params = this.buildParamsFromSearch(search, params)
+    }
+
+    return this.authHttp.get<ResultList<AdminAbuse>>(url, { params })
+      .pipe(
+        catchError(res => this.restExtractor.handleError(res))
+      )
+  }
+
+  getUserAbuses (options: {
+    pagination: RestPagination,
+    sort: SortMeta,
+    search?: string
+  }): Observable<ResultList<UserAbuse>> {
+    const { pagination, sort, search } = options
+    const url = AbuseService.BASE_MY_ABUSE_URL
+
+    let params = new HttpParams()
+    params = this.restService.addRestGetParams(params, pagination, sort)
 
-      params = this.restService.addObjectParams(params, filters)
+    if (search) {
+      params = this.buildParamsFromSearch(search, params)
     }
 
-    return this.authHttp.get<ResultList<Abuse>>(url, { params })
+    return this.authHttp.get<ResultList<UserAbuse>>(url, { params })
       .pipe(
         catchError(res => this.restExtractor.handleError(res))
       )
@@ -79,7 +85,7 @@ export class AbuseService {
       )
   }
 
-  updateAbuse (abuse: Abuse, abuseUpdate: AbuseUpdate) {
+  updateAbuse (abuse: AdminAbuse, abuseUpdate: AbuseUpdate) {
     const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id
 
     return this.authHttp.put(url, abuseUpdate)
@@ -89,7 +95,7 @@ export class AbuseService {
       )
   }
 
-  removeAbuse (abuse: Abuse) {
+  removeAbuse (abuse: AdminAbuse) {
     const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id
 
     return this.authHttp.delete(url)
@@ -99,6 +105,35 @@ export class AbuseService {
       )
   }
 
+  addAbuseMessage (abuse: UserAbuse, message: string) {
+    const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id + '/messages'
+
+    return this.authHttp.post(url, { message })
+    .pipe(
+      map(this.restExtractor.extractDataBool),
+      catchError(res => this.restExtractor.handleError(res))
+    )
+  }
+
+  listAbuseMessages (abuse: UserAbuse) {
+    const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id + '/messages'
+
+    return this.authHttp.get<ResultList<AbuseMessage>>(url)
+    .pipe(
+      catchError(res => this.restExtractor.handleError(res))
+    )
+  }
+
+  deleteAbuseMessage (abuse: UserAbuse, abuseMessage: AbuseMessage) {
+    const url = AbuseService.BASE_ABUSE_URL + '/' + abuse.id + '/messages/' + abuseMessage.id
+
+    return this.authHttp.delete(url)
+    .pipe(
+      map(this.restExtractor.extractDataBool),
+      catchError(res => this.restExtractor.handleError(res))
+    )
+  }
+
   getPrefefinedReasons (type: AbuseFilter) {
     let reasons: { id: AbusePredefinedReasonsString, label: string, description?: string, help?: string }[] = [
       {
@@ -123,8 +158,8 @@ export class AbuseService {
       },
       {
         id: 'rights',
-        label: this.i18n('Intellectual property violation'),
-        help: this.i18n('Infringes my intellectual property or copyright, wrt. the regional rules with which the server must comply.')
+        label: this.i18n('Copyright'),
+        help: this.i18n('Infringes your copyright wrt. the regional laws with which the server must comply.')
       },
       {
         id: 'serverRules',
@@ -151,4 +186,33 @@ export class AbuseService {
     return reasons
   }
 
+  private buildParamsFromSearch (search: string, params: HttpParams) {
+    const filters = this.restService.parseQueryStringFilter(search, {
+      id: { prefix: '#' },
+      state: {
+        prefix: 'state:',
+        handler: v => {
+          if (v === 'accepted') return AbuseState.ACCEPTED
+          if (v === 'pending') return AbuseState.PENDING
+          if (v === 'rejected') return AbuseState.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:' },
+      predefinedReason: { prefix: 'tag:' }
+    })
+
+    return this.restService.addObjectParams(params, filters)
+  }
 }