aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-09-14 11:57:49 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-09-14 11:57:49 +0200
commitd592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb (patch)
tree549b14b842de296efed846a11b3681efe08cfa9e /client/src/app/shared
parent91f6f169b1110eeae6ebf5c387f4204b0d07703c (diff)
downloadPeerTube-d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb.tar.gz
PeerTube-d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb.tar.zst
PeerTube-d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb.zip
Move to HttpClient and PrimeNG data table
Diffstat (limited to 'client/src/app/shared')
-rw-r--r--client/src/app/shared/auth/auth-http.service.ts93
-rw-r--r--client/src/app/shared/auth/auth-interceptor.service.ts62
-rw-r--r--client/src/app/shared/auth/index.ts2
-rw-r--r--client/src/app/shared/rest/index.ts1
-rw-r--r--client/src/app/shared/rest/rest-data-source.ts98
-rw-r--r--client/src/app/shared/rest/rest-extractor.service.ts66
-rw-r--r--client/src/app/shared/rest/rest-pagination.ts5
-rw-r--r--client/src/app/shared/rest/rest-table.ts27
-rw-r--r--client/src/app/shared/rest/rest.service.ts31
-rw-r--r--client/src/app/shared/shared.module.ts19
-rw-r--r--client/src/app/shared/users/user.service.ts16
-rw-r--r--client/src/app/shared/utils.ts10
-rw-r--r--client/src/app/shared/video-abuse/video-abuse.service.ts41
13 files changed, 223 insertions, 248 deletions
diff --git a/client/src/app/shared/auth/auth-http.service.ts b/client/src/app/shared/auth/auth-http.service.ts
deleted file mode 100644
index 0fbaab0a8..000000000
--- a/client/src/app/shared/auth/auth-http.service.ts
+++ /dev/null
@@ -1,93 +0,0 @@
1import { Injectable } from '@angular/core'
2import {
3 ConnectionBackend,
4 Headers,
5 Http,
6 Request,
7 RequestMethod,
8 RequestOptions,
9 RequestOptionsArgs,
10 Response,
11 XHRBackend
12} from '@angular/http'
13import { Observable } from 'rxjs/Observable'
14
15import { AuthService } from '../../core'
16
17@Injectable()
18export class AuthHttp extends Http {
19 constructor (backend: ConnectionBackend, defaultOptions: RequestOptions, private authService: AuthService) {
20 super(backend, defaultOptions)
21 }
22
23 request (url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
24 if (!options) options = {}
25
26 options.headers = new Headers()
27 this.setAuthorizationHeader(options.headers)
28
29 return super.request(url, options)
30 .catch((err) => {
31 if (err.status === 401) {
32 return this.handleTokenExpired(url, options)
33 }
34
35 return Observable.throw(err)
36 })
37 }
38
39 delete (url: string, options?: RequestOptionsArgs): Observable<Response> {
40 if (!options) options = {}
41 options.method = RequestMethod.Delete
42
43 return this.request(url, options)
44 }
45
46 get (url: string, options?: RequestOptionsArgs): Observable<Response> {
47 if (!options) options = {}
48 options.method = RequestMethod.Get
49
50 return this.request(url, options)
51 }
52
53 post (url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
54 if (!options) options = {}
55 options.method = RequestMethod.Post
56 options.body = body
57
58 return this.request(url, options)
59 }
60
61 put (url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
62 if (!options) options = {}
63 options.method = RequestMethod.Put
64 options.body = body
65
66 return this.request(url, options)
67 }
68
69 private handleTokenExpired (url: string | Request, options: RequestOptionsArgs) {
70 return this.authService.refreshAccessToken()
71 .flatMap(() => {
72 this.setAuthorizationHeader(options.headers)
73
74 return super.request(url, options)
75 })
76 }
77
78 private setAuthorizationHeader (headers: Headers) {
79 headers.set('Authorization', this.authService.getRequestHeaderValue())
80 }
81}
82
83export function useFactory (backend: XHRBackend, defaultOptions: RequestOptions, authService: AuthService) {
84 return new AuthHttp(backend, defaultOptions, authService)
85}
86
87export const AUTH_HTTP_PROVIDERS = [
88 {
89 provide: AuthHttp,
90 useFactory,
91 deps: [ XHRBackend, RequestOptions, AuthService ]
92 }
93]
diff --git a/client/src/app/shared/auth/auth-interceptor.service.ts b/client/src/app/shared/auth/auth-interceptor.service.ts
new file mode 100644
index 000000000..1e890d8f3
--- /dev/null
+++ b/client/src/app/shared/auth/auth-interceptor.service.ts
@@ -0,0 +1,62 @@
1import { Injectable, Injector } from '@angular/core'
2import {
3 HttpInterceptor,
4 HttpRequest,
5 HttpEvent,
6 HttpHandler, HTTP_INTERCEPTORS
7} from '@angular/common/http'
8import { Observable } from 'rxjs/Observable'
9
10import { AuthService } from '../../core'
11import 'rxjs/add/operator/switchMap'
12
13@Injectable()
14export class AuthInterceptor implements HttpInterceptor {
15 private authService: AuthService
16
17 // https://github.com/angular/angular/issues/18224#issuecomment-316957213
18 constructor (private injector: Injector) {}
19
20 intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
21 if (this.authService === undefined) {
22 this.authService = this.injector.get(AuthService)
23 }
24
25 const authReq = this.cloneRequestWithAuth(req)
26
27 // Pass on the cloned request instead of the original request
28 // Catch 401 errors (refresh token expired)
29 return next.handle(authReq)
30 .catch(err => {
31 if (err.status === 401) {
32 return this.handleTokenExpired(req, next)
33 }
34
35 return Observable.throw(err)
36 })
37 }
38
39 private handleTokenExpired (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
40 return this.authService.refreshAccessToken()
41 .switchMap(() => {
42 const authReq = this.cloneRequestWithAuth(req)
43
44 return next.handle(authReq)
45 })
46 }
47
48 private cloneRequestWithAuth (req: HttpRequest<any>) {
49 const authHeaderValue = this.authService.getRequestHeaderValue()
50
51 if (authHeaderValue === null) return req
52
53 // Clone the request to add the new header
54 return req.clone({ headers: req.headers.set('Authorization', authHeaderValue) })
55 }
56}
57
58export const AUTH_INTERCEPTOR_PROVIDER = {
59 provide: HTTP_INTERCEPTORS,
60 useClass: AuthInterceptor,
61 multi: true
62}
diff --git a/client/src/app/shared/auth/index.ts b/client/src/app/shared/auth/index.ts
index 0f2bfb0d6..84a07196f 100644
--- a/client/src/app/shared/auth/index.ts
+++ b/client/src/app/shared/auth/index.ts
@@ -1 +1 @@
export * from './auth-http.service' export * from './auth-interceptor.service'
diff --git a/client/src/app/shared/rest/index.ts b/client/src/app/shared/rest/index.ts
index e0be155cf..3f1996130 100644
--- a/client/src/app/shared/rest/index.ts
+++ b/client/src/app/shared/rest/index.ts
@@ -2,3 +2,4 @@ export * from './rest-data-source'
2export * from './rest-extractor.service' 2export * from './rest-extractor.service'
3export * from './rest-pagination' 3export * from './rest-pagination'
4export * from './rest.service' 4export * from './rest.service'
5export * from './rest-table'
diff --git a/client/src/app/shared/rest/rest-data-source.ts b/client/src/app/shared/rest/rest-data-source.ts
index 5c205d280..57a2efb57 100644
--- a/client/src/app/shared/rest/rest-data-source.ts
+++ b/client/src/app/shared/rest/rest-data-source.ts
@@ -1,68 +1,32 @@
1import { Http, RequestOptionsArgs, URLSearchParams, Response } from '@angular/http' 1export class RestDataSource {
2 2 // protected addSortRequestOptions (requestOptions: RequestOptionsArgs) {
3import { ServerDataSource } from 'ng2-smart-table' 3 // const searchParams = requestOptions.params as URLSearchParams
4 4 //
5export class RestDataSource extends ServerDataSource { 5 // if (this.sortConf) {
6 private updateResponse: (input: any[]) => any[] 6 // this.sortConf.forEach((fieldConf) => {
7 7 // const sortPrefix = fieldConf.direction === 'desc' ? '-' : ''
8 constructor (http: Http, endpoint: string, updateResponse?: (input: any[]) => any[]) { 8 //
9 const options = { 9 // searchParams.set(this.conf.sortFieldKey, sortPrefix + fieldConf.field)
10 endPoint: endpoint, 10 // })
11 sortFieldKey: 'sort', 11 // }
12 dataKey: 'data' 12 //
13 } 13 // return requestOptions
14 super(http, options) 14 // }
15 15 //
16 if (updateResponse) { 16 // protected addPagerRequestOptions (requestOptions: RequestOptionsArgs) {
17 this.updateResponse = updateResponse 17 // const searchParams = requestOptions.params as URLSearchParams
18 } 18 //
19 } 19 // if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) {
20 20 // const perPage = this.pagingConf['perPage']
21 protected extractDataFromResponse (res: Response) { 21 // const page = this.pagingConf['page']
22 const json = res.json() 22 //
23 if (!json) return [] 23 // const start = (page - 1) * perPage
24 let data = json.data 24 // const count = perPage
25 25 //
26 if (this.updateResponse !== undefined) { 26 // searchParams.set('start', start.toString())
27 data = this.updateResponse(data) 27 // searchParams.set('count', count.toString())
28 } 28 // }
29 29 //
30 return data 30 // return requestOptions
31 } 31 // }
32
33 protected extractTotalFromResponse (res: Response) {
34 const rawData = res.json()
35 return rawData ? parseInt(rawData.total, 10) : 0
36 }
37
38 protected addSortRequestOptions (requestOptions: RequestOptionsArgs) {
39 const searchParams = requestOptions.params as URLSearchParams
40
41 if (this.sortConf) {
42 this.sortConf.forEach((fieldConf) => {
43 const sortPrefix = fieldConf.direction === 'desc' ? '-' : ''
44
45 searchParams.set(this.conf.sortFieldKey, sortPrefix + fieldConf.field)
46 })
47 }
48
49 return requestOptions
50 }
51
52 protected addPagerRequestOptions (requestOptions: RequestOptionsArgs) {
53 const searchParams = requestOptions.params as URLSearchParams
54
55 if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) {
56 const perPage = this.pagingConf['perPage']
57 const page = this.pagingConf['page']
58
59 const start = (page - 1) * perPage
60 const count = perPage
61
62 searchParams.set('start', start.toString())
63 searchParams.set('count', count.toString())
64 }
65
66 return requestOptions
67 }
68} 32}
diff --git a/client/src/app/shared/rest/rest-extractor.service.ts b/client/src/app/shared/rest/rest-extractor.service.ts
index f6a818ec8..32dad5c73 100644
--- a/client/src/app/shared/rest/rest-extractor.service.ts
+++ b/client/src/app/shared/rest/rest-extractor.service.ts
@@ -1,52 +1,58 @@
1import { Injectable } from '@angular/core' 1import { Injectable } from '@angular/core'
2import { Response } from '@angular/http'
3import { Observable } from 'rxjs/Observable' 2import { Observable } from 'rxjs/Observable'
3import { HttpErrorResponse } from '@angular/common/http'
4 4
5export interface ResultList { 5import { Utils } from '../utils'
6 data: any[] 6import { ResultList } from '../../../../../shared'
7 total: number
8}
9 7
10@Injectable() 8@Injectable()
11export class RestExtractor { 9export class RestExtractor {
12 10
13 extractDataBool (res: Response) { 11 extractDataBool () {
14 return true 12 return true
15 } 13 }
16 14
17 extractDataList (res: Response) { 15 applyToResultListData <T> (result: ResultList<T>, fun: Function, additionalArgs?: any[]): ResultList<T> {
18 const body = res.json() 16 const data: T[] = result.data
17 const newData: T[] = []
19 18
20 const ret: ResultList = { 19 data.forEach(d => newData.push(fun.call(this, d, additionalArgs)))
21 data: body.data,
22 total: body.total
23 }
24 20
25 return ret 21 return {
22 total: result.total,
23 data: newData
24 }
26 } 25 }
27 26
28 extractDataGet (res: Response) { 27 convertResultListDateToHuman <T> (result: ResultList<T>, fieldsToConvert: string[] = [ 'createdAt' ]): ResultList<T> {
29 return res.json() 28 return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert ])
30 } 29 }
31 30
32 handleError (res: Response) { 31 convertDateToHuman (target: object, fieldsToConvert: string[]) {
33 let text = 'Server error: ' 32 const source = {}
34 text += res.text() 33 fieldsToConvert.forEach(field => {
35 let json = '' 34 source[field] = Utils.dateToHuman(target[field])
35 })
36 36
37 try { 37 return Object.assign(target, source)
38 json = res.json() 38 }
39 } catch (err) {
40 console.error('Cannot get JSON from response.')
41 }
42 39
43 const error = { 40 handleError (err: HttpErrorResponse) {
44 json, 41 let errorMessage
45 text 42
43 if (err.error instanceof Error) {
44 // A client-side or network error occurred. Handle it accordingly.
45 errorMessage = err.error.message
46 console.error('An error occurred:', errorMessage)
47 } else if (err.status !== undefined) {
48 // The backend returned an unsuccessful response code.
49 // The response body may contain clues as to what went wrong,
50 errorMessage = err.error
51 console.error(`Backend returned code ${err.status}, body was: ${errorMessage}`)
52 } else {
53 errorMessage = err
46 } 54 }
47 55
48 console.error(error) 56 return Observable.throw(errorMessage)
49
50 return Observable.throw(error)
51 } 57 }
52} 58}
diff --git a/client/src/app/shared/rest/rest-pagination.ts b/client/src/app/shared/rest/rest-pagination.ts
index 766e7a9e5..0faa59303 100644
--- a/client/src/app/shared/rest/rest-pagination.ts
+++ b/client/src/app/shared/rest/rest-pagination.ts
@@ -1,5 +1,4 @@
1export interface RestPagination { 1export interface RestPagination {
2 currentPage: number 2 start: number
3 itemsPerPage: number 3 count: number
4 totalItems: number
5} 4}
diff --git a/client/src/app/shared/rest/rest-table.ts b/client/src/app/shared/rest/rest-table.ts
new file mode 100644
index 000000000..db2cb5e14
--- /dev/null
+++ b/client/src/app/shared/rest/rest-table.ts
@@ -0,0 +1,27 @@
1import { LazyLoadEvent, SortMeta } from 'primeng/primeng'
2
3import { RestPagination } from './rest-pagination'
4
5export abstract class RestTable {
6 abstract totalRecords: number
7 abstract rowsPerPage: number
8 abstract sort: SortMeta
9 abstract pagination: RestPagination
10
11 protected abstract loadData (): void
12
13 loadLazy (event: LazyLoadEvent) {
14 this.sort = {
15 order: event.sortOrder,
16 field: event.sortField
17 }
18
19 this.pagination = {
20 start: event.first,
21 count: this.rowsPerPage
22 }
23
24 this.loadData()
25 }
26
27}
diff --git a/client/src/app/shared/rest/rest.service.ts b/client/src/app/shared/rest/rest.service.ts
index 43dc20b34..f7838ba06 100644
--- a/client/src/app/shared/rest/rest.service.ts
+++ b/client/src/app/shared/rest/rest.service.ts
@@ -1,27 +1,34 @@
1import { Injectable } from '@angular/core' 1import { Injectable } from '@angular/core'
2import { URLSearchParams } from '@angular/http' 2import { HttpParams } from '@angular/common/http'
3import { SortMeta } from 'primeng/primeng'
3 4
4import { RestPagination } from './rest-pagination' 5import { RestPagination } from './rest-pagination'
5 6
6@Injectable() 7@Injectable()
7export class RestService { 8export class RestService {
8 9
9 buildRestGetParams (pagination?: RestPagination, sort?: string) { 10 addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) {
10 const params = new URLSearchParams() 11 let newParams = params
11 12
12 if (pagination) { 13 if (pagination !== undefined) {
13 const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage 14 newParams = newParams.set('start', pagination.start.toString())
14 const count: number = pagination.itemsPerPage 15 .set('count', pagination.count.toString())
15
16 params.set('start', start.toString())
17 params.set('count', count.toString())
18 } 16 }
19 17
20 if (sort) { 18 if (sort !== undefined) {
21 params.set('sort', sort) 19 let sortString = ''
20
21 if (typeof sort === 'string') {
22 sortString = sort
23 } else {
24 const sortPrefix = sort.order === 1 ? '' : '-'
25 sortString = sortPrefix + sort.field
26 }
27
28 newParams = newParams.set('sort', sortString)
22 } 29 }
23 30
24 return params 31 return newParams
25 } 32 }
26 33
27} 34}
diff --git a/client/src/app/shared/shared.module.ts b/client/src/app/shared/shared.module.ts
index 99b51aa4e..118ce822d 100644
--- a/client/src/app/shared/shared.module.ts
+++ b/client/src/app/shared/shared.module.ts
@@ -1,6 +1,6 @@
1import { NgModule } from '@angular/core' 1import { NgModule } from '@angular/core'
2import { HttpClientModule } from '@angular/common/http'
2import { CommonModule } from '@angular/common' 3import { CommonModule } from '@angular/common'
3import { HttpModule } from '@angular/http'
4import { FormsModule, ReactiveFormsModule } from '@angular/forms' 4import { FormsModule, ReactiveFormsModule } from '@angular/forms'
5import { RouterModule } from '@angular/router' 5import { RouterModule } from '@angular/router'
6 6
@@ -11,9 +11,9 @@ import { ProgressbarModule } from 'ngx-bootstrap/progressbar'
11import { PaginationModule } from 'ngx-bootstrap/pagination' 11import { PaginationModule } from 'ngx-bootstrap/pagination'
12import { ModalModule } from 'ngx-bootstrap/modal' 12import { ModalModule } from 'ngx-bootstrap/modal'
13import { FileUploadModule } from 'ng2-file-upload/ng2-file-upload' 13import { FileUploadModule } from 'ng2-file-upload/ng2-file-upload'
14import { Ng2SmartTableModule } from 'ng2-smart-table' 14import { DataTableModule, SharedModule as PrimeSharedModule } from 'primeng/primeng'
15 15
16import { AUTH_HTTP_PROVIDERS } from './auth' 16import { AUTH_INTERCEPTOR_PROVIDER } from './auth'
17import { RestExtractor, RestService } from './rest' 17import { RestExtractor, RestService } from './rest'
18import { SearchComponent, SearchService } from './search' 18import { SearchComponent, SearchService } from './search'
19import { UserService } from './users' 19import { UserService } from './users'
@@ -24,8 +24,8 @@ import { VideoAbuseService } from './video-abuse'
24 CommonModule, 24 CommonModule,
25 FormsModule, 25 FormsModule,
26 ReactiveFormsModule, 26 ReactiveFormsModule,
27 HttpModule,
28 RouterModule, 27 RouterModule,
28 HttpClientModule,
29 29
30 BsDropdownModule.forRoot(), 30 BsDropdownModule.forRoot(),
31 ModalModule.forRoot(), 31 ModalModule.forRoot(),
@@ -33,7 +33,9 @@ import { VideoAbuseService } from './video-abuse'
33 ProgressbarModule.forRoot(), 33 ProgressbarModule.forRoot(),
34 34
35 FileUploadModule, 35 FileUploadModule,
36 Ng2SmartTableModule 36
37 DataTableModule,
38 PrimeSharedModule
37 ], 39 ],
38 40
39 declarations: [ 41 declarations: [
@@ -46,15 +48,16 @@ import { VideoAbuseService } from './video-abuse'
46 CommonModule, 48 CommonModule,
47 FormsModule, 49 FormsModule,
48 ReactiveFormsModule, 50 ReactiveFormsModule,
49 HttpModule,
50 RouterModule, 51 RouterModule,
52 HttpClientModule,
51 53
52 BsDropdownModule, 54 BsDropdownModule,
53 FileUploadModule, 55 FileUploadModule,
54 ModalModule, 56 ModalModule,
55 PaginationModule, 57 PaginationModule,
56 ProgressbarModule, 58 ProgressbarModule,
57 Ng2SmartTableModule, 59 DataTableModule,
60 PrimeSharedModule,
58 BytesPipe, 61 BytesPipe,
59 KeysPipe, 62 KeysPipe,
60 63
@@ -62,7 +65,7 @@ import { VideoAbuseService } from './video-abuse'
62 ], 65 ],
63 66
64 providers: [ 67 providers: [
65 AUTH_HTTP_PROVIDERS, 68 AUTH_INTERCEPTOR_PROVIDER,
66 RestExtractor, 69 RestExtractor,
67 RestService, 70 RestService,
68 SearchService, 71 SearchService,
diff --git a/client/src/app/shared/users/user.service.ts b/client/src/app/shared/users/user.service.ts
index 35180be4d..5c089d221 100644
--- a/client/src/app/shared/users/user.service.ts
+++ b/client/src/app/shared/users/user.service.ts
@@ -1,10 +1,8 @@
1import { Injectable } from '@angular/core' 1import { Injectable } from '@angular/core'
2import { Http } from '@angular/http' 2import { HttpClient } from '@angular/common/http'
3import 'rxjs/add/operator/catch' 3import 'rxjs/add/operator/catch'
4import 'rxjs/add/operator/map' 4import 'rxjs/add/operator/map'
5 5
6import { AuthService } from '../../core'
7import { AuthHttp } from '../auth'
8import { RestExtractor } from '../rest' 6import { RestExtractor } from '../rest'
9import { UserCreate, UserUpdateMe } from '../../../../../shared' 7import { UserCreate, UserUpdateMe } from '../../../../../shared'
10 8
@@ -13,9 +11,7 @@ export class UserService {
13 static BASE_USERS_URL = API_URL + '/api/v1/users/' 11 static BASE_USERS_URL = API_URL + '/api/v1/users/'
14 12
15 constructor ( 13 constructor (
16 private http: Http, 14 private authHttp: HttpClient,
17 private authHttp: AuthHttp,
18 private authService: AuthService,
19 private restExtractor: RestExtractor 15 private restExtractor: RestExtractor
20 ) {} 16 ) {}
21 17
@@ -34,7 +30,7 @@ export class UserService {
34 30
35 return this.authHttp.put(url, body) 31 return this.authHttp.put(url, body)
36 .map(this.restExtractor.extractDataBool) 32 .map(this.restExtractor.extractDataBool)
37 .catch((res) => this.restExtractor.handleError(res)) 33 .catch(res => this.restExtractor.handleError(res))
38 } 34 }
39 35
40 updateMyDetails (details: UserUpdateMe) { 36 updateMyDetails (details: UserUpdateMe) {
@@ -42,12 +38,12 @@ export class UserService {
42 38
43 return this.authHttp.put(url, details) 39 return this.authHttp.put(url, details)
44 .map(this.restExtractor.extractDataBool) 40 .map(this.restExtractor.extractDataBool)
45 .catch((res) => this.restExtractor.handleError(res)) 41 .catch(res => this.restExtractor.handleError(res))
46 } 42 }
47 43
48 signup (userCreate: UserCreate) { 44 signup (userCreate: UserCreate) {
49 return this.http.post(UserService.BASE_USERS_URL + 'register', userCreate) 45 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
50 .map(this.restExtractor.extractDataBool) 46 .map(this.restExtractor.extractDataBool)
51 .catch(this.restExtractor.handleError) 47 .catch(res => this.restExtractor.handleError(res))
52 } 48 }
53} 49}
diff --git a/client/src/app/shared/utils.ts b/client/src/app/shared/utils.ts
index c3189a570..7c8ae2e3e 100644
--- a/client/src/app/shared/utils.ts
+++ b/client/src/app/shared/utils.ts
@@ -2,15 +2,7 @@ import { DatePipe } from '@angular/common'
2 2
3export class Utils { 3export class Utils {
4 4
5 static dateToHuman (date: String) { 5 static dateToHuman (date: Date) {
6 return new DatePipe('en').transform(date, 'medium') 6 return new DatePipe('en').transform(date, 'medium')
7 } 7 }
8
9 static getRowDeleteButton () {
10 return '<span class="glyphicon glyphicon-remove glyphicon-black"></span>'
11 }
12
13 static getRowEditButton () {
14 return '<span class="glyphicon glyphicon-pencil glyphicon-black"></span>'
15 }
16} 8}
diff --git a/client/src/app/shared/video-abuse/video-abuse.service.ts b/client/src/app/shared/video-abuse/video-abuse.service.ts
index 636a02084..984581114 100644
--- a/client/src/app/shared/video-abuse/video-abuse.service.ts
+++ b/client/src/app/shared/video-abuse/video-abuse.service.ts
@@ -1,42 +1,53 @@
1import { Injectable } from '@angular/core' 1import { Injectable } from '@angular/core'
2import { Http } from '@angular/http' 2import { HttpClient, HttpParams } from '@angular/common/http'
3import { Observable } from 'rxjs/Observable'
4import 'rxjs/add/operator/catch' 3import 'rxjs/add/operator/catch'
5import 'rxjs/add/operator/map' 4import 'rxjs/add/operator/map'
5import { Observable } from 'rxjs/Observable'
6
7import { SortMeta } from 'primeng/primeng'
6 8
7import { AuthService } from '../core' 9import { AuthService } from '../core'
8import { AuthHttp } from '../auth' 10import { RestExtractor, RestPagination, RestService } from '../rest'
9import { RestDataSource, RestExtractor, ResultList } from '../rest' 11import { Utils } from '../utils'
10import { VideoAbuse } from '../../../../../shared' 12import { ResultList, VideoAbuse } from '../../../../../shared'
11 13
12@Injectable() 14@Injectable()
13export class VideoAbuseService { 15export class VideoAbuseService {
14 private static BASE_VIDEO_ABUSE_URL = API_URL + '/api/v1/videos/' 16 private static BASE_VIDEO_ABUSE_URL = API_URL + '/api/v1/videos/'
15 17
16 constructor ( 18 constructor (
17 private authHttp: AuthHttp, 19 private authHttp: HttpClient,
20 private restService: RestService,
18 private restExtractor: RestExtractor 21 private restExtractor: RestExtractor
19 ) {} 22 ) {}
20 23
21 getDataSource () { 24 getVideoAbuses (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoAbuse>> {
22 return new RestDataSource(this.authHttp, VideoAbuseService.BASE_VIDEO_ABUSE_URL + 'abuse') 25 const url = VideoAbuseService.BASE_VIDEO_ABUSE_URL + 'abuse'
26
27 let params = new HttpParams()
28 params = this.restService.addRestGetParams(params, pagination, sort)
29
30 return this.authHttp.get<ResultList<VideoAbuse>>(url, { params })
31 .map(res => this.restExtractor.convertResultListDateToHuman(res))
32 .map(res => this.restExtractor.applyToResultListData(res, this.formatVideoAbuse.bind(this)))
33 .catch(res => this.restExtractor.handleError(res))
23 } 34 }
24 35
25 reportVideo (id: number, reason: string) { 36 reportVideo (id: number, reason: string) {
37 const url = VideoAbuseService.BASE_VIDEO_ABUSE_URL + id + '/abuse'
26 const body = { 38 const body = {
27 reason 39 reason
28 } 40 }
29 const url = VideoAbuseService.BASE_VIDEO_ABUSE_URL + id + '/abuse'
30 41
31 return this.authHttp.post(url, body) 42 return this.authHttp.post(url, body)
32 .map(this.restExtractor.extractDataBool) 43 .map(this.restExtractor.extractDataBool)
33 .catch((res) => this.restExtractor.handleError(res)) 44 .catch(res => this.restExtractor.handleError(res))
34 } 45 }
35 46
36 private extractVideoAbuses (result: ResultList) { 47 private formatVideoAbuse (videoAbuse: VideoAbuse) {
37 const videoAbuses: VideoAbuse[] = result.data 48 return Object.assign(videoAbuse, {
38 const totalVideoAbuses = result.total 49 createdAt: Utils.dateToHuman(videoAbuse.createdAt)
39 50 })
40 return { videoAbuses, totalVideoAbuses }
41 } 51 }
52
42} 53}