]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video.service.ts
Add nsfw support in import-videos
[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'
6de36768 21import { objectToFormData } from '@app/shared/misc/utils'
dc8bc31b
C
22
23@Injectable()
41a2aee3 24export class VideoService {
63c4db6d 25 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
dc8bc31b 26
df98563e 27 constructor (
d592e0a9 28 private authHttp: HttpClient,
de59c48f
C
29 private restExtractor: RestExtractor,
30 private restService: RestService
4fd8aa32
C
31 ) {}
32
8cac1b64
C
33 getVideoViewUrl (uuid: string) {
34 return VideoService.BASE_VIDEO_URL + uuid + '/views'
35 }
36
9d9597df 37 getVideo (uuid: string): Observable<VideoDetails> {
404b54e1
C
38 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
39 .map(videoHash => new VideoDetails(videoHash))
d592e0a9 40 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32 41 }
dc8bc31b 42
1f3e9fec 43 viewVideo (uuid: string): Observable<VideoDetails> {
8cac1b64 44 return this.authHttp.post(this.getVideoViewUrl(uuid), {})
1f3e9fec
C
45 .map(this.restExtractor.extractDataBool)
46 .catch(this.restExtractor.handleError)
47 }
48
404b54e1 49 updateVideo (video: VideoEdit) {
6de36768
C
50 const language = video.language || undefined
51 const licence = video.licence || undefined
52 const category = video.category || undefined
53 const description = video.description || undefined
c24ac1c1 54
4771e000 55 const body: VideoUpdate = {
d8e689b8 56 name: video.name,
cadb46d8
C
57 category,
58 licence,
c24ac1c1 59 language,
cadb46d8 60 description,
fd45e8f4 61 privacy: video.privacy,
4771e000 62 tags: video.tags,
47564bbe 63 nsfw: video.nsfw,
6de36768 64 commentsEnabled: video.commentsEnabled,
07fa4c97 65 support: video.support,
6de36768
C
66 thumbnailfile: video.thumbnailfile,
67 previewfile: video.previewfile
df98563e 68 }
c24ac1c1 69
6de36768
C
70 const data = objectToFormData(body)
71
72 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
d8e689b8 73 .map(this.restExtractor.extractDataBool)
df98563e 74 .catch(this.restExtractor.handleError)
d8e689b8
C
75 }
76
db7af09b 77 uploadVideo (video: FormData) {
334ddfa4 78 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
bfb3a98f 79
fd45e8f4
C
80 return this.authHttp
81 .request(req)
82 .catch(this.restExtractor.handleError)
bfb3a98f
C
83 }
84
4635f59d
C
85 getMyVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
86 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
cf20596c 87
d592e0a9
C
88 let params = new HttpParams()
89 params = this.restService.addRestGetParams(params, pagination, sort)
dc8bc31b 90
fd45e8f4
C
91 return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
92 .map(this.extractVideos)
93 .catch((res) => this.restExtractor.handleError(res))
dc8bc31b
C
94 }
95
4635f59d
C
96 getVideos (videoPagination: ComponentPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
97 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
fd45e8f4
C
98
99 let params = new HttpParams()
100 params = this.restService.addRestGetParams(params, pagination, sort)
101
102 return this.authHttp
103 .get(VideoService.BASE_VIDEO_URL, { params })
104 .map(this.extractVideos)
105 .catch((res) => this.restExtractor.handleError(res))
106 }
107
4635f59d
C
108 searchVideos (
109 search: string,
110 videoPagination: ComponentPagination,
111 sort: SortField
112 ): Observable<{ videos: Video[], totalVideos: number}> {
f3aaa9a9 113 const url = VideoService.BASE_VIDEO_URL + 'search'
d592e0a9 114
4635f59d 115 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
d592e0a9
C
116
117 let params = new HttpParams()
118 params = this.restService.addRestGetParams(params, pagination, sort)
f3aaa9a9 119 params = params.append('search', search)
cf20596c 120
fd45e8f4
C
121 return this.authHttp
122 .get<ResultList<VideoServerModel>>(url, { params })
123 .map(this.extractVideos)
124 .catch((res) => this.restExtractor.handleError(res))
d592e0a9
C
125 }
126
127 removeVideo (id: number) {
fd45e8f4
C
128 return this.authHttp
129 .delete(VideoService.BASE_VIDEO_URL + id)
130 .map(this.restExtractor.extractDataBool)
131 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32
C
132 }
133
2de96f4d
C
134 loadCompleteDescription (descriptionPath: string) {
135 return this.authHttp
63c4db6d 136 .get(environment.apiUrl + descriptionPath)
2de96f4d
C
137 .map(res => res['description'])
138 .catch((res) => this.restExtractor.handleError(res))
d38b8281
C
139 }
140
0a6658fd 141 setVideoLike (id: number) {
df98563e 142 return this.setVideoRate(id, 'like')
d38b8281
C
143 }
144
0a6658fd 145 setVideoDislike (id: number) {
df98563e 146 return this.setVideoRate(id, 'dislike')
d38b8281
C
147 }
148
57a49263
BB
149 unsetVideoLike (id: number) {
150 return this.setVideoRate(id, 'none')
151 }
152
0a6658fd 153 getUserVideoRating (id: number): Observable<UserVideoRate> {
334ddfa4 154 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
d38b8281 155
fd45e8f4
C
156 return this.authHttp
157 .get(url)
158 .catch(res => this.restExtractor.handleError(res))
d38b8281
C
159 }
160
0a6658fd 161 private setVideoRate (id: number, rateType: VideoRateType) {
df98563e 162 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
4771e000 163 const body: UserVideoRateUpdate = {
d38b8281 164 rating: rateType
df98563e 165 }
d38b8281 166
fd45e8f4
C
167 return this.authHttp
168 .put(url, body)
169 .map(this.restExtractor.extractDataBool)
170 .catch(res => this.restExtractor.handleError(res))
4f8c0eb0
C
171 }
172
d592e0a9 173 private extractVideos (result: ResultList<VideoServerModel>) {
df98563e
C
174 const videosJson = result.data
175 const totalVideos = result.total
176 const videos = []
d592e0a9 177
de59c48f 178 for (const videoJson of videosJson) {
df98563e 179 videos.push(new Video(videoJson))
501bc6c2
C
180 }
181
df98563e 182 return { videos, totalVideos }
501bc6c2 183 }
dc8bc31b 184}