]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Add new name/terms/description config options
[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'
4635f59d 13import { ComponentPagination } from '../rest/component-pagination.model'
202f6b6c
C
14import { RestExtractor } from '../rest/rest-extractor.service'
15import { RestService } from '../rest/rest.service'
202f6b6c 16import { UserService } from '../users/user.service'
df98563e 17import { SortField } from './sort-field.type'
404b54e1
C
18import { VideoDetails } from './video-details.model'
19import { VideoEdit } from './video-edit.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) {
66b16caf
C
45 const language = video.language || null
46 const licence = video.licence || null
47 const category = video.category || null
48 const description = video.description || null
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 57 tags: video.tags,
47564bbe
C
58 nsfw: video.nsfw,
59 commentsEnabled: video.commentsEnabled
df98563e 60 }
c24ac1c1 61
334ddfa4 62 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
d8e689b8 63 .map(this.restExtractor.extractDataBool)
df98563e 64 .catch(this.restExtractor.handleError)
d8e689b8
C
65 }
66
db7af09b 67 uploadVideo (video: FormData) {
334ddfa4 68 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
bfb3a98f 69
fd45e8f4
C
70 return this.authHttp
71 .request(req)
72 .catch(this.restExtractor.handleError)
bfb3a98f
C
73 }
74
4635f59d
C
75 getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
76 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
cf20596c 77
d592e0a9
C
78 let params = new HttpParams()
79 params = this.restService.addRestGetParams(params, pagination, sort)
dc8bc31b 80
fd45e8f4
C
81 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
82 .map(this.extractVideos)
83 .catch((res) => this.restExtractor.handleError(res))
dc8bc31b
C
84 }
85
4635f59d
C
86 getVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
87 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
fd45e8f4
C
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
4635f59d
C
98 searchVideos (
99 search: string,
100 videoPagination: ComponentPagination,
101 sort: SortField
102 ): Observable<{ videos: Video[], totalVideos: number}> {
f3aaa9a9 103 const url = VideoService.BASE_VIDEO_URL + 'search'
d592e0a9 104
4635f59d 105 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
d592e0a9
C
106
107 let params = new HttpParams()
108 params = this.restService.addRestGetParams(params, pagination, sort)
f3aaa9a9 109 params = params.append('search', search)
cf20596c 110
fd45e8f4
C
111 return this.authHttp
112 .get<ResultList<VideoServerModel>>(url, { params })
113 .map(this.extractVideos)
114 .catch((res) => this.restExtractor.handleError(res))
d592e0a9
C
115 }
116
117 removeVideo (id: number) {
fd45e8f4
C
118 return this.authHttp
119 .delete(VideoService.BASE_VIDEO_URL + id)
120 .map(this.restExtractor.extractDataBool)
121 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32
C
122 }
123
2de96f4d
C
124 loadCompleteDescription (descriptionPath: string) {
125 return this.authHttp
63c4db6d 126 .get(environment.apiUrl + descriptionPath)
2de96f4d
C
127 .map(res => res['description'])
128 .catch((res) => this.restExtractor.handleError(res))
d38b8281
C
129 }
130
0a6658fd 131 setVideoLike (id: number) {
df98563e 132 return this.setVideoRate(id, 'like')
d38b8281
C
133 }
134
0a6658fd 135 setVideoDislike (id: number) {
df98563e 136 return this.setVideoRate(id, 'dislike')
d38b8281
C
137 }
138
57a49263
BB
139 unsetVideoLike (id: number) {
140 return this.setVideoRate(id, 'none')
141 }
142
0a6658fd 143 getUserVideoRating (id: number): Observable<UserVideoRate> {
334ddfa4 144 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 145
fd45e8f4
C
146 return this.authHttp
147 .get(url)
148 .catch(res => this.restExtractor.handleError(res))
d38b8281
C
149 }
150
0a6658fd 151 private setVideoRate (id: number, rateType: VideoRateType) {
df98563e 152 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
4771e000 153 const body: UserVideoRateUpdate = {
d38b8281 154 rating: rateType
df98563e 155 }
d38b8281 156
fd45e8f4
C
157 return this.authHttp
158 .put(url, body)
159 .map(this.restExtractor.extractDataBool)
160 .catch(res => this.restExtractor.handleError(res))
4f8c0eb0
C
161 }
162
d592e0a9 163 private extractVideos (result: ResultList<VideoServerModel>) {
df98563e
C
164 const videosJson = result.data
165 const totalVideos = result.total
166 const videos = []
d592e0a9 167
de59c48f 168 for (const videoJson of videosJson) {
df98563e 169 videos.push(new Video(videoJson))
501bc6c2
C
170 }
171
df98563e 172 return { videos, totalVideos }
501bc6c2 173 }
dc8bc31b 174}