PeerTube/client/src/app/shared/video/video.service.ts

170 lines
6.0 KiB
TypeScript
Raw Normal View History

2017-10-31 16:37:37 +01:00
import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
2017-12-01 18:56:26 +01:00
import { Injectable } from '@angular/core'
import 'rxjs/add/operator/catch'
import 'rxjs/add/operator/map'
2017-12-01 18:56:26 +01:00
import { Observable } from 'rxjs/Observable'
import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared'
import { ResultList } from '../../../../../shared/models/result-list.model'
import { UserVideoRateUpdate } from '../../../../../shared/models/videos/user-video-rate-update.model'
import { UserVideoRate } from '../../../../../shared/models/videos/user-video-rate.model'
import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
import { RestExtractor } from '../rest/rest-extractor.service'
import { RestService } from '../rest/rest.service'
2017-12-05 17:46:33 +01:00
import { Search } from '../header/search.model'
2017-12-01 18:56:26 +01:00
import { UserService } from '../users/user.service'
import { SortField } from './sort-field.type'
2017-10-25 16:43:19 +02:00
import { VideoDetails } from './video-details.model'
import { VideoEdit } from './video-edit.model'
import { VideoPagination } from './video-pagination.model'
2017-12-01 18:56:26 +01:00
import { Video } from './video.model'
2016-03-14 13:50:19 +01:00
@Injectable()
export class VideoService {
private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
2016-03-14 13:50:19 +01:00
constructor (
private authHttp: HttpClient,
private restExtractor: RestExtractor,
private restService: RestService
2016-05-27 17:49:18 +02:00
) {}
getVideo (uuid: string): Observable<VideoDetails> {
2017-10-25 16:43:19 +02:00
return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
.map(videoHash => new VideoDetails(videoHash))
.catch((res) => this.restExtractor.handleError(res))
2016-05-27 17:49:18 +02:00
}
2016-03-14 13:50:19 +01:00
2017-11-30 09:21:11 +01:00
viewVideo (uuid: string): Observable<VideoDetails> {
return this.authHttp.post(VideoService.BASE_VIDEO_URL + uuid + '/views', {})
.map(this.restExtractor.extractDataBool)
.catch(this.restExtractor.handleError)
}
2017-10-25 16:43:19 +02:00
updateVideo (video: VideoEdit) {
const language = video.language ? video.language : null
const body: VideoUpdate = {
name: video.name,
category: video.category,
licence: video.licence,
language,
description: video.description,
2017-10-31 11:52:52 +01:00
privacy: video.privacy,
tags: video.tags,
nsfw: video.nsfw
}
return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
.map(this.restExtractor.extractDataBool)
.catch(this.restExtractor.handleError)
}
uploadVideo (video: FormData) {
const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
2017-10-31 11:52:52 +01:00
return this.authHttp
.request(req)
.catch(this.restExtractor.handleError)
}
2017-10-31 11:52:52 +01:00
getMyVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
const pagination = this.videoPaginationToRestPagination(videoPagination)
2016-05-23 11:07:42 +02:00
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
2016-03-14 13:50:19 +01:00
2017-10-31 11:52:52 +01:00
return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
.map(this.extractVideos)
.catch((res) => this.restExtractor.handleError(res))
2016-03-14 13:50:19 +01:00
}
2017-10-31 11:52:52 +01:00
getVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
const pagination = this.videoPaginationToRestPagination(videoPagination)
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
return this.authHttp
.get(VideoService.BASE_VIDEO_URL, { params })
.map(this.extractVideos)
.catch((res) => this.restExtractor.handleError(res))
}
2017-12-05 17:46:33 +01:00
searchVideos (search: string, videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
const url = VideoService.BASE_VIDEO_URL + 'search'
const pagination = this.videoPaginationToRestPagination(videoPagination)
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
2017-12-05 17:46:33 +01:00
params = params.append('search', search)
2016-05-23 11:07:42 +02:00
2017-10-31 11:52:52 +01:00
return this.authHttp
.get<ResultList<VideoServerModel>>(url, { params })
.map(this.extractVideos)
.catch((res) => this.restExtractor.handleError(res))
}
removeVideo (id: number) {
2017-10-31 11:52:52 +01:00
return this.authHttp
.delete(VideoService.BASE_VIDEO_URL + id)
.map(this.restExtractor.extractDataBool)
.catch((res) => this.restExtractor.handleError(res))
2016-05-27 17:49:18 +02:00
}
loadCompleteDescription (descriptionPath: string) {
return this.authHttp
.get(API_URL + descriptionPath)
.map(res => res['description'])
.catch((res) => this.restExtractor.handleError(res))
2017-03-08 21:35:43 +01:00
}
setVideoLike (id: number) {
return this.setVideoRate(id, 'like')
2017-03-08 21:35:43 +01:00
}
setVideoDislike (id: number) {
return this.setVideoRate(id, 'dislike')
2017-03-08 21:35:43 +01:00
}
getUserVideoRating (id: number): Observable<UserVideoRate> {
const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
2017-03-08 21:35:43 +01:00
2017-10-31 11:52:52 +01:00
return this.authHttp
.get(url)
.catch(res => this.restExtractor.handleError(res))
2017-03-08 21:35:43 +01:00
}
private videoPaginationToRestPagination (videoPagination: VideoPagination) {
const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
const count: number = videoPagination.itemsPerPage
return { start, count }
Add ability for an administrator to remove any video (#61) * Add ability for an admin to remove every video on the pod. * Server: add BlacklistedVideos relation. * Server: Insert in BlacklistedVideos relation upon deletion of a video. * Server: Modify BlacklistedVideos schema to add Pod id information. * Server: Moving insertion of a blacklisted video from the `afterDestroy` hook into the process of deletion of a video. To avoid inserting a video when it is removed on its origin pod. When a video is removed on its origin pod, the `afterDestroy` hook is fire, but no request is made on the delete('/:videoId') interface. Hence, we insert into `BlacklistedVideos` only on request on delete('/:videoId') (if requirements for insertion are met). * Server: Add removeVideoFromBlacklist hook on deletion of a video. We are going to proceed in another way :). We will add a new route : /:videoId/blacklist to blacklist a video. We do not blacklist a video upon its deletion now (to distinguish a video blacklist from a regular video delete) When we blacklist a video, the video remains in the DB, so we don't have any concern about its update. It just doesn't appear in the video list. When we remove a video, we then have to remove it from the blacklist too. We could also remove a video from the blacklist to 'unremove' it and make it appear again in the video list (will be another feature). * Server: Add handler for new route post(/:videoId/blacklist) * Client: Add isBlacklistable method * Client: Update isRemovableBy method. * Client: Move 'Delete video' feature from the video-list to the video-watch module. * Server: Exclude blacklisted videos from the video list * Server: Use findAll() in BlacklistedVideos.list() method * Server: Fix addVideoToBlacklist function. * Client: Add blacklist feature. * Server: Use JavaScript Standard Style. * Server: In checkUserCanDeleteVideo, move the callback call inside the db callback function * Server: Modify BlacklistVideo relation * Server: Modifiy Videos methods. * Server: Add checkVideoIsBlacklistable method * Server: Rewrite addVideoToBlacklist method * Server: Fix checkVideoIsBlacklistable method * Server: Add return to addVideoToBlacklist method
2017-04-26 21:22:10 +02:00
}
private setVideoRate (id: number, rateType: VideoRateType) {
const url = VideoService.BASE_VIDEO_URL + id + '/rate'
const body: UserVideoRateUpdate = {
2017-03-08 21:35:43 +01:00
rating: rateType
}
2017-03-08 21:35:43 +01:00
2017-10-31 11:52:52 +01:00
return this.authHttp
.put(url, body)
.map(this.restExtractor.extractDataBool)
.catch(res => this.restExtractor.handleError(res))
2017-01-20 19:22:15 +01:00
}
private extractVideos (result: ResultList<VideoServerModel>) {
const videosJson = result.data
const totalVideos = result.total
const videos = []
for (const videoJson of videosJson) {
videos.push(new Video(videoJson))
}
return { videos, totalVideos }
}
2016-03-14 13:50:19 +01:00
}