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