]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Begin to add avatar to actors
[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) {
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
4635f59d
C
74 getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
75 const pagination = this.restService.componentPaginationToRestPagination(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
4635f59d
C
85 getVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
86 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
fd45e8f4
C
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
4635f59d
C
97 searchVideos (
98 search: string,
99 videoPagination: ComponentPagination,
100 sort: SortField
101 ): Observable<{ videos: Video[], totalVideos: number}> {
f3aaa9a9 102 const url = VideoService.BASE_VIDEO_URL + 'search'
d592e0a9 103
4635f59d 104 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
d592e0a9
C
105
106 let params = new HttpParams()
107 params = this.restService.addRestGetParams(params, pagination, sort)
f3aaa9a9 108 params = params.append('search', search)
cf20596c 109
fd45e8f4
C
110 return this.authHttp
111 .get<ResultList<VideoServerModel>>(url, { params })
112 .map(this.extractVideos)
113 .catch((res) => this.restExtractor.handleError(res))
d592e0a9
C
114 }
115
116 removeVideo (id: number) {
fd45e8f4
C
117 return this.authHttp
118 .delete(VideoService.BASE_VIDEO_URL + id)
119 .map(this.restExtractor.extractDataBool)
120 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32
C
121 }
122
2de96f4d
C
123 loadCompleteDescription (descriptionPath: string) {
124 return this.authHttp
63c4db6d 125 .get(environment.apiUrl + descriptionPath)
2de96f4d
C
126 .map(res => res['description'])
127 .catch((res) => this.restExtractor.handleError(res))
d38b8281
C
128 }
129
0a6658fd 130 setVideoLike (id: number) {
df98563e 131 return this.setVideoRate(id, 'like')
d38b8281
C
132 }
133
0a6658fd 134 setVideoDislike (id: number) {
df98563e 135 return this.setVideoRate(id, 'dislike')
d38b8281
C
136 }
137
0a6658fd 138 getUserVideoRating (id: number): Observable<UserVideoRate> {
334ddfa4 139 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 140
fd45e8f4
C
141 return this.authHttp
142 .get(url)
143 .catch(res => this.restExtractor.handleError(res))
d38b8281
C
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}