]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/shared/video.service.ts
Upgrade client dep
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.service.ts
CommitLineData
df98563e 1import { Injectable } from '@angular/core'
df98563e 2import { Observable } from 'rxjs/Observable'
7a8032bb 3import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
df98563e
C
4import 'rxjs/add/operator/catch'
5import 'rxjs/add/operator/map'
6
df98563e 7import { SortField } from './sort-field.type'
d38b8281 8import {
d38b8281 9 RestExtractor,
d38b8281 10 RestService,
7a8032bb
C
11 UserService,
12 Search
df98563e
C
13} from '../../shared'
14import { Video } from './video.model'
404b54e1
C
15import { VideoDetails } from './video-details.model'
16import { VideoEdit } from './video-edit.model'
d592e0a9 17import { VideoPagination } from './video-pagination.model'
4771e000 18import {
bfb3a98f
C
19 UserVideoRate,
20 VideoRateType,
21 VideoUpdate,
bfb3a98f
C
22 UserVideoRateUpdate,
23 Video as VideoServerModel,
404b54e1 24 VideoDetails as VideoDetailsServerModel,
bfb3a98f 25 ResultList
4771e000 26} from '../../../../../shared'
dc8bc31b
C
27
28@Injectable()
41a2aee3 29export class VideoService {
df98563e 30 private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
dc8bc31b 31
df98563e 32 constructor (
d592e0a9 33 private authHttp: HttpClient,
de59c48f
C
34 private restExtractor: RestExtractor,
35 private restService: RestService
4fd8aa32
C
36 ) {}
37
9d9597df 38 getVideo (uuid: string): Observable<VideoDetails> {
404b54e1
C
39 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
40 .map(videoHash => new VideoDetails(videoHash))
d592e0a9 41 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32 42 }
dc8bc31b 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}