]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/shared/video.service.ts
Server: upgrade packages
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.service.ts
CommitLineData
df98563e 1import { Injectable } from '@angular/core'
df98563e
C
2import { Observable } from 'rxjs/Observable'
3import 'rxjs/add/operator/catch'
4import 'rxjs/add/operator/map'
bfb3a98f 5import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
df98563e
C
6
7import { Search } from '../../shared'
8import { SortField } from './sort-field.type'
d38b8281 9import {
d38b8281 10 RestExtractor,
d38b8281 11 RestService,
d38b8281 12 UserService
df98563e
C
13} from '../../shared'
14import { Video } from './video.model'
d592e0a9 15import { VideoPagination } from './video-pagination.model'
4771e000 16import {
bfb3a98f
C
17 VideoCreate,
18 UserVideoRate,
19 VideoRateType,
20 VideoUpdate,
21 VideoAbuseCreate,
22 UserVideoRateUpdate,
23 Video as VideoServerModel,
24 ResultList
4771e000 25} from '../../../../../shared'
dc8bc31b
C
26
27@Injectable()
41a2aee3 28export class VideoService {
df98563e 29 private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
dc8bc31b 30
df98563e
C
31 videoCategories: Array<{ id: number, label: string }> = []
32 videoLicences: Array<{ id: number, label: string }> = []
33 videoLanguages: Array<{ id: number, label: string }> = []
6e07c3de 34
df98563e 35 constructor (
d592e0a9 36 private authHttp: HttpClient,
de59c48f
C
37 private restExtractor: RestExtractor,
38 private restService: RestService
4fd8aa32
C
39 ) {}
40
df98563e 41 loadVideoCategories () {
4771e000 42 return this.loadVideoAttributeEnum('categories', this.videoCategories)
6e07c3de
C
43 }
44
df98563e 45 loadVideoLicences () {
4771e000 46 return this.loadVideoAttributeEnum('licences', this.videoLicences)
d07137b9
C
47 }
48
df98563e 49 loadVideoLanguages () {
4771e000 50 return this.loadVideoAttributeEnum('languages', this.videoLanguages)
db216afd
C
51 }
52
d592e0a9
C
53 getVideo (uuid: string) {
54 return this.authHttp.get<VideoServerModel>(VideoService.BASE_VIDEO_URL + uuid)
55 .map(videoHash => new Video(videoHash))
56 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32 57 }
dc8bc31b 58
df98563e
C
59 updateVideo (video: Video) {
60 const language = video.language ? video.language : null
c24ac1c1 61
4771e000 62 const body: VideoUpdate = {
d8e689b8
C
63 name: video.name,
64 category: video.category,
65 licence: video.licence,
c24ac1c1 66 language,
d8e689b8 67 description: video.description,
4771e000
C
68 tags: video.tags,
69 nsfw: video.nsfw
df98563e 70 }
c24ac1c1 71
d592e0a9 72 return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${video.id}`, body)
d8e689b8 73 .map(this.restExtractor.extractDataBool)
df98563e 74 .catch(this.restExtractor.handleError)
d8e689b8
C
75 }
76
bfb3a98f
C
77 // uploadVideo (video: VideoCreate) {
78 uploadVideo (video: any) {
79 const req = new HttpRequest('POST', `${VideoService.BASE_VIDEO_URL}/upload`, video, { reportProgress: true })
80
81 return this.authHttp.request(req)
82 .catch(this.restExtractor.handleError)
83 }
84
d592e0a9
C
85 getVideos (videoPagination: VideoPagination, sort: SortField) {
86 const pagination = this.videoPaginationToRestPagination(videoPagination)
cf20596c 87
d592e0a9
C
88 let params = new HttpParams()
89 params = this.restService.addRestGetParams(params, pagination, sort)
dc8bc31b 90
d592e0a9
C
91 return this.authHttp.get(VideoService.BASE_VIDEO_URL, { params })
92 .map(this.extractVideos)
df98563e 93 .catch((res) => this.restExtractor.handleError(res))
dc8bc31b
C
94 }
95
d592e0a9
C
96 searchVideos (search: Search, videoPagination: VideoPagination, sort: SortField) {
97 const url = VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value)
98
99 const pagination = this.videoPaginationToRestPagination(videoPagination)
100
101 let params = new HttpParams()
102 params = this.restService.addRestGetParams(params, pagination, sort)
cf20596c 103
df98563e 104 if (search.field) params.set('field', search.field)
cf20596c 105
d592e0a9
C
106 return this.authHttp.get<ResultList<VideoServerModel>>(url, { params })
107 .map(this.extractVideos)
108 .catch((res) => this.restExtractor.handleError(res))
109 }
110
111 removeVideo (id: number) {
112 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
113 .map(this.restExtractor.extractDataBool)
114 .catch((res) => this.restExtractor.handleError(res))
4fd8aa32
C
115 }
116
0a6658fd 117 reportVideo (id: number, reason: string) {
df98563e 118 const url = VideoService.BASE_VIDEO_URL + id + '/abuse'
4771e000 119 const body: VideoAbuseCreate = {
4f8c0eb0 120 reason
df98563e 121 }
4f8c0eb0
C
122
123 return this.authHttp.post(url, body)
d38b8281 124 .map(this.restExtractor.extractDataBool)
d592e0a9 125 .catch(res => this.restExtractor.handleError(res))
d38b8281
C
126 }
127
0a6658fd 128 setVideoLike (id: number) {
df98563e 129 return this.setVideoRate(id, 'like')
d38b8281
C
130 }
131
0a6658fd 132 setVideoDislike (id: number) {
df98563e 133 return this.setVideoRate(id, 'dislike')
d38b8281
C
134 }
135
0a6658fd 136 getUserVideoRating (id: number): Observable<UserVideoRate> {
df98563e 137 const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating'
d38b8281
C
138
139 return this.authHttp.get(url)
d592e0a9 140 .catch(res => this.restExtractor.handleError(res))
d38b8281
C
141 }
142
0a6658fd 143 blacklistVideo (id: number) {
198b205c 144 return this.authHttp.post(VideoService.BASE_VIDEO_URL + id + '/blacklist', {})
ab683a8e 145 .map(this.restExtractor.extractDataBool)
d592e0a9
C
146 .catch(res => this.restExtractor.handleError(res))
147 }
148
149 private videoPaginationToRestPagination (videoPagination: VideoPagination) {
150 const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
151 const count: number = videoPagination.itemsPerPage
152
153 return { start, count }
198b205c
GS
154 }
155
0a6658fd 156 private setVideoRate (id: number, rateType: VideoRateType) {
df98563e 157 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
4771e000 158 const body: UserVideoRateUpdate = {
d38b8281 159 rating: rateType
df98563e 160 }
d38b8281
C
161
162 return this.authHttp.put(url, body)
163 .map(this.restExtractor.extractDataBool)
d592e0a9 164 .catch(res => this.restExtractor.handleError(res))
4f8c0eb0
C
165 }
166
d592e0a9 167 private extractVideos (result: ResultList<VideoServerModel>) {
df98563e
C
168 const videosJson = result.data
169 const totalVideos = result.total
170 const videos = []
d592e0a9 171
de59c48f 172 for (const videoJson of videosJson) {
df98563e 173 videos.push(new Video(videoJson))
501bc6c2
C
174 }
175
df98563e 176 return { videos, totalVideos }
501bc6c2 177 }
4771e000
C
178
179 private loadVideoAttributeEnum (attributeName: 'categories' | 'licences' | 'languages', hashToPopulate: { id: number, label: string }[]) {
d592e0a9
C
180 return this.authHttp.get(VideoService.BASE_VIDEO_URL + attributeName)
181 .subscribe(data => {
182 Object.keys(data).forEach(dataKey => {
183 hashToPopulate.push({
184 id: parseInt(dataKey, 10),
185 label: data[dataKey]
186 })
187 })
4771e000 188 })
4771e000 189 }
dc8bc31b 190}