]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video-ownership/video-ownership.service.ts
Users can change ownership of their video [#510] (#888)
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video-ownership / video-ownership.service.ts
CommitLineData
74d63469
GR
1import { catchError, map } from 'rxjs/operators'
2import { HttpClient, HttpParams } from '@angular/common/http'
3import { Injectable } from '@angular/core'
4import { environment } from '../../../environments/environment'
5import { RestExtractor, RestService } from '../rest'
6import { VideoChangeOwnershipCreate } from '../../../../../shared/models/videos'
7import { Observable } from 'rxjs/index'
8import { SortMeta } from 'primeng/components/common/sortmeta'
9import { ResultList, VideoChangeOwnership } from '../../../../../shared'
10import { RestPagination } from '@app/shared/rest'
11import { VideoChangeOwnershipAccept } from '../../../../../shared/models/videos/video-change-ownership-accept.model'
12
13@Injectable()
14export class VideoOwnershipService {
15 private static BASE_VIDEO_CHANGE_OWNERSHIP_URL = environment.apiUrl + '/api/v1/videos/'
16
17 constructor (
18 private authHttp: HttpClient,
19 private restService: RestService,
20 private restExtractor: RestExtractor
21 ) {
22 }
23
24 changeOwnership (id: number, username: string) {
25 const url = VideoOwnershipService.BASE_VIDEO_CHANGE_OWNERSHIP_URL + id + '/give-ownership'
26 const body: VideoChangeOwnershipCreate = {
27 username
28 }
29
30 return this.authHttp.post(url, body)
31 .pipe(
32 map(this.restExtractor.extractDataBool),
33 catchError(res => this.restExtractor.handleError(res))
34 )
35 }
36
37 getOwnershipChanges (pagination: RestPagination, sort: SortMeta): Observable<ResultList<VideoChangeOwnership>> {
38 const url = VideoOwnershipService.BASE_VIDEO_CHANGE_OWNERSHIP_URL + 'ownership'
39
40 let params = new HttpParams()
41 params = this.restService.addRestGetParams(params, pagination, sort)
42
43 return this.authHttp.get<ResultList<VideoChangeOwnership>>(url, { params })
44 .pipe(
45 map(res => this.restExtractor.convertResultListDateToHuman(res)),
46 catchError(res => this.restExtractor.handleError(res))
47 )
48 }
49
50 acceptOwnership (id: number, input: VideoChangeOwnershipAccept) {
51 const url = VideoOwnershipService.BASE_VIDEO_CHANGE_OWNERSHIP_URL + 'ownership/' + id + '/accept'
52 return this.authHttp.post(url, input)
53 .pipe(
54 map(this.restExtractor.extractDataBool),
55 catchError(this.restExtractor.handleError)
56 )
57 }
58
59 refuseOwnership (id: number) {
60 const url = VideoOwnershipService.BASE_VIDEO_CHANGE_OWNERSHIP_URL + 'ownership/' + id + '/refuse'
61 return this.authHttp.post(url, {})
62 .pipe(
63 map(this.restExtractor.extractDataBool),
64 catchError(this.restExtractor.handleError)
65 )
66 }
67}