]>
Commit | Line | Data |
---|---|---|
df98563e C |
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' | |
df98563e | 9 | import { AuthService } from '../../core' |
d38b8281 C |
10 | import { |
11 | AuthHttp, | |
12 | RestExtractor, | |
13 | RestPagination, | |
14 | RestService, | |
15 | ResultList, | |
16 | UserService | |
df98563e C |
17 | } from '../../shared' |
18 | import { Video } from './video.model' | |
4771e000 C |
19 | import { |
20 | UserVideoRate, | |
21 | VideoRateType, | |
22 | VideoUpdate, | |
23 | VideoAbuseCreate, | |
24 | UserVideoRateUpdate | |
25 | } from '../../../../../shared' | |
dc8bc31b C |
26 | |
27 | @Injectable() | |
41a2aee3 | 28 | export 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 ( |
4fd8aa32 | 36 | private authService: AuthService, |
bd5c83a8 | 37 | private authHttp: AuthHttp, |
de59c48f C |
38 | private http: Http, |
39 | private restExtractor: RestExtractor, | |
40 | private restService: RestService | |
4fd8aa32 C |
41 | ) {} |
42 | ||
df98563e | 43 | loadVideoCategories () { |
4771e000 | 44 | return this.loadVideoAttributeEnum('categories', this.videoCategories) |
6e07c3de C |
45 | } |
46 | ||
df98563e | 47 | loadVideoLicences () { |
4771e000 | 48 | return this.loadVideoAttributeEnum('licences', this.videoLicences) |
d07137b9 C |
49 | } |
50 | ||
df98563e | 51 | loadVideoLanguages () { |
4771e000 | 52 | return this.loadVideoAttributeEnum('languages', this.videoLanguages) |
db216afd C |
53 | } |
54 | ||
0a6658fd C |
55 | getVideo (uuid: string): Observable<Video> { |
56 | return this.http.get(VideoService.BASE_VIDEO_URL + uuid) | |
de59c48f | 57 | .map(this.restExtractor.extractDataGet) |
df98563e C |
58 | .map(videoHash => new Video(videoHash)) |
59 | .catch((res) => this.restExtractor.handleError(res)) | |
4fd8aa32 | 60 | } |
dc8bc31b | 61 | |
df98563e C |
62 | updateVideo (video: Video) { |
63 | const language = video.language ? video.language : null | |
c24ac1c1 | 64 | |
4771e000 | 65 | const body: VideoUpdate = { |
d8e689b8 C |
66 | name: video.name, |
67 | category: video.category, | |
68 | licence: video.licence, | |
c24ac1c1 | 69 | language, |
d8e689b8 | 70 | description: video.description, |
4771e000 C |
71 | tags: video.tags, |
72 | nsfw: video.nsfw | |
df98563e | 73 | } |
c24ac1c1 | 74 | |
df98563e C |
75 | const headers = new Headers({ 'Content-Type': 'application/json' }) |
76 | const options = new RequestOptions({ headers: headers }) | |
d8e689b8 C |
77 | |
78 | return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${video.id}`, body, options) | |
79 | .map(this.restExtractor.extractDataBool) | |
df98563e | 80 | .catch(this.restExtractor.handleError) |
d8e689b8 C |
81 | } |
82 | ||
df98563e C |
83 | getVideos (pagination: RestPagination, sort: SortField) { |
84 | const params = this.restService.buildRestGetParams(pagination, sort) | |
cf20596c | 85 | |
ccf6ed16 | 86 | return this.http.get(VideoService.BASE_VIDEO_URL, { search: params }) |
501bc6c2 C |
87 | .map(res => res.json()) |
88 | .map(this.extractVideos) | |
df98563e | 89 | .catch((res) => this.restExtractor.handleError(res)) |
dc8bc31b C |
90 | } |
91 | ||
0a6658fd | 92 | removeVideo (id: number) { |
bd5c83a8 | 93 | return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id) |
de59c48f | 94 | .map(this.restExtractor.extractDataBool) |
df98563e | 95 | .catch((res) => this.restExtractor.handleError(res)) |
dc8bc31b C |
96 | } |
97 | ||
df98563e C |
98 | searchVideos (search: Search, pagination: RestPagination, sort: SortField) { |
99 | const params = this.restService.buildRestGetParams(pagination, sort) | |
cf20596c | 100 | |
df98563e | 101 | if (search.field) params.set('field', search.field) |
cf20596c | 102 | |
ccf6ed16 | 103 | return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params }) |
de59c48f | 104 | .map(this.restExtractor.extractDataList) |
501bc6c2 | 105 | .map(this.extractVideos) |
df98563e | 106 | .catch((res) => this.restExtractor.handleError(res)) |
4fd8aa32 C |
107 | } |
108 | ||
0a6658fd | 109 | reportVideo (id: number, reason: string) { |
df98563e | 110 | const url = VideoService.BASE_VIDEO_URL + id + '/abuse' |
4771e000 | 111 | const body: VideoAbuseCreate = { |
4f8c0eb0 | 112 | reason |
df98563e | 113 | } |
4f8c0eb0 C |
114 | |
115 | return this.authHttp.post(url, body) | |
d38b8281 | 116 | .map(this.restExtractor.extractDataBool) |
df98563e | 117 | .catch((res) => this.restExtractor.handleError(res)) |
d38b8281 C |
118 | } |
119 | ||
0a6658fd | 120 | setVideoLike (id: number) { |
df98563e | 121 | return this.setVideoRate(id, 'like') |
d38b8281 C |
122 | } |
123 | ||
0a6658fd | 124 | setVideoDislike (id: number) { |
df98563e | 125 | return this.setVideoRate(id, 'dislike') |
d38b8281 C |
126 | } |
127 | ||
0a6658fd | 128 | getUserVideoRating (id: number): Observable<UserVideoRate> { |
df98563e | 129 | const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating' |
d38b8281 C |
130 | |
131 | return this.authHttp.get(url) | |
132 | .map(this.restExtractor.extractDataGet) | |
df98563e | 133 | .catch((res) => this.restExtractor.handleError(res)) |
d38b8281 C |
134 | } |
135 | ||
0a6658fd | 136 | blacklistVideo (id: number) { |
198b205c | 137 | return this.authHttp.post(VideoService.BASE_VIDEO_URL + id + '/blacklist', {}) |
ab683a8e | 138 | .map(this.restExtractor.extractDataBool) |
df98563e | 139 | .catch((res) => this.restExtractor.handleError(res)) |
198b205c GS |
140 | } |
141 | ||
0a6658fd | 142 | private setVideoRate (id: number, rateType: VideoRateType) { |
df98563e | 143 | const url = VideoService.BASE_VIDEO_URL + id + '/rate' |
4771e000 | 144 | const body: UserVideoRateUpdate = { |
d38b8281 | 145 | rating: rateType |
df98563e | 146 | } |
d38b8281 C |
147 | |
148 | return this.authHttp.put(url, body) | |
149 | .map(this.restExtractor.extractDataBool) | |
df98563e | 150 | .catch((res) => this.restExtractor.handleError(res)) |
4f8c0eb0 C |
151 | } |
152 | ||
df98563e C |
153 | private extractVideos (result: ResultList) { |
154 | const videosJson = result.data | |
155 | const totalVideos = result.total | |
156 | const videos = [] | |
de59c48f | 157 | for (const videoJson of videosJson) { |
df98563e | 158 | videos.push(new Video(videoJson)) |
501bc6c2 C |
159 | } |
160 | ||
df98563e | 161 | return { videos, totalVideos } |
501bc6c2 | 162 | } |
4771e000 C |
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 | } | |
dc8bc31b | 176 | } |