]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Move to angular cli
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video.service.ts
CommitLineData
7a8032bb 1import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
202f6b6c 2import { Injectable } from '@angular/core'
df98563e
C
3import 'rxjs/add/operator/catch'
4import 'rxjs/add/operator/map'
202f6b6c
C
5import { Observable } from 'rxjs/Observable'
6import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared'
7import { ResultList } from '../../../../../shared/models/result-list.model'
8import { UserVideoRateUpdate } from '../../../../../shared/models/videos/user-video-rate-update.model'
9import { UserVideoRate } from '../../../../../shared/models/videos/user-video-rate.model'
10import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
11import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
63c4db6d 12import { environment } from '../../../environments/environment'
202f6b6c
C
13import { RestExtractor } from '../rest/rest-extractor.service'
14import { RestService } from '../rest/rest.service'
202f6b6c 15import { UserService } from '../users/user.service'
df98563e 16import { SortField } from './sort-field.type'
404b54e1
C
17import { VideoDetails } from './video-details.model'
18import { VideoEdit } from './video-edit.model'
d592e0a9 19import { VideoPagination } from './video-pagination.model'
202f6b6c 20import { Video } from './video.model'
dc8bc31b
C
21
22@Injectable()
41a2aee3 23export class VideoService {
63c4db6d 24 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
dc8bc31b 25
df98563e 26 constructor (
d592e0a9 27 private authHttp: HttpClient,
de59c48f
C
28 private restExtractor: RestExtractor,
29 private restService: RestService
4fd8aa32
C
30 ) {}
31
9d9597df 32 getVideo (uuid: string): Observable<VideoDetails> {
404b54e1
C
33 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
34 .map(videoHash => new VideoDetails(videoHash))
d592e0a9 35 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32 36 }
dc8bc31b 37
1f3e9fec
C
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
404b54e1 44 updateVideo (video: VideoEdit) {
cadb46d8
C
45 const language = video.language || undefined
46 const licence = video.licence || undefined
47 const category = video.category || undefined
48 const description = video.description || undefined
c24ac1c1 49
4771e000 50 const body: VideoUpdate = {
d8e689b8 51 name: video.name,
cadb46d8
C
52 category,
53 licence,
c24ac1c1 54 language,
cadb46d8 55 description,
fd45e8f4 56 privacy: video.privacy,
4771e000
C
57 tags: video.tags,
58 nsfw: video.nsfw
df98563e 59 }
c24ac1c1 60
334ddfa4 61 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
d8e689b8 62 .map(this.restExtractor.extractDataBool)
df98563e 63 .catch(this.restExtractor.handleError)
d8e689b8
C
64 }
65
db7af09b 66 uploadVideo (video: FormData) {
334ddfa4 67 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
bfb3a98f 68
fd45e8f4
C
69 return this.authHttp
70 .request(req)
71 .catch(this.restExtractor.handleError)
bfb3a98f
C
72 }
73
fd45e8f4 74 getMyVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
d592e0a9 75 const pagination = this.videoPaginationToRestPagination(videoPagination)
cf20596c 76
d592e0a9
C
77 let params = new HttpParams()
78 params = this.restService.addRestGetParams(params, pagination, sort)
dc8bc31b 79
fd45e8f4
C
80 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
81 .map(this.extractVideos)
82 .catch((res) => this.restExtractor.handleError(res))
dc8bc31b
C
83 }
84
fd45e8f4
C
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
f3aaa9a9
C
97 searchVideos (search: string, videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
98 const url = VideoService.BASE_VIDEO_URL + 'search'
d592e0a9
C
99
100 const pagination = this.videoPaginationToRestPagination(videoPagination)
101
102 let params = new HttpParams()
103 params = this.restService.addRestGetParams(params, pagination, sort)
f3aaa9a9 104 params = params.append('search', search)
cf20596c 105
fd45e8f4
C
106 return this.authHttp
107 .get<ResultList<VideoServerModel>>(url, { params })
108 .map(this.extractVideos)
109 .catch((res) => this.restExtractor.handleError(res))
d592e0a9
C
110 }
111
112 removeVideo (id: number) {
fd45e8f4
C
113 return this.authHttp
114 .delete(VideoService.BASE_VIDEO_URL + id)
115 .map(this.restExtractor.extractDataBool)
116 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32
C
117 }
118
2de96f4d
C
119 loadCompleteDescription (descriptionPath: string) {
120 return this.authHttp
63c4db6d 121 .get(environment.apiUrl + descriptionPath)
2de96f4d
C
122 .map(res => res['description'])
123 .catch((res) => this.restExtractor.handleError(res))
d38b8281
C
124 }
125
0a6658fd 126 setVideoLike (id: number) {
df98563e 127 return this.setVideoRate(id, 'like')
d38b8281
C
128 }
129
0a6658fd 130 setVideoDislike (id: number) {
df98563e 131 return this.setVideoRate(id, 'dislike')
d38b8281
C
132 }
133
0a6658fd 134 getUserVideoRating (id: number): Observable<UserVideoRate> {
334ddfa4 135 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 136
fd45e8f4
C
137 return this.authHttp
138 .get(url)
139 .catch(res => this.restExtractor.handleError(res))
d38b8281
C
140 }
141
d592e0a9
C
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 }
198b205c
GS
147 }
148
0a6658fd 149 private setVideoRate (id: number, rateType: VideoRateType) {
df98563e 150 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
4771e000 151 const body: UserVideoRateUpdate = {
d38b8281 152 rating: rateType
df98563e 153 }
d38b8281 154
fd45e8f4
C
155 return this.authHttp
156 .put(url, body)
157 .map(this.restExtractor.extractDataBool)
158 .catch(res => this.restExtractor.handleError(res))
4f8c0eb0
C
159 }
160
d592e0a9 161 private extractVideos (result: ResultList<VideoServerModel>) {
df98563e
C
162 const videosJson = result.data
163 const totalVideos = result.total
164 const videos = []
d592e0a9 165
de59c48f 166 for (const videoJson of videosJson) {
df98563e 167 videos.push(new Video(videoJson))
501bc6c2
C
168 }
169
df98563e 170 return { videos, totalVideos }
501bc6c2 171 }
dc8bc31b 172}