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