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