]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/video.service.ts
a151a2983170854ebb7863d89f8999a47bce7d26
[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 const support = video.support || undefined
55
56 const body: VideoUpdate = {
57 name: video.name,
58 category,
59 licence,
60 language,
61 support,
62 description,
63 privacy: video.privacy,
64 tags: video.tags,
65 nsfw: video.nsfw,
66 commentsEnabled: video.commentsEnabled,
67 thumbnailfile: video.thumbnailfile,
68 previewfile: video.previewfile
69 }
70
71 const data = objectToFormData(body)
72
73 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
74 .map(this.restExtractor.extractDataBool)
75 .catch(this.restExtractor.handleError)
76 }
77
78 uploadVideo (video: FormData) {
79 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
80
81 return this.authHttp
82 .request(req)
83 .catch(this.restExtractor.handleError)
84 }
85
86 getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
87 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
88
89 let params = new HttpParams()
90 params = this.restService.addRestGetParams(params, pagination, sort)
91
92 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
93 .map(this.extractVideos)
94 .catch((res) => this.restExtractor.handleError(res))
95 }
96
97 getVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
98 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
99
100 let params = new HttpParams()
101 params = this.restService.addRestGetParams(params, pagination, sort)
102
103 return this.authHttp
104 .get(VideoService.BASE_VIDEO_URL, { params })
105 .map(this.extractVideos)
106 .catch((res) => this.restExtractor.handleError(res))
107 }
108
109 searchVideos (
110 search: string,
111 videoPagination: ComponentPagination,
112 sort: SortField
113 ): Observable<{ videos: Video[], totalVideos: number}> {
114 const url = VideoService.BASE_VIDEO_URL + 'search'
115
116 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
117
118 let params = new HttpParams()
119 params = this.restService.addRestGetParams(params, pagination, sort)
120 params = params.append('search', search)
121
122 return this.authHttp
123 .get<ResultList<VideoServerModel>>(url, { params })
124 .map(this.extractVideos)
125 .catch((res) => this.restExtractor.handleError(res))
126 }
127
128 removeVideo (id: number) {
129 return this.authHttp
130 .delete(VideoService.BASE_VIDEO_URL + id)
131 .map(this.restExtractor.extractDataBool)
132 .catch((res) => this.restExtractor.handleError(res))
133 }
134
135 loadCompleteDescription (descriptionPath: string) {
136 return this.authHttp
137 .get(environment.apiUrl + descriptionPath)
138 .map(res => res['description'])
139 .catch((res) => this.restExtractor.handleError(res))
140 }
141
142 setVideoLike (id: number) {
143 return this.setVideoRate(id, 'like')
144 }
145
146 setVideoDislike (id: number) {
147 return this.setVideoRate(id, 'dislike')
148 }
149
150 unsetVideoLike (id: number) {
151 return this.setVideoRate(id, 'none')
152 }
153
154 getUserVideoRating (id: number): Observable<UserVideoRate> {
155 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
156
157 return this.authHttp
158 .get(url)
159 .catch(res => this.restExtractor.handleError(res))
160 }
161
162 private setVideoRate (id: number, rateType: VideoRateType) {
163 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
164 const body: UserVideoRateUpdate = {
165 rating: rateType
166 }
167
168 return this.authHttp
169 .put(url, body)
170 .map(this.restExtractor.extractDataBool)
171 .catch(res => this.restExtractor.handleError(res))
172 }
173
174 private extractVideos (result: ResultList<VideoServerModel>) {
175 const videosJson = result.data
176 const totalVideos = result.total
177 const videos = []
178
179 for (const videoJson of videosJson) {
180 videos.push(new Video(videoJson))
181 }
182
183 return { videos, totalVideos }
184 }
185 }