]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Design confirm dialog
[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'
f3aaa9a9 14import { Search } from '../header/search.model'
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 {
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
f3aaa9a9
C
94 searchVideos (search: string, videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
95 const url = VideoService.BASE_VIDEO_URL + 'search'
d592e0a9
C
96
97 const pagination = this.videoPaginationToRestPagination(videoPagination)
98
99 let params = new HttpParams()
100 params = this.restService.addRestGetParams(params, pagination, sort)
f3aaa9a9 101 params = params.append('search', search)
cf20596c 102
fd45e8f4
C
103 return this.authHttp
104 .get<ResultList<VideoServerModel>>(url, { params })
105 .map(this.extractVideos)
106 .catch((res) => this.restExtractor.handleError(res))
d592e0a9
C
107 }
108
109 removeVideo (id: number) {
fd45e8f4
C
110 return this.authHttp
111 .delete(VideoService.BASE_VIDEO_URL + id)
112 .map(this.restExtractor.extractDataBool)
113 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32
C
114 }
115
2de96f4d
C
116 loadCompleteDescription (descriptionPath: string) {
117 return this.authHttp
118 .get(API_URL + descriptionPath)
119 .map(res => res['description'])
120 .catch((res) => this.restExtractor.handleError(res))
d38b8281
C
121 }
122
0a6658fd 123 setVideoLike (id: number) {
df98563e 124 return this.setVideoRate(id, 'like')
d38b8281
C
125 }
126
0a6658fd 127 setVideoDislike (id: number) {
df98563e 128 return this.setVideoRate(id, 'dislike')
d38b8281
C
129 }
130
0a6658fd 131 getUserVideoRating (id: number): Observable<UserVideoRate> {
334ddfa4 132 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 133
fd45e8f4
C
134 return this.authHttp
135 .get(url)
136 .catch(res => this.restExtractor.handleError(res))
d38b8281
C
137 }
138
d592e0a9
C
139 private videoPaginationToRestPagination (videoPagination: VideoPagination) {
140 const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
141 const count: number = videoPagination.itemsPerPage
142
143 return { start, count }
198b205c
GS
144 }
145
0a6658fd 146 private setVideoRate (id: number, rateType: VideoRateType) {
df98563e 147 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
4771e000 148 const body: UserVideoRateUpdate = {
d38b8281 149 rating: rateType
df98563e 150 }
d38b8281 151
fd45e8f4
C
152 return this.authHttp
153 .put(url, body)
154 .map(this.restExtractor.extractDataBool)
155 .catch(res => this.restExtractor.handleError(res))
4f8c0eb0
C
156 }
157
d592e0a9 158 private extractVideos (result: ResultList<VideoServerModel>) {
df98563e
C
159 const videosJson = result.data
160 const totalVideos = result.total
161 const videos = []
d592e0a9 162
de59c48f 163 for (const videoJson of videosJson) {
df98563e 164 videos.push(new Video(videoJson))
501bc6c2
C
165 }
166
df98563e 167 return { videos, totalVideos }
501bc6c2 168 }
dc8bc31b 169}