]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.service.ts
Add markdown support to video description
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.service.ts
1 import { Injectable } from '@angular/core'
2 import { Observable } from 'rxjs/Observable'
3 import 'rxjs/add/operator/catch'
4 import 'rxjs/add/operator/map'
5 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
6
7 import { Search } from '../../shared'
8 import { SortField } from './sort-field.type'
9 import {
10 RestExtractor,
11 RestService,
12 UserService
13 } from '../../shared'
14 import { Video } from './video.model'
15 import { VideoDetails } from './video-details.model'
16 import { VideoEdit } from './video-edit.model'
17 import { VideoPagination } from './video-pagination.model'
18 import {
19 UserVideoRate,
20 VideoRateType,
21 VideoUpdate,
22 VideoAbuseCreate,
23 UserVideoRateUpdate,
24 Video as VideoServerModel,
25 VideoDetails as VideoDetailsServerModel,
26 ResultList
27 } from '../../../../../shared'
28
29 @Injectable()
30 export class VideoService {
31 private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
32
33 constructor (
34 private authHttp: HttpClient,
35 private restExtractor: RestExtractor,
36 private restService: RestService
37 ) {}
38
39 getVideo (uuid: string): Observable<VideoDetails> {
40 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
41 .map(videoHash => new VideoDetails(videoHash))
42 .catch((res) => this.restExtractor.handleError(res))
43 }
44
45 updateVideo (video: VideoEdit) {
46 const language = video.language ? video.language : null
47
48 const body: VideoUpdate = {
49 name: video.name,
50 category: video.category,
51 licence: video.licence,
52 language,
53 description: video.description,
54 tags: video.tags,
55 nsfw: video.nsfw
56 }
57
58 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
59 .map(this.restExtractor.extractDataBool)
60 .catch(this.restExtractor.handleError)
61 }
62
63 uploadVideo (video: FormData) {
64 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
65
66 return this.authHttp.request(req)
67 .catch(this.restExtractor.handleError)
68 }
69
70 getVideos (videoPagination: VideoPagination, sort: SortField) {
71 const pagination = this.videoPaginationToRestPagination(videoPagination)
72
73 let params = new HttpParams()
74 params = this.restService.addRestGetParams(params, pagination, sort)
75
76 return this.authHttp.get(VideoService.BASE_VIDEO_URL, { params })
77 .map(this.extractVideos)
78 .catch((res) => this.restExtractor.handleError(res))
79 }
80
81 searchVideos (search: Search, videoPagination: VideoPagination, sort: SortField) {
82 const url = VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value)
83
84 const pagination = this.videoPaginationToRestPagination(videoPagination)
85
86 let params = new HttpParams()
87 params = this.restService.addRestGetParams(params, pagination, sort)
88
89 if (search.field) params.set('field', search.field)
90
91 return this.authHttp.get<ResultList<VideoServerModel>>(url, { params })
92 .map(this.extractVideos)
93 .catch((res) => this.restExtractor.handleError(res))
94 }
95
96 removeVideo (id: number) {
97 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
98 .map(this.restExtractor.extractDataBool)
99 .catch((res) => this.restExtractor.handleError(res))
100 }
101
102 reportVideo (id: number, reason: string) {
103 const url = VideoService.BASE_VIDEO_URL + id + '/abuse'
104 const body: VideoAbuseCreate = {
105 reason
106 }
107
108 return this.authHttp.post(url, body)
109 .map(this.restExtractor.extractDataBool)
110 .catch(res => this.restExtractor.handleError(res))
111 }
112
113 setVideoLike (id: number) {
114 return this.setVideoRate(id, 'like')
115 }
116
117 setVideoDislike (id: number) {
118 return this.setVideoRate(id, 'dislike')
119 }
120
121 getUserVideoRating (id: number): Observable<UserVideoRate> {
122 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
123
124 return this.authHttp.get(url)
125 .catch(res => this.restExtractor.handleError(res))
126 }
127
128 private videoPaginationToRestPagination (videoPagination: VideoPagination) {
129 const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
130 const count: number = videoPagination.itemsPerPage
131
132 return { start, count }
133 }
134
135 private setVideoRate (id: number, rateType: VideoRateType) {
136 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
137 const body: UserVideoRateUpdate = {
138 rating: rateType
139 }
140
141 return this.authHttp.put(url, body)
142 .map(this.restExtractor.extractDataBool)
143 .catch(res => this.restExtractor.handleError(res))
144 }
145
146 private extractVideos (result: ResultList<VideoServerModel>) {
147 const videosJson = result.data
148 const totalVideos = result.total
149 const videos = []
150
151 for (const videoJson of videosJson) {
152 videos.push(new Video(videoJson))
153 }
154
155 return { videos, totalVideos }
156 }
157 }