]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.service.ts
Add account settings new design
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.service.ts
1 import { Injectable } from '@angular/core'
2 import { Observable } from 'rxjs/Observable'
3 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
4 import 'rxjs/add/operator/catch'
5 import 'rxjs/add/operator/map'
6
7 import { SortField } from './sort-field.type'
8 import {
9 RestExtractor,
10 RestService,
11 UserService,
12 Search
13 } from '../../shared'
14 import { Video } from './video.model'
15 import { VideoDetails } from './video-details.model'
16 import { VideoEdit } from './video-edit.model'
17 import { VideoPagination } from './video-pagination.model'
18 import {
19 UserVideoRate,
20 VideoRateType,
21 VideoUpdate,
22 UserVideoRateUpdate,
23 Video as VideoServerModel,
24 VideoDetails as VideoDetailsServerModel,
25 ResultList
26 } from '../../../../../shared'
27
28 @Injectable()
29 export class VideoService {
30 private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
31
32 constructor (
33 private authHttp: HttpClient,
34 private restExtractor: RestExtractor,
35 private restService: RestService
36 ) {}
37
38 getVideo (uuid: string): Observable<VideoDetails> {
39 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
40 .map(videoHash => new VideoDetails(videoHash))
41 .catch((res) => this.restExtractor.handleError(res))
42 }
43
44 viewVideo (uuid: string): Observable<VideoDetails> {
45 return this.authHttp.post(VideoService.BASE_VIDEO_URL + uuid + '/views', {})
46 .map(this.restExtractor.extractDataBool)
47 .catch(this.restExtractor.handleError)
48 }
49
50 updateVideo (video: VideoEdit) {
51 const language = video.language ? video.language : null
52
53 const body: VideoUpdate = {
54 name: video.name,
55 category: video.category,
56 licence: video.licence,
57 language,
58 description: video.description,
59 privacy: video.privacy,
60 tags: video.tags,
61 nsfw: video.nsfw
62 }
63
64 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
65 .map(this.restExtractor.extractDataBool)
66 .catch(this.restExtractor.handleError)
67 }
68
69 uploadVideo (video: FormData) {
70 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
71
72 return this.authHttp
73 .request(req)
74 .catch(this.restExtractor.handleError)
75 }
76
77 getMyVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
78 const pagination = this.videoPaginationToRestPagination(videoPagination)
79
80 let params = new HttpParams()
81 params = this.restService.addRestGetParams(params, pagination, sort)
82
83 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
84 .map(this.extractVideos)
85 .catch((res) => this.restExtractor.handleError(res))
86 }
87
88 getVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
89 const pagination = this.videoPaginationToRestPagination(videoPagination)
90
91 let params = new HttpParams()
92 params = this.restService.addRestGetParams(params, pagination, sort)
93
94 return this.authHttp
95 .get(VideoService.BASE_VIDEO_URL, { params })
96 .map(this.extractVideos)
97 .catch((res) => this.restExtractor.handleError(res))
98 }
99
100 searchVideos (search: Search, videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
101 const url = VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value)
102
103 const pagination = this.videoPaginationToRestPagination(videoPagination)
104
105 let params = new HttpParams()
106 params = this.restService.addRestGetParams(params, pagination, sort)
107
108 if (search.field) params.set('field', search.field)
109
110 return this.authHttp
111 .get<ResultList<VideoServerModel>>(url, { params })
112 .map(this.extractVideos)
113 .catch((res) => this.restExtractor.handleError(res))
114 }
115
116 removeVideo (id: number) {
117 return this.authHttp
118 .delete(VideoService.BASE_VIDEO_URL + id)
119 .map(this.restExtractor.extractDataBool)
120 .catch((res) => this.restExtractor.handleError(res))
121 }
122
123 loadCompleteDescription (descriptionPath: string) {
124 return this.authHttp
125 .get(API_URL + descriptionPath)
126 .map(res => res['description'])
127 .catch((res) => this.restExtractor.handleError(res))
128 }
129
130 setVideoLike (id: number) {
131 return this.setVideoRate(id, 'like')
132 }
133
134 setVideoDislike (id: number) {
135 return this.setVideoRate(id, 'dislike')
136 }
137
138 getUserVideoRating (id: number): Observable<UserVideoRate> {
139 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
140
141 return this.authHttp
142 .get(url)
143 .catch(res => this.restExtractor.handleError(res))
144 }
145
146 private videoPaginationToRestPagination (videoPagination: VideoPagination) {
147 const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
148 const count: number = videoPagination.itemsPerPage
149
150 return { start, count }
151 }
152
153 private setVideoRate (id: number, rateType: VideoRateType) {
154 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
155 const body: UserVideoRateUpdate = {
156 rating: rateType
157 }
158
159 return this.authHttp
160 .put(url, body)
161 .map(this.restExtractor.extractDataBool)
162 .catch(res => this.restExtractor.handleError(res))
163 }
164
165 private extractVideos (result: ResultList<VideoServerModel>) {
166 const videosJson = result.data
167 const totalVideos = result.total
168 const videos = []
169
170 for (const videoJson of videosJson) {
171 videos.push(new Video(videoJson))
172 }
173
174 return { videos, totalVideos }
175 }
176 }