]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Design signup and login pages
[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'
12import { RestExtractor } from '../rest/rest-extractor.service'
13import { RestService } from '../rest/rest.service'
14import { Search } from '../search/search.model'
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 {
df98563e 24 private static BASE_VIDEO_URL = API_URL + '/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) {
df98563e 45 const language = video.language ? video.language : null
c24ac1c1 46
4771e000 47 const body: VideoUpdate = {
d8e689b8
C
48 name: video.name,
49 category: video.category,
50 licence: video.licence,
c24ac1c1 51 language,
d8e689b8 52 description: video.description,
fd45e8f4 53 privacy: video.privacy,
4771e000
C
54 tags: video.tags,
55 nsfw: video.nsfw
df98563e 56 }
c24ac1c1 57
334ddfa4 58 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
d8e689b8 59 .map(this.restExtractor.extractDataBool)
df98563e 60 .catch(this.restExtractor.handleError)
d8e689b8
C
61 }
62
db7af09b 63 uploadVideo (video: FormData) {
334ddfa4 64 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
bfb3a98f 65
fd45e8f4
C
66 return this.authHttp
67 .request(req)
68 .catch(this.restExtractor.handleError)
bfb3a98f
C
69 }
70
fd45e8f4 71 getMyVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
d592e0a9 72 const pagination = this.videoPaginationToRestPagination(videoPagination)
cf20596c 73
d592e0a9
C
74 let params = new HttpParams()
75 params = this.restService.addRestGetParams(params, pagination, sort)
dc8bc31b 76
fd45e8f4
C
77 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
78 .map(this.extractVideos)
79 .catch((res) => this.restExtractor.handleError(res))
dc8bc31b
C
80 }
81
fd45e8f4
C
82 getVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
83 const pagination = this.videoPaginationToRestPagination(videoPagination)
84
85 let params = new HttpParams()
86 params = this.restService.addRestGetParams(params, pagination, sort)
87
88 return this.authHttp
89 .get(VideoService.BASE_VIDEO_URL, { params })
90 .map(this.extractVideos)
91 .catch((res) => this.restExtractor.handleError(res))
92 }
93
94 searchVideos (search: Search, videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
d592e0a9
C
95 const url = VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value)
96
97 const pagination = this.videoPaginationToRestPagination(videoPagination)
98
99 let params = new HttpParams()
100 params = this.restService.addRestGetParams(params, pagination, sort)
cf20596c 101
df98563e 102 if (search.field) params.set('field', search.field)
cf20596c 103
fd45e8f4
C
104 return this.authHttp
105 .get<ResultList<VideoServerModel>>(url, { params })
106 .map(this.extractVideos)
107 .catch((res) => this.restExtractor.handleError(res))
d592e0a9
C
108 }
109
110 removeVideo (id: number) {
fd45e8f4
C
111 return this.authHttp
112 .delete(VideoService.BASE_VIDEO_URL + id)
113 .map(this.restExtractor.extractDataBool)
114 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32
C
115 }
116
2de96f4d
C
117 loadCompleteDescription (descriptionPath: string) {
118 return this.authHttp
119 .get(API_URL + descriptionPath)
120 .map(res => res['description'])
121 .catch((res) => this.restExtractor.handleError(res))
d38b8281
C
122 }
123
0a6658fd 124 setVideoLike (id: number) {
df98563e 125 return this.setVideoRate(id, 'like')
d38b8281
C
126 }
127
0a6658fd 128 setVideoDislike (id: number) {
df98563e 129 return this.setVideoRate(id, 'dislike')
d38b8281
C
130 }
131
0a6658fd 132 getUserVideoRating (id: number): Observable<UserVideoRate> {
334ddfa4 133 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 134
fd45e8f4
C
135 return this.authHttp
136 .get(url)
137 .catch(res => this.restExtractor.handleError(res))
d38b8281
C
138 }
139
d592e0a9
C
140 private videoPaginationToRestPagination (videoPagination: VideoPagination) {
141 const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
142 const count: number = videoPagination.itemsPerPage
143
144 return { start, count }
198b205c
GS
145 }
146
0a6658fd 147 private setVideoRate (id: number, rateType: VideoRateType) {
df98563e 148 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
4771e000 149 const body: UserVideoRateUpdate = {
d38b8281 150 rating: rateType
df98563e 151 }
d38b8281 152
fd45e8f4
C
153 return this.authHttp
154 .put(url, body)
155 .map(this.restExtractor.extractDataBool)
156 .catch(res => this.restExtractor.handleError(res))
4f8c0eb0
C
157 }
158
d592e0a9 159 private extractVideos (result: ResultList<VideoServerModel>) {
df98563e
C
160 const videosJson = result.data
161 const totalVideos = result.total
162 const videos = []
d592e0a9 163
de59c48f 164 for (const videoJson of videosJson) {
df98563e 165 videos.push(new Video(videoJson))
501bc6c2
C
166 }
167
df98563e 168 return { videos, totalVideos }
501bc6c2 169 }
dc8bc31b 170}