Use global uuid instead of remoteId for videos
[oweals/peertube.git] / client / src / app / videos / shared / video.service.ts
1 import { Injectable } from '@angular/core'
2 import { Http, Headers, RequestOptions } from '@angular/http'
3 import { Observable } from 'rxjs/Observable'
4 import 'rxjs/add/operator/catch'
5 import 'rxjs/add/operator/map'
6
7 import { Search } from '../../shared'
8 import { SortField } from './sort-field.type'
9 import { AuthService } from '../../core'
10 import {
11   AuthHttp,
12   RestExtractor,
13   RestPagination,
14   RestService,
15   ResultList,
16   UserService
17 } from '../../shared'
18 import { Video } from './video.model'
19 import {
20   UserVideoRate,
21   VideoRateType,
22   VideoUpdate,
23   VideoAbuseCreate,
24   UserVideoRateUpdate
25 } from '../../../../../shared'
26
27 @Injectable()
28 export class VideoService {
29   private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
30
31   videoCategories: Array<{ id: number, label: string }> = []
32   videoLicences: Array<{ id: number, label: string }> = []
33   videoLanguages: Array<{ id: number, label: string }> = []
34
35   constructor (
36     private authService: AuthService,
37     private authHttp: AuthHttp,
38     private http: Http,
39     private restExtractor: RestExtractor,
40     private restService: RestService
41   ) {}
42
43   loadVideoCategories () {
44     return this.loadVideoAttributeEnum('categories', this.videoCategories)
45   }
46
47   loadVideoLicences () {
48     return this.loadVideoAttributeEnum('licences', this.videoLicences)
49   }
50
51   loadVideoLanguages () {
52     return this.loadVideoAttributeEnum('languages', this.videoLanguages)
53   }
54
55   getVideo (uuid: string): Observable<Video> {
56     return this.http.get(VideoService.BASE_VIDEO_URL + uuid)
57                     .map(this.restExtractor.extractDataGet)
58                     .map(videoHash => new Video(videoHash))
59                     .catch((res) => this.restExtractor.handleError(res))
60   }
61
62   updateVideo (video: Video) {
63     const language = video.language ? video.language : null
64
65     const body: VideoUpdate = {
66       name: video.name,
67       category: video.category,
68       licence: video.licence,
69       language,
70       description: video.description,
71       tags: video.tags,
72       nsfw: video.nsfw
73     }
74
75     const headers = new Headers({ 'Content-Type': 'application/json' })
76     const options = new RequestOptions({ headers: headers })
77
78     return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${video.id}`, body, options)
79                         .map(this.restExtractor.extractDataBool)
80                         .catch(this.restExtractor.handleError)
81   }
82
83   getVideos (pagination: RestPagination, sort: SortField) {
84     const params = this.restService.buildRestGetParams(pagination, sort)
85
86     return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
87                     .map(res => res.json())
88                     .map(this.extractVideos)
89                     .catch((res) => this.restExtractor.handleError(res))
90   }
91
92   removeVideo (id: number) {
93     return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
94                         .map(this.restExtractor.extractDataBool)
95                         .catch((res) => this.restExtractor.handleError(res))
96   }
97
98   searchVideos (search: Search, pagination: RestPagination, sort: SortField) {
99     const params = this.restService.buildRestGetParams(pagination, sort)
100
101     if (search.field) params.set('field', search.field)
102
103     return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
104                     .map(this.restExtractor.extractDataList)
105                     .map(this.extractVideos)
106                     .catch((res) => this.restExtractor.handleError(res))
107   }
108
109   reportVideo (id: number, reason: string) {
110     const url = VideoService.BASE_VIDEO_URL + id + '/abuse'
111     const body: VideoAbuseCreate = {
112       reason
113     }
114
115     return this.authHttp.post(url, body)
116                         .map(this.restExtractor.extractDataBool)
117                         .catch((res) => this.restExtractor.handleError(res))
118   }
119
120   setVideoLike (id: number) {
121     return this.setVideoRate(id, 'like')
122   }
123
124   setVideoDislike (id: number) {
125     return this.setVideoRate(id, 'dislike')
126   }
127
128   getUserVideoRating (id: number): Observable<UserVideoRate> {
129     const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating'
130
131     return this.authHttp.get(url)
132                         .map(this.restExtractor.extractDataGet)
133                         .catch((res) => this.restExtractor.handleError(res))
134   }
135
136   blacklistVideo (id: number) {
137     return this.authHttp.post(VideoService.BASE_VIDEO_URL + id + '/blacklist', {})
138                         .map(this.restExtractor.extractDataBool)
139                         .catch((res) => this.restExtractor.handleError(res))
140   }
141
142   private setVideoRate (id: number, rateType: VideoRateType) {
143     const url = VideoService.BASE_VIDEO_URL + id + '/rate'
144     const body: UserVideoRateUpdate = {
145       rating: rateType
146     }
147
148     return this.authHttp.put(url, body)
149                         .map(this.restExtractor.extractDataBool)
150                         .catch((res) => this.restExtractor.handleError(res))
151   }
152
153   private extractVideos (result: ResultList) {
154     const videosJson = result.data
155     const totalVideos = result.total
156     const videos = []
157     for (const videoJson of videosJson) {
158       videos.push(new Video(videoJson))
159     }
160
161     return { videos, totalVideos }
162   }
163
164   private loadVideoAttributeEnum (attributeName: 'categories' | 'licences' | 'languages', hashToPopulate: { id: number, label: string }[]) {
165     return this.http.get(VideoService.BASE_VIDEO_URL + attributeName)
166                     .map(this.restExtractor.extractDataGet)
167                     .subscribe(data => {
168                       Object.keys(data).forEach(dataKey => {
169                         hashToPopulate.push({
170                           id: parseInt(dataKey, 10),
171                           label: data[dataKey]
172                         })
173                       })
174                     })
175   }
176 }